| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #  You may distribute under the terms of the GNU General Public License | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | #  (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package Circle::Collection; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 4 |  |  | 4 |  | 23 | use strict; | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 145 |  | 
| 8 | 4 |  |  | 4 |  | 22 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 118 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 4 |  |  | 4 |  | 23 | use Carp; | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 4 |  |  |  |  | 326 |  | 
| 11 |  |  |  |  |  |  | require attributes; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 4 |  |  | 4 |  | 35 | use Attribute::Storage qw( apply_subattrs_for_pkg ); | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 65 |  | 
| 14 | 4 |  |  | 4 |  | 246 | use Class::Method::Modifiers qw( install_modifier ); | 
|  | 4 |  |  |  |  | 14 |  | 
|  | 4 |  |  |  |  | 10694 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # A template role to merge | 
| 17 |  |  |  |  |  |  | sub import | 
| 18 |  |  |  |  |  |  | { | 
| 19 | 16 |  |  | 16 |  | 43 | my $pkg = shift; | 
| 20 | 16 |  |  |  |  | 77 | my %args = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 16 |  |  |  |  | 48 | my $caller = caller; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 16 | 50 |  |  |  | 76 | my $name = $args{name} or croak "Need a collection name"; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 16 | 50 |  |  |  | 62 | my $attrs = $args{attrs} or croak "Need attrs"; | 
| 27 | 16 | 50 |  |  |  | 64 | ref $attrs eq "ARRAY" or croak "Expected 'attrs' to be an ARRAY"; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 16 |  | 33 |  |  | 104 | my $desc2 = $args{desc_plural} || $name; | 
| 30 | 16 |  | 33 |  |  | 61 | my $desc1 = $args{desc_single} || do { $_ = $name; s/s$//; $_ }; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 16 | 50 |  |  |  | 61 | my $storage = $args{storage} or croak "Need a storage type"; | 
| 33 | 16 |  |  |  |  | 36 | my $config  = $args{config}; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # Now parse it down to several fields | 
| 36 | 16 |  |  |  |  | 30 | my @attrs_all; | 
| 37 |  |  |  |  |  |  | my @attrs_persisted; | 
| 38 | 0 |  |  |  |  | 0 | my %attrs; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 16 |  |  |  |  | 65 | for( my $i = 0; $i < @$attrs; $i += 2 ) { | 
| 41 | 52 |  |  |  |  | 80 | my $name = $attrs->[$i]; | 
| 42 | 52 |  |  |  |  | 73 | my $a = $attrs->[$i+1]; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 52 |  |  |  |  | 74 | push @attrs_all, $name; | 
| 45 | 52 | 100 |  |  |  | 204 | push @attrs_persisted, $name unless $a->{transient}; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 52 |  |  |  |  | 214 | $attrs{$name} = $a; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 16 |  |  |  |  | 29 | my $keyattr = $attrs_all[0]; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 16 |  |  |  |  | 30 | my %commands; | 
| 53 | 16 | 100 |  |  |  | 48 | %commands = %{ $args{commands} } if $args{commands}; | 
|  | 4 |  |  |  |  | 36 |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # Data access code | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 16 |  |  |  |  | 59 | my ( $method_list, $method_get, $method_set, $method_add, $method_del ); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 16 | 100 |  |  |  | 80 | if( ref $storage eq "HASH" ) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 60 | 8 |  |  |  |  | 20 | $method_list = $storage->{list}; | 
| 61 | 8 |  |  |  |  | 14 | $method_get  = $storage->{get}; | 
| 62 | 8 |  |  |  |  | 123 | $method_set  = $storage->{set}; | 
| 63 | 8 |  |  |  |  | 14 | $method_add  = $storage->{add}; | 
| 64 | 8 |  |  |  |  | 16 | $method_del  = $storage->{del}; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | elsif( $storage eq "methods" ) { | 
| 67 | 4 |  |  |  |  | 13 | $method_list = "${name}_list"; | 
| 68 | 4 |  |  |  |  | 10 | $method_get  = "${name}_get"; | 
| 69 | 4 |  |  |  |  | 10 | $method_set  = "${name}_set"; | 
| 70 | 4 |  |  |  |  | 9 | $method_add  = "${name}_add"; | 
| 71 | 4 |  |  |  |  | 10 | $method_del  = "${name}_del"; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | elsif( $storage eq "array" ) { | 
| 74 |  |  |  |  |  |  | $method_list = sub { | 
| 75 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 76 | 0 |  |  |  |  | 0 | return @{ $self->{$name} } | 
|  | 0 |  |  |  |  | 0 |  | 
| 77 | 4 |  |  |  |  | 22 | }; | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | $method_get = sub { | 
| 80 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 81 | 0 |  |  |  |  | 0 | my ( $key ) = @_; | 
| 82 | 0 |  |  |  |  | 0 | return ( grep { $_->{$keyattr} eq $key } @{ $self->{$name} } )[0]; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 83 | 4 |  |  |  |  | 30 | }; | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | $method_add = sub { | 
| 86 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 87 | 0 |  |  |  |  | 0 | my ( $key, $item ) = @_; | 
| 88 |  |  |  |  |  |  | # TODO: something with key | 
| 89 | 0 |  |  |  |  | 0 | push @{ $self->{$name} }, $item; | 
|  | 0 |  |  |  |  | 0 |  | 
| 90 | 4 |  |  |  |  | 21 | }; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | $method_del = sub { | 
| 93 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 94 | 0 |  |  |  |  | 0 | my ( $key, $item ) = @_; | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 0 |  |  |  |  | 0 | my $items = $self->{$name}; | 
| 97 | 0 |  |  |  |  | 0 | my ( $idx ) = grep { $items->[$_] == $item } 0 .. $#$items; | 
|  | 0 |  |  |  |  | 0 |  | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 | 0 |  |  |  | 0 | return 0 unless defined $idx; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  | 0 | splice @$items, $idx, 1, (); | 
| 102 | 0 |  |  |  |  | 0 | return 1; | 
| 103 | 4 |  |  |  |  | 21 | }; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  | else { | 
| 106 | 0 |  |  |  |  | 0 | croak "Unrecognised storage type $storage"; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | # Manipulation commands | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 16 | 50 |  |  |  | 72 | unless( exists $commands{list} ) { | 
| 112 | 16 | 50 |  |  |  | 44 | defined $method_list or croak "No list method defined for list subcommand"; | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | $commands{list} = apply_subattrs_for_pkg( $caller, | 
| 115 |  |  |  |  |  |  | Command_description => qq("List the $desc2"), | 
| 116 |  |  |  |  |  |  | Command_subof       => qq('$name'), | 
| 117 |  |  |  |  |  |  | Command_default     => qq(), | 
| 118 |  |  |  |  |  |  | sub { | 
| 119 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 120 | 0 |  |  |  |  | 0 | my ( $cinv ) = @_; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 |  |  |  |  | 0 | my @items = $self->$method_list; | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 | 0 |  |  |  | 0 | unless( @items ) { | 
| 125 | 0 |  |  |  |  | 0 | $cinv->respond( "No $desc2" ); | 
| 126 | 0 |  |  |  |  | 0 | return; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  |  |  | 0 | my @table; | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  |  |  | 0 | foreach my $item ( @items ) { | 
| 132 | 0 |  |  |  |  | 0 | my @shown_item; | 
| 133 | 0 |  |  |  |  | 0 | foreach my $attr ( @attrs_all ) { | 
| 134 | 0 |  |  |  |  | 0 | my $value = $item->{$attr}; | 
| 135 | 0 | 0 |  |  |  | 0 | push @shown_item, exists $attrs{$attr}{show} ? $attrs{$attr}{show}->( local $_ = $value ) : $value; | 
| 136 |  |  |  |  |  |  | } | 
| 137 | 0 |  |  |  |  | 0 | push @table, \@shown_item; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 0 |  |  |  |  | 0 | $cinv->respond_table( \@table, headings => \@attrs_all ); | 
| 141 | 0 |  |  |  |  | 0 | return; | 
| 142 |  |  |  |  |  |  | } | 
| 143 | 16 |  |  |  |  | 232 | ); | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 16 |  |  |  |  | 1679 | my @opts_add; | 
| 147 |  |  |  |  |  |  | my @opts_mod; | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 16 |  |  |  |  | 50 | foreach ( @attrs_persisted ) { | 
| 150 | 48 | 100 |  |  |  | 133 | next if $_ eq $keyattr; | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 32 |  | 66 |  |  | 138 | my $desc = $attrs{$_}{desc} || $_; | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 32 | 100 |  |  |  | 109 | $desc .= qq[ (default \\"$attrs{$_}{default}\\")] if exists $attrs{$_}{default}; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 32 |  |  |  |  | 107 | push @opts_add, qq('$_=\$', desc => "$desc"); | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 32 | 100 |  |  |  | 196 | push @opts_mod, qq('$_=\$',   desc => "$desc"), | 
| 159 |  |  |  |  |  |  | qq('no-$_=+', desc => "remove $_") unless $attrs{$_}{nomod}; | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 16 | 100 |  |  |  | 82 | unless( exists $commands{add} ) { | 
| 163 | 12 | 50 |  |  |  | 39 | defined $method_add or croak "No add method defined for add subcommand"; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 28 |  |  |  |  | 164 | $commands{add} = apply_subattrs_for_pkg( $caller, | 
| 166 |  |  |  |  |  |  | Command_description => qq("Add a $desc1"), | 
| 167 |  |  |  |  |  |  | Command_subof       => qq('$name'), | 
| 168 |  |  |  |  |  |  | Command_arg         => qq('$keyattr'), | 
| 169 |  |  |  |  |  |  | ( map { +Command_opt => $_ } @opts_add ), | 
| 170 |  |  |  |  |  |  | sub { | 
| 171 | 2 |  |  | 2 |  | 4 | my $self = shift; | 
|  |  |  |  | 1 |  |  |  | 
| 172 | 2 |  |  |  |  | 6 | my ( $key, $opts, $cinv ) = @_; | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 2 | 50 |  |  |  | 10 | if( $self->$method_get( $key ) ) { | 
| 175 | 0 |  |  |  |  | 0 | $cinv->responderr( "Already have a $desc1 '$key'" ); | 
| 176 | 0 |  |  |  |  | 0 | return; | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  |  | 
| 179 | 2 |  |  |  |  | 36 | my $item = { $keyattr => $key }; | 
| 180 | 2 |  | 66 |  |  | 26 | exists $attrs{$_}{default} and $item->{$_} = $attrs{$_}{default} for @attrs_persisted; | 
| 181 |  |  |  |  |  |  |  | 
| 182 | 2 |  | 66 |  |  | 22 | defined $opts->{$_} and $item->{$_} = $opts->{$_} for @attrs_persisted; | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 2 | 50 |  |  |  | 5 | unless( eval { $self->$method_add( $key, $item ); 1 } ) { | 
|  | 2 |  |  |  |  | 17 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 185 | 0 |  |  |  |  | 0 | my $err = "$@"; chomp $err; | 
|  | 0 |  |  |  |  | 0 |  | 
| 186 | 0 |  |  |  |  | 0 | $cinv->responderr( "Cannot add $desc1 '$key' - $err" ); | 
| 187 | 0 |  |  |  |  | 0 | return; | 
| 188 |  |  |  |  |  |  | } | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 2 |  |  |  |  | 21 | $cinv->respond( "Added $desc1 '$key'" ); | 
| 191 | 2 |  |  |  |  | 16 | return; | 
| 192 |  |  |  |  |  |  | } | 
| 193 | 12 |  |  |  |  | 56 | ); | 
| 194 |  |  |  |  |  |  | } | 
| 195 |  |  |  |  |  |  |  | 
| 196 | 16 | 100 |  |  |  | 991 | unless( exists $commands{mod} ) { | 
| 197 | 12 | 50 |  |  |  | 47 | defined $method_get or croak "No get method defined for mod subcommand"; | 
| 198 |  |  |  |  |  |  |  | 
| 199 | 48 |  |  |  |  | 150 | $commands{mod} = apply_subattrs_for_pkg( $caller, | 
| 200 |  |  |  |  |  |  | Command_description => qq("Modify an existing $desc1"), | 
| 201 |  |  |  |  |  |  | Command_subof       => qq('$name'), | 
| 202 |  |  |  |  |  |  | Command_arg         => qq('$keyattr'), | 
| 203 |  |  |  |  |  |  | ( map { +Command_opt => $_ } @opts_mod ), | 
| 204 |  |  |  |  |  |  | sub { | 
| 205 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 206 | 0 |  |  |  |  | 0 | my ( $key, $opts, $cinv ) = @_; | 
| 207 |  |  |  |  |  |  |  | 
| 208 | 0 |  |  |  |  | 0 | my $item = $self->$method_get( $key ); | 
| 209 |  |  |  |  |  |  |  | 
| 210 | 0 | 0 |  |  |  | 0 | unless( $item ) { | 
| 211 | 0 |  |  |  |  | 0 | $cinv->responderr( "No such $desc1 '$key'" ); | 
| 212 | 0 |  |  |  |  | 0 | return; | 
| 213 |  |  |  |  |  |  | } | 
| 214 |  |  |  |  |  |  |  | 
| 215 | 0 |  |  |  |  | 0 | my %mod; | 
| 216 | 0 |  | 0 |  |  | 0 | exists $opts->{$_} and $mod{$_} = $opts->{$_} for @attrs_persisted; | 
| 217 | 0 |  | 0 |  |  | 0 | exists $opts->{"no-$_"} and $mod{$_} = $attrs{$_}{default} for @attrs_persisted; | 
| 218 |  |  |  |  |  |  |  | 
| 219 | 0 | 0 |  |  |  | 0 | if( $method_set ) { | 
| 220 | 0 |  |  |  |  | 0 | $self->$method_set( $key, \%mod ); | 
| 221 |  |  |  |  |  |  | } | 
| 222 |  |  |  |  |  |  | else { | 
| 223 | 0 |  |  |  |  | 0 | $item->{$_} = $mod{$_} for keys %mod; | 
| 224 |  |  |  |  |  |  | } | 
| 225 |  |  |  |  |  |  |  | 
| 226 | 0 |  |  |  |  | 0 | $cinv->respond( "Modified $desc1 '$key'" ); | 
| 227 | 0 |  |  |  |  | 0 | return; | 
| 228 |  |  |  |  |  |  | } | 
| 229 | 12 |  |  |  |  | 95 | ); | 
| 230 |  |  |  |  |  |  | } | 
| 231 |  |  |  |  |  |  |  | 
| 232 | 16 | 100 |  |  |  | 1493 | unless( exists $commands{del} ) { | 
| 233 | 12 | 50 |  |  |  | 506 | defined $method_del or croak "No del method defined for del subcommand"; | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | $commands{del} = apply_subattrs_for_pkg( $caller, | 
| 236 |  |  |  |  |  |  | Command_description => qq("Delete a $desc1"), | 
| 237 |  |  |  |  |  |  | Command_subof       => qq('$name'), | 
| 238 |  |  |  |  |  |  | Command_arg         => qq('$keyattr'), | 
| 239 |  |  |  |  |  |  | sub { | 
| 240 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 241 | 0 |  |  |  |  | 0 | my ( $key, $cinv ) = @_; | 
| 242 |  |  |  |  |  |  |  | 
| 243 | 0 |  |  |  |  | 0 | my $item = $self->$method_get( $key ); | 
| 244 |  |  |  |  |  |  |  | 
| 245 | 0 | 0 |  |  |  | 0 | unless( $item ) { | 
| 246 | 0 |  |  |  |  | 0 | $cinv->responderr( "No such $desc1 '$key'" ); | 
| 247 | 0 |  |  |  |  | 0 | return; | 
| 248 |  |  |  |  |  |  | } | 
| 249 |  |  |  |  |  |  |  | 
| 250 | 0 | 0 |  |  |  | 0 | unless( eval { $self->$method_del( $key, $item ); 1 } ) { | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 251 | 0 |  |  |  |  | 0 | my $err = "$@"; chomp $err; | 
|  | 0 |  |  |  |  | 0 |  | 
| 252 | 0 |  |  |  |  | 0 | $cinv->responderr( "Cannot delete $desc1 '$key' - $err" ); | 
| 253 | 0 |  |  |  |  | 0 | return; | 
| 254 |  |  |  |  |  |  | } | 
| 255 |  |  |  |  |  |  |  | 
| 256 | 0 |  |  |  |  | 0 | $cinv->respond( "Removed $desc1 '$key'" ); | 
| 257 | 0 |  |  |  |  | 0 | return; | 
| 258 |  |  |  |  |  |  | } | 
| 259 | 12 |  |  |  |  | 151 | ); | 
| 260 |  |  |  |  |  |  | } | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | # Now delete present-but-undef ones; these are where the caller vetoed the | 
| 263 |  |  |  |  |  |  | # above autogeneration | 
| 264 | 16 |  | 100 |  |  | 1102 | defined $commands{$_} or delete $commands{$_} for keys %commands; | 
| 265 |  |  |  |  |  |  |  | 
| 266 | 16 |  |  |  |  | 41 | my %subs; | 
| 267 | 16 |  |  |  |  | 158 | $subs{"command_${name}_$_"} = $commands{$_} for keys %commands; | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | $subs{"command_${name}"} = apply_subattrs_for_pkg( $caller, | 
| 270 |  |  |  |  |  |  | Command_description => qq("Display or manipulate $desc2"), | 
| 271 |  |  |  |  |  |  | # body matters not but it needs to be a cloned closure | 
| 272 | 16 |  |  | 0 |  | 55 | do { my $dummy; sub { undef $dummy } } | 
|  | 16 |  |  |  |  | 20 |  | 
|  | 16 |  |  |  |  | 107 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 273 |  |  |  |  |  |  | ); | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | { | 
| 276 | 4 |  |  | 4 |  | 34 | no strict 'refs'; | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 4 |  |  |  |  | 3780 |  | 
|  | 16 |  |  |  |  | 1323 |  | 
| 277 | 16 |  |  |  |  | 62 | *{"${caller}::$_"} = $subs{$_} for keys %subs; | 
|  | 68 |  |  |  |  | 480 |  | 
| 278 |  |  |  |  |  |  | } | 
| 279 |  |  |  |  |  |  |  | 
| 280 | 16 | 100 | 100 |  |  | 322 | if( !defined $config or $config ) { | 
| 281 | 12 |  | 100 |  |  | 77 | my $config_type = $config->{type} || "array"; | 
| 282 | 12 |  |  |  |  | 29 | my $type_array = $config_type eq "array"; | 
| 283 | 12 |  |  |  |  | 33 | my $type_hash  = $config_type eq "hash"; | 
| 284 | 12 | 50 | 66 |  |  | 60 | $type_array or $type_hash or | 
| 285 |  |  |  |  |  |  | die "Expected config type either 'array' or 'hash'"; | 
| 286 |  |  |  |  |  |  |  | 
| 287 |  |  |  |  |  |  | # Optional config-related methods | 
| 288 | 12 |  |  |  |  | 26 | my $method_store = $config->{store}; | 
| 289 | 12 |  |  |  |  | 22 | my $method_load  = $config->{load}; | 
| 290 |  |  |  |  |  |  |  | 
| 291 |  |  |  |  |  |  | # Configuration load/store | 
| 292 |  |  |  |  |  |  | install_modifier $caller, after => load_configuration => sub { | 
| 293 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 294 | 0 |  |  |  |  | 0 | my ( $ynode ) = @_; | 
| 295 |  |  |  |  |  |  |  | 
| 296 | 0 | 0 |  |  |  | 0 | my $ynodes = $ynode->{$name} or return; | 
| 297 |  |  |  |  |  |  |  | 
| 298 | 0 | 0 |  |  |  | 0 | foreach my $this ( $type_array ? @$ynodes : keys %$ynodes ) { | 
| 299 | 0 |  |  |  |  | 0 | my $item = {}; | 
| 300 | 0 | 0 |  |  |  | 0 | my $n = $type_array ? $this : $ynodes->{$this}; | 
| 301 |  |  |  |  |  |  |  | 
| 302 | 0 |  |  |  |  | 0 | $item->{$_} = $n->{$_} for @attrs_persisted; | 
| 303 | 0 | 0 |  |  |  | 0 | $item->{$keyattr} = $this if $type_hash; | 
| 304 |  |  |  |  |  |  |  | 
| 305 | 0 |  |  |  |  | 0 | $self->$method_add( $item->{$keyattr}, $item ); | 
| 306 |  |  |  |  |  |  |  | 
| 307 | 0 | 0 | 0 |  |  | 0 | if( ref $method_load or $method_load && $self->can( $method_load ) ) { | 
|  |  |  | 0 |  |  |  |  | 
| 308 | 0 |  |  |  |  | 0 | $self->$method_load( $item->{$keyattr}, $n ); | 
| 309 |  |  |  |  |  |  | } | 
| 310 |  |  |  |  |  |  | } | 
| 311 | 12 |  |  |  |  | 303 | }; | 
| 312 |  |  |  |  |  |  |  | 
| 313 |  |  |  |  |  |  | install_modifier $caller, after => store_configuration => sub { | 
| 314 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 315 | 0 |  |  |  |  |  | my ( $ynode ) = @_; | 
| 316 |  |  |  |  |  |  |  | 
| 317 | 0 | 0 | 0 |  |  |  | my $ynodes = $ynode->{$name} ||= $type_array ? [] : YAML::Node->new({}); | 
| 318 | 0 | 0 |  |  |  |  | $type_array ? ( @$ynodes = () ) : ( %$ynodes = () ); | 
| 319 |  |  |  |  |  |  |  | 
| 320 | 0 |  |  |  |  |  | foreach my $item ( $self->$method_list ) { | 
| 321 | 0 |  |  |  |  |  | my $n = YAML::Node->new({}); | 
| 322 |  |  |  |  |  |  |  | 
| 323 | 0 |  | 0 |  |  |  | defined $item->{$_} and $n->{$_} = $item->{$_} for @attrs_persisted; | 
| 324 |  |  |  |  |  |  |  | 
| 325 | 0 | 0 | 0 |  |  |  | if( ref $method_store or $method_store && $self->can( $method_store ) ) { | 
|  |  |  | 0 |  |  |  |  | 
| 326 | 0 |  |  |  |  |  | $self->$method_store( $item->{$keyattr}, $n ); | 
| 327 |  |  |  |  |  |  | } | 
| 328 |  |  |  |  |  |  |  | 
| 329 |  |  |  |  |  |  | $type_array ? | 
| 330 |  |  |  |  |  |  | ( push @$ynodes, $n ) : | 
| 331 | 0 | 0 |  |  |  |  | do { $ynodes->{$n->{$keyattr}} = $n; delete $n->{$keyattr} }; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 332 |  |  |  |  |  |  | } | 
| 333 | 12 |  |  |  |  | 3152 | }; | 
| 334 |  |  |  |  |  |  | } | 
| 335 |  |  |  |  |  |  | } | 
| 336 |  |  |  |  |  |  |  | 
| 337 |  |  |  |  |  |  | 0x55AA; |