File Coverage

blib/lib/Object/FromData.pm
Criterion Covered Total %
statement 20 21 95.2
branch 4 6 66.6
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 31 34 91.1


line stmt bran cond sub pod time code
1             package Object::FromData;
2              
3 3     3   239123 use strict;
  3         4  
  3         66  
4 3     3   13 use warnings;
  3         3  
  3         63  
5 3     3   929 use Object::FromData::Array;
  3         4  
  3         62  
6 3     3   974 use Object::FromData::Hash;
  3         5  
  3         77  
7 3     3   15 use Carp 'confess';
  3         6  
  3         421  
8              
9             =head1 NAME
10              
11             Object::FromData - Throw arrayrefs or hashrefs at this to get an object
12              
13             =head1 VERSION
14              
15             Version 0.01
16              
17             =cut
18              
19             our $VERSION = '0.02';
20              
21             =head1 SYNOPSIS
22              
23             use Object::FromData;
24              
25             my $object = Object::FromData->new(
26             { ref => {
27             foo => 'bar',
28             this => 'that',
29             _private => 'no method will be created',
30             colors => [qw/red blue green/],
31             numbers => {
32             un => 1,
33             deux => 2,
34             trois => 3,
35             },
36             },
37             }
38             );
39              
40             say $object->is_hashref; # 1
41             say $object->is_arrayref; # 0
42              
43             say $object->foo; # bar
44             say $object->this; # that
45             say $object->_private; # BOOM! No such method
46              
47             say $object->numbers->is_hashref; # 0
48             say $object->numbers->is_arrayref; # 1
49             say $object->numbers->deux; # 2
50              
51             my $colors = $object->colors;
52             while ( $colors->has_more ) {
53             say $colors->next; # red, blue, green on successive lines
54             }
55              
56             =head2 C
57              
58             my $object = Object::FromData->new({ ref => $ref });
59              
60             Takes a hashref. The key of C must be a hashref or arrayref. Returns an
61             object. The object represents a hash or an arrayref as created by
62             C or C.
63              
64             The primary purpose of this module was to be able to take arbitrary JSON from
65             a client and create an object I could safely embed in another object.
66              
67             =cut
68              
69             sub new {
70 4     4 1 154 my ( $class, $arg_for ) = @_;
71              
72             my $ref = $arg_for->{ref}
73 4 50       13 or confess "no ref() supplied to Object::FromData::new";
74 4 100       14 if ( 'ARRAY' eq ref $ref ) {
    50          
75 2         10 return Object::FromData::Array->_new( { ref => $ref } );
76             }
77             elsif ( 'HASH' eq ref $ref ) {
78 2         11 return Object::FromData::Hash->_new( { ref => $ref } );
79             }
80             else {
81 0           croak(
82             "Don't know how to convert '$ref' into an object. I only handle arrayrefs and hashrefs"
83             );
84             }
85             }
86              
87             =head1 AUTHOR
88              
89             Curtis "Ovid" Poe, C<< >>
90              
91             =head1 BUGS
92              
93             Please report any bugs or feature requests to C, or through
94             the web interface at L. I will be notified, and then you'll
95             automatically be notified of progress on your bug as I make changes.
96              
97             We've tried to keep the methods minimal, but because we're inheriting from
98             C and C, the latter might
99             have keys which override the main keys. If that happens, call the methods as
100             class methods, passing in the object as an argument:
101              
102             my %hash = (
103             keys => [qw/foo bar baz/],
104             );
105             my $object = Object::FromData->new({ ref => \%hash });
106              
107             my @keys = $hash->keys; # returns an Object::FromData::Array instance of foo, bar, and baz
108             my @keys = Object::FromData::Hash->keys($hash); # returns 'keys'
109              
110             =head1 SUPPORT
111              
112             You can find documentation for this module with the perldoc command.
113              
114             perldoc Object::FromData
115              
116             You can also look for information at:
117              
118             =over 4
119              
120             =item * RT: CPAN's request tracker (report bugs here)
121              
122             L
123              
124             =item * AnnoCPAN: Annotated CPAN documentation
125              
126             L
127              
128             =item * CPAN Ratings
129              
130             L
131              
132             =item * Search CPAN
133              
134             L
135              
136             =back
137              
138              
139             =head1 ACKNOWLEDGEMENTS
140              
141              
142             =head1 LICENSE AND COPYRIGHT
143              
144             Copyright 2016 Curtis "Ovid" Poe.
145              
146             This program is free software; you can redistribute it and/or modify it
147             under the terms of the the Artistic License (2.0). You may obtain a
148             copy of the full license at:
149              
150             L
151              
152             Any use, modification, and distribution of the Standard or Modified
153             Versions is governed by this Artistic License. By using, modifying or
154             distributing the Package, you accept this license. Do not use, modify,
155             or distribute the Package, if you do not accept this license.
156              
157             If your Modified Version has been derived from a Modified Version made
158             by someone other than you, you are nevertheless required to ensure that
159             your Modified Version complies with the requirements of this license.
160              
161             This license does not grant you the right to use any trademark, service
162             mark, tradename, or logo of the Copyright Holder.
163              
164             This license includes the non-exclusive, worldwide, free-of-charge
165             patent license to make, have made, use, offer to sell, sell, import and
166             otherwise transfer the Package with respect to any patent claims
167             licensable by the Copyright Holder that are necessarily infringed by the
168             Package. If you institute patent litigation (including a cross-claim or
169             counterclaim) against any party alleging that the Package constitutes
170             direct or contributory patent infringement, then this Artistic License
171             to you shall terminate on the date that such litigation is filed.
172              
173             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
174             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
175             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
176             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
177             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
178             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
179             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
180             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
181              
182              
183             =cut
184              
185             1; # End of Object::FromData