File Coverage

blib/lib/WWW/Google/Contacts/Base.pm
Criterion Covered Total %
statement 30 63 47.6
branch 9 24 37.5
condition 5 15 33.3
subroutine 7 11 63.6
pod 0 4 0.0
total 51 117 43.5


line stmt bran cond sub pod time code
1             package WWW::Google::Contacts::Base;
2             {
3             $WWW::Google::Contacts::Base::VERSION = '0.39';
4             }
5              
6 20     20   14920 use Moose;
  20         44  
  20         208  
7 20     20   127987 use Scalar::Util qw( blessed );
  20         46  
  20         1192  
8 20     20   115 use Try::Tiny;
  20         40  
  20         1195  
9 20     20   1213 use Data::Dumper;
  20         7407  
  20         18317  
10              
11             sub xml_attributes {
12 51     51 0 75 my $self = shift;
13 51         212 return grep { $_->does('XmlField') } $self->meta->get_all_attributes;
  246         42769  
14             }
15              
16             sub get_xml_key {
17 0     0 0 0 my ( $self, $field ) = @_;
18              
19 0         0 foreach my $attr ( $self->xml_attributes ) {
20 0         0 my $name = $attr->name;
21 0         0 my $val = $self->$name;
22 0 0 0     0 if ( $name eq $field ) {
    0          
23 0         0 return $attr->xml_key;
24             }
25             elsif ( blessed($val) and $val->can("to_xml_hashref") ) {
26 0         0 my $recurse = $val->get_xml_key($field);
27 0 0       0 if ($recurse) {
28 0         0 my $parent = $attr->xml_key;
29 0         0 return { $parent => $recurse };
30             }
31             }
32             }
33 0         0 return undef;
34             }
35              
36             sub to_xml_hashref {
37 3     3 0 25 my $self = shift;
38              
39 3         5 my $to_return = {};
40 3         11 foreach my $attr ( $self->xml_attributes ) {
41 9         1100 my $incl = $attr->include_in_xml;
42 9 100       33 next unless $self->$incl;
43              
44 6         98 my $predicate = $attr->predicate;
45              
46             next
47 6 50 33     300 if defined $predicate
      33        
48             and not $self->$predicate
49             and not $attr->is_lazy;
50              
51 6         20 my $name = $attr->name;
52 6         283 my $val = $self->$name;
53              
54 6 50       15 next if ( not $val );
55              
56             $to_return->{ $attr->xml_key } =
57             ( blessed($val) and $val->can("to_xml_hashref") )
58             ? $val->to_xml_hashref
59             : ( ref($val) and ref($val) eq 'ARRAY' )
60 0         0 ? [ map { $_->to_xml_hashref } @{$val} ]
  0         0  
61 6 50 66     251 : $attr->has_to_xml ? do { my $code = $attr->to_xml; &$code($val) }
  0 50 33     0  
  0 50       0  
    100          
62             : $attr->is_element ? [$val]
63             : $val;
64             }
65 3         39 return $to_return;
66             }
67              
68             sub set_from_server {
69 0     0 0   my ( $self, $data ) = @_;
70              
71 0           foreach my $attr ( $self->xml_attributes ) {
72 0 0         if ( defined $data->{ $attr->xml_key } ) {
73 0 0         if ( my $writer = $attr->writer ) {
74              
75             # write attributes that are read only to the user
76 0           $self->$writer( $data->{ $attr->xml_key } );
77             }
78             else {
79 0           my $name = $attr->name;
80             try {
81 0     0     $self->$name( $data->{ $attr->xml_key } );
82             }
83             catch {
84 0     0     my @err = split m{\n}, $_;
85 0           print "\nERROR - Failed to set attribute\n";
86 0           print "-------------------------------\n";
87 0           print "Attribute: " . $name . "\n";
88 0           print "Value: " . Dumper $data->{ $attr->xml_key };
89 0           print "Error: " . $err[0] . "\n";
90 0           print
91             "\nPlease include the above in an email bug report to magnus\@erixzon.com\n";
92 0           print
93             "Remove personal content in the 'value' hash, but please leave the structure intact.\n\n";
94 0           die "\n";
95 0           };
96             }
97             }
98             }
99 0           return $self;
100             }
101              
102             around BUILDARGS => sub {
103             my $orig = shift;
104             my $class = shift;
105              
106             return $class->$orig() unless (@_);
107              
108             # let's see if we need to mangle xml fields
109             my $data;
110             if ( @_ > 1 ) {
111             $data = {@_};
112             }
113             else {
114             $data = shift @_;
115             }
116              
117             foreach my $attr ( $class->xml_attributes ) {
118             if ( defined $data->{ $attr->xml_key } ) {
119             $data->{ $attr->name } = delete $data->{ $attr->xml_key };
120             }
121             }
122             return $class->$orig($data);
123             };
124              
125 20     20   123 no Moose;
  20         45  
  20         152  
126             __PACKAGE__->meta->make_immutable;
127             1;
128             __END__