| 1 | package Yukki::Role::Savable; | ||||||||||||||||||||||||||||
| 2 | 13 | $Yukki::Role::Savable::VERSION = '0.991_002'; # TRIAL | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | $Yukki::Role::Savable::VERSION = '0.991002';use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use YAML qw( Dump Load ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Provides a mechanism for YAML-izing objects | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 13 | requires 'savable_attributes'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub dump_yaml { | ||||||||||||||||||||||||||||
| 18 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_yaml: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | my %output; | |||||||||||||||||||||||||||
| 21 | 3 | for my $attr ($self->savable_attributes) { | |||||||||||||||||||||||||||
| 22 | 15 | my $v = $self->$attr; | |||||||||||||||||||||||||||
| 23 | 15 | ▶ | if (blessed $v && $v->does('Yukki::Role::Savable')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($v) and $v->does("Yukki::Role::Savable")
MC/DC0%0/2
builtin::blessed($v) and $v->does("Yukki::Role::Savable")
builtin::blessed($v)
$v->does("Yukki::Role::Savable")
Truth table33%1/3
A and B
A= builtin::blessed($v)B= $v->does("Yukki::Role::Savable")
| |||||||||||||||||||||||||||||
| 24 | 0 | $v = $v->dump_yaml; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 15 | $output{ $attr } = $v; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | return Dump(\%output); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub load_yaml { | ||||||||||||||||||||||||||||
| 34 | 1 | ▶ | my ($class, $yaml) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_yaml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my $input = Load($yaml); | |||||||||||||||||||||||||||
| 37 | 1 | $class->new($input); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 13 | 1; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | __END__ | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =pod | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 NAME | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Yukki::Role::Savable - Provides a mechanism for YAML-izing objects | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 VERSION | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | version 0.991_002 | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | package Yukki::Things; | ||||||||||||||||||||||||||||
| 59 | use v5.24; | ||||||||||||||||||||||||||||
| 60 | use Moo; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | with 'Yukki::Role::Savable'; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | has foo => ( is => 'ro' ); | ||||||||||||||||||||||||||||
| 65 | has bar => ( is => 'ro' ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub savable_attributes { qw( foo bar ) } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | my $things = Yukki::Things->new(foo => 1, bar => 2); | ||||||||||||||||||||||||||||
| 70 | path('file.yaml')->spew_utf8($things->dump_yaml); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | my $things = Yukki::Things->load_yaml(path('file.yaml')->slurp_utf8); | ||||||||||||||||||||||||||||
| 73 | say $things->foo; #> 1 | ||||||||||||||||||||||||||||
| 74 | say $things->bar; #> 2 | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | This is intended to provide L<Yukki> with a nice, neat way to save and load some configuration objects in a standard way. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 REQUIRED METHODS | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 savable_attributes | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my @attr = $obj->savable_attributes; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Returns the list of attributes to save in the YAML. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 METHODS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head2 dump_yaml | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $yaml_text = $obj->dump_yaml; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | This converts the object to YAML and returns the ext. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 load_yaml | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | my $obj = $class->load_yaml($yaml_text); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | This constructs a new class from the data loaded from the given YAML text. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | This software is copyright (c) 2017 by Qubling Software LLC. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 111 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||