File Coverage

lib/Data/AsObject.pm
Criterion Covered Total %
statement 44 46 95.6
branch 8 12 66.6
condition n/a
subroutine 12 12 100.0
pod n/a
total 64 70 91.4


line stmt bran cond sub pod time code
1             package Data::AsObject;
2             BEGIN {
3 4     4   130635 $Data::AsObject::VERSION = '0.07';
4             }
5              
6             # ABSTRACT: Easy OO access to complex perl data structures
7              
8 4     4   36 use warnings;
  4         8  
  4         106  
9 4     4   23 use strict;
  4         8  
  4         117  
10 4     4   22 use Carp;
  4         6  
  4         296  
11 4     4   27 use Scalar::Util qw(reftype blessed);
  4         7  
  4         370  
12 4     4   1546 use Data::AsObject::Hash;
  4         13  
  4         109  
13 4     4   1826 use Data::AsObject::Array;
  4         10  
  4         123  
14 4     4   21 use namespace::clean;
  4         7  
  4         16  
15              
16             our $__check_type = sub {
17             my $data = shift;
18             return unless $data;
19              
20             my $type = reftype($data);
21              
22             if (defined $type) {
23             if ( $type eq "ARRAY" && ( !blessed($data) || ref($data) =~ /^Data::AsObject::Array::(Strict|Loose|Silent)/ ) ) {
24             return "ARRAY";
25             } elsif ( $type eq "HASH" && ( !blessed($data) || ref($data) =~ /^Data::AsObject::Hash::(Strict|Loose|Silent)/ ) ) {
26             return "HASH";
27             } else {
28             return "";
29             }
30             } else {
31             return "";
32             }
33             };
34              
35             sub __build_dao
36             {
37 6     6   563 my ($class, $sub, $arg) = @_;
38 6 50       36 my $mode = $arg->{mode} if $arg;
39              
40             return sub
41             {
42 8     8   1168 my @result;
43 8         54 push @result, __bless_dao($_, $mode) for @_;
44 8 50       30 return wantarray ? @result : $result[0];
45             }
46 6         35 }
47              
48             sub __bless_dao
49             {
50 11     11   22 my ($data, $mode) = @_;
51              
52 11 100       28 if ($mode)
53             {
54 6 50       26 croak "Unknown mode '$mode' for dao construction" unless $mode =~/^strict|loose|silent$/;
55 6         16 $mode = ucfirst($mode);
56             }
57             else
58             {
59 5         11 $mode = 'Strict';
60             }
61              
62 11         21 my $array_class = "Data::AsObject::Array::$mode";
63 11         20 my $hash_class = "Data::AsObject::Hash::$mode";
64              
65 11         34 my $type = reftype($data);
66              
67 11         14 my $dao;
68              
69 11 100       39 if ($type eq "ARRAY")
    50          
70             {
71 1         5 $dao = bless $data, $array_class;
72             }
73             elsif ($type eq "HASH")
74             {
75 10         48 $dao = bless $data, $hash_class;
76             }
77             else
78             {
79 0         0 carp "Invalid argument to dao: must be hashref or arrayref!";
80 0         0 $dao = undef;
81             }
82 11         42 return $dao;
83             }
84              
85 4     4   10290 use Sub::Exporter -setup => { exports => [ dao => \'__build_dao' ] };
  4         73666  
  4         37  
86              
87             1;
88              
89              
90              
91              
92             __END__