| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # $Id: /mirror/coderepos/lang/perl/Class-Data-ConfigHash/trunk/lib/Class/Data/ConfigHash.pm 69719 2008-08-27T02:20:26.319462Z daisuke  $ | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Class::Data::ConfigHash; | 
| 4 | 2 |  |  | 2 |  | 798 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 72 |  | 
| 5 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 73 |  | 
| 6 | 2 |  |  | 2 |  | 9 | use base qw(Class::Data::Inheritable); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 2667 |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.00002'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | __PACKAGE__->mk_classdata(_config => {}); | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub config { | 
| 12 | 16 |  |  | 16 | 1 | 2221 | my $self = shift; | 
| 13 | 16 |  |  |  |  | 87 | my $config_sub = $self->can('_config'); | 
| 14 | 16 |  | 50 |  |  | 53 | my $config = $self->$config_sub() || {}; | 
| 15 | 16 | 100 |  |  |  | 140 | if (@_) { | 
| 16 | 5 | 100 | 100 |  |  | 7 | my $newconfig = { %{@_ > 1 ? {@_} : ($_[0] || {})} }; | 
|  | 5 |  |  |  |  | 4415 |  | 
| 17 | 5 |  |  |  |  | 33 | $self->_config( | 
| 18 |  |  |  |  |  |  | $self->merge_config_hashes( $config, $newconfig ) | 
| 19 |  |  |  |  |  |  | ); | 
| 20 |  |  |  |  |  |  | } else { | 
| 21 |  |  |  |  |  |  | # this is a bit of a kludge, required to make | 
| 22 |  |  |  |  |  |  | # __PACKAGE__->config->{foo} = 'bar'; | 
| 23 |  |  |  |  |  |  | # work in a subclass. Calling the Class::Data::Inheritable setter | 
| 24 |  |  |  |  |  |  | # will create a new _config method in the current class if it's | 
| 25 |  |  |  |  |  |  | # currently inherited from the superclass. So, the can() call will | 
| 26 |  |  |  |  |  |  | # return a different subref in that case and that means we know to | 
| 27 |  |  |  |  |  |  | # copy and reset the value stored in the class data. | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 11 |  |  |  |  | 37 | $self->_config( $config ); | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 11 | 100 |  |  |  | 166 | if ((my $config_sub_now = $self->can('_config')) ne $config_sub) { | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 1 |  |  |  |  | 8 | $config = $self->merge_config_hashes( $config, {} ); | 
| 34 | 1 |  |  |  |  | 5 | $self->$config_sub_now( $config ); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 | 16 |  |  |  |  | 158 | return $config; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub merge_config_hashes | 
| 41 |  |  |  |  |  |  | { | 
| 42 | 6 |  |  | 6 | 1 | 13 | my ($self, $lefthash, $righthash) = @_; | 
| 43 | 6 |  |  |  |  | 16 | return __merge_hashes($lefthash, $righthash); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub __merge_hashes | 
| 47 |  |  |  |  |  |  | { | 
| 48 |  |  |  |  |  |  | # XXX - If Catalyst is in effect, we might just as well use | 
| 49 |  |  |  |  |  |  | # Catalyst::Utils::merge_hashes, I suppose. | 
| 50 | 6 |  |  | 6 |  | 10 | my ( $lefthash, $righthash ) = @_; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 6 | 50 |  |  |  | 19 | if ( !defined $righthash ) { | 
| 53 | 0 |  |  |  |  | 0 | return $lefthash; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 6 | 50 |  |  |  | 12 | if ( !defined $lefthash ) { | 
| 57 | 0 |  |  |  |  | 0 | return $righthash; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 6 |  |  |  |  | 9 | my %merged = %{$lefthash}; | 
|  | 6 |  |  |  |  | 44 |  | 
| 61 | 6 |  |  |  |  | 12 | for my $key ( keys %{$righthash} ) { | 
|  | 6 |  |  |  |  | 19 |  | 
| 62 | 6 |  | 100 |  |  | 35 | my $right_ref = ( ref $righthash->{$key} || '' ) eq 'HASH'; | 
| 63 | 6 |  | 50 |  |  | 46 | my $left_ref = | 
| 64 |  |  |  |  |  |  | ( ( exists $lefthash->{$key} && ref $lefthash->{$key} ) || '' ) eq | 
| 65 |  |  |  |  |  |  | 'HASH'; | 
| 66 | 6 | 50 | 66 |  |  | 23 | if ( $right_ref and $left_ref ) { | 
| 67 | 0 |  |  |  |  | 0 | $merged{$key} = | 
| 68 |  |  |  |  |  |  | __merge_hashes( $lefthash->{$key}, $righthash->{$key} ); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | else { | 
| 71 | 6 |  |  |  |  | 19 | $merged{$key} = $righthash->{$key}; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 6 |  |  |  |  | 33 | return \%merged; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | 1; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | __END__ |