File Coverage

blib/lib/Data/OpenStruct/Deep.pm
Criterion Covered Total %
statement 34 36 94.4
branch 9 10 90.0
condition n/a
subroutine 7 8 87.5
pod 2 2 100.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             package Data::OpenStruct::Deep;
2              
3 3     3   1553 use 5.008001;
  3         18  
  3         119  
4 3     3   16 use strict;
  3         5  
  3         82  
5 3     3   14 use warnings;
  3         6  
  3         81  
6 3     3   3637 use Storable ();
  3         11190  
  3         69  
7 3     3   2809 use Want ();
  3         6060  
  3         969  
8              
9             our $VERSION = '0.03';
10              
11             sub new {
12 2     2 1 40 my $class = shift;
13 2 100       15 my $fields = ref $_[0] eq 'HASH' ? $_[0] : { @_ };
14 2         270 return bless { __stack => [], __fields => Storable::dclone($fields) }, $class;
15             }
16              
17             sub to_hash {
18 0     0 1 0 my $self = shift;
19 0         0 return Storable::dclone($self->{__fields});
20             }
21              
22             our $AUTOLOAD;
23             sub AUTOLOAD {
24 24     24   93 my ($self, $value) = @_;
25              
26 24         197 my ($method) = $AUTOLOAD =~ /([^:]+)$/;
27 24 50       75 return if $method eq 'DESTROY';
28              
29 24 100       89 if (Want::want('OBJECT', 'SCALAR')) {
30             # method chain
31 11         959 push @{ $self->{__stack} }, $method;
  11         37  
32 11         94 return $self;
33             }
34             else {
35 13         653 my $node = $self->{__fields}; # root node
36              
37             # calc chain
38 13         24 for my $name (@{ $self->{__stack} }) {
  13         41  
39             # override for chain
40 11 100       42 unless (ref $node->{$name} eq 'HASH') {
41 2         9 $node->{$name} = {};
42             }
43              
44 11         29 $node = $node->{$name};
45             }
46 13         34 $self->{__stack} = []; # clear stack
47              
48 13 100       47 $node->{$method} = $value if defined $value;
49 13         95 return $node->{$method};
50             }
51             }
52              
53             1;
54              
55             =head1 NAME
56              
57             Data::OpenStruct::Deep - allows you to create data objects and set arbitrary attributes deeply
58              
59             =head1 SYNOPSIS
60              
61             use Data::OpenStruct::Deep;
62              
63             my %hash = (
64             foo => 1,
65             bar => {
66             baz => 2,
67             },
68             );
69              
70             my $struct = Data::OpenStruct::Deep->new(%hash);
71             my $foo = $struct->foo; #=> 1
72             my $bar = $struct->bar; #=> { baz => 2 }
73             my $baz = $struct->bar->baz; #=> 2
74              
75             my $empty = Data::OpenStruct::Deep->new;
76             $empty->foo->bar->baz->quux('deeply'); # deeply, ok
77             print $empty->foo->bar; #=> { baz => { quux => "deeply" } }
78             print $empty->foo->bar->baz->quux; #=> "deeply"
79              
80             =head1 DESCRIPTION
81              
82             This module allows you to create data objects and set arbitrary attributes.
83              
84             It is like a hash with a different way to access the data.
85             In fact, it is implemented with a hash and C,
86             and you can initialize it with one.
87              
88             =head1 METHODS
89              
90             =head2 new(%hash?)
91              
92             =head2 to_hash
93              
94             =head1 AUTHOR
95              
96             NAKAGAWA Masaki Emasaki@cpan.orgE
97              
98             =head1 LICENSE
99              
100             This library is free software; you can redistribute it and/or modify
101             it under the same terms as Perl itself.
102              
103             =head1 SEE ALSO
104              
105             L, L, L,
106              
107             L
108              
109             =cut