blib/lib/WebService/MyGengo/Base.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 158 CC 2 SCAR 10.6
CC
2
Cov
0%
to_hash
6.0
_build_attributes_to_serialize
2.0
BEGIN
2.0
CRAP 2.9
1 package WebService::MyGengo::Base;
2  
3 use Moose;
4 use Moose::Util::TypeConstraints;
5 use MooseX::Types::DateTime;# qw(DateTime Duration);
6 use Scalar::Util qw(blessed);
7 use namespace::autoclean;
8  
9 use DateTime;
10  
11 =head1 NAME
12  
13 WebService::MyGengo::Base - Generic base class for all objects in the
14 WebService::MyGengo namespace
15  
16 =head1 DESCRIPTION
17  
18 Provides custom subtypes and coercions used by child classes.
19  
20 =cut
21 #todo These are supposed to be global. So can I just define them
22 #in the classes in which they're used, instead?
23 subtype 'WebService::MyGengo::LanguageCode'
24 , as 'Str'
25 , where { my $len = length($_); ($len == 2 || $len == 5) }
26 , message { "Valid language codes are 2 or 5 characters: '$_'" }
27 ;
28  
29 # todo Not all values in this range are valid error codes
30 subtype 'WebService::MyGengo::ErrorCode'
31 , as 'Num'
32 , where { $_ == 0 || ($_ > 999 && $_ < 2801) }
33 , message { "Invalid API error code: $_" }
34 ;
35  
36 subtype 'WebService::MyGengo::Tier'
37 , as 'Str'
38 , where {
39 my $val = shift;
40 grep { $val eq $_ } qw/machine standard pro ultra ultra_pro/;
41 }
42 , message { "Valid tiers are: machine, standard, pro, ultra, ultra_pro" }
43 ;
44  
45 # todo Do we still need this?
46 subtype 'WebService::MyGengo::CommentThread'
47 , as 'ArrayRef[WebService::MyGengo::Comment]'
48 ;
49  
50 subtype 'WebService::MyGengo::DateTime'
51 , as "DateTime";
52 coerce 'WebService::MyGengo::DateTime'
53 , from 'Undef'
54 , via { 'DateTime'->from_epoch( epoch => 0 ) }
55 , from 'Num', via { 'DateTime'->from_epoch( epoch => $_ ) }
56 , from 'Str'
57 , via {
58 my ( $datetime ) = ( @_ );
59 $datetime =~ s!JST$!Asia/Tokyo!;
60 $datetime =~ m!^
61 (?:\s+)?(\d{4,4})[/-](\d{1,2})[/-](\d{1,2}) # Required date portion
62 (?:[T\s](\d{1,2}):(\d{1,2}):(\d{1,2}))? # Optional time portion
63 (?:\s?([\w/\+:]+))? # Optional timezone
64 $!x;
65 my ($y, $m, $d, $H, $M, $S, $tz) = ($1, $2, $3, $4, $5, $6, $7);
66  
67 return unless $y && $m && $d;
68
69 $tz ||= 'UTC'; # todo assume UTC OK?
70
71 return eval {
72 return DateTime->new(
73 year => $y,
74 month => $m,
75 day => $d,
76 hour => $H || 0,
77 minute => $M || 0,
78 second => $S || 0,
79 time_zone => $tz,
80 );
81 };
82 }
83 ;
84  
85 subtype 'WebService::MyGengo::DateTime::Duration'
86 , as 'DateTime::Duration';
87 coerce 'WebService::MyGengo::DateTime::Duration'
88 , from 'Undef', via { DateTime::Duration->new( seconds => 0 ) }
89 , from 'Str', via { s/\D/0/g; DateTime::Duration->new( seconds => $_ ) }
90 ;
91  
92 subtype 'WebService::MyGengo::Num'
93 , as 'Num';
94 coerce 'WebService::MyGengo::Num'
95 , from 'Undef', via { 0.0 }
96 ;
97  
98 # todo Convenient, but do they belong here?
99 coerce 'WebService::MyGengo::Comment'
100 , from 'Str'
101 , via { return WebService::MyGengo::Comment->new( body => shift ) }
102 ;
103 coerce 'WebService::MyGengo::CommentThread'
104 , from 'ArrayRef[HashRef]'
105 , via {
106 my $obj = shift;
107 my @a;
108 push @a, WebService::MyGengo::Comment->new( $_ )
109 foreach ( @$obj );
110 return \@a;
111 }
112 ;
113  
114 subtype 'WebService::MyGengo::body_tgt'
115 , as 'Str';
116 coerce 'WebService::MyGengo::body_tgt'
117 , from 'Undef' , via { '' } ;
118  
119 subtype 'WebService::MyGengo::Job::Status'
120 , as 'Str'
121 , where {
122 my $val = shift;
123 grep { $val eq $_ } qw/unpaid available pending reviewable revising
124 approved rejected cancelled held/;
125 }
126 , message { "Valid statuses are: unpaid, available, pending, reviewable"
127 . ", revising, approved, rejected, cancelled, held"
128 }
129 ;
130  
131 subtype 'WebService::MyGengo::UnitType'
132 , as 'Str'
133 , where { m/word|character/ }
134 , message { "Invalid translation unit: '$_'" }
135 ;
136  
137 subtype 'WebService::MyGengo::URI'
138 , as 'URI';
139 coerce 'WebService::MyGengo::URI'
140 , from 'Str'
141 , via { URI->new(shift) }
142 ;
143  
144 =head1 ATTRIBUTES
145  
146 =head2 attributes_to_serialize
147  
148 A list of attribute names to be serialized by the `to_struct` method.
149  
150 Override `_build_attributes_to_serialize` in subclasses.
151  
152 =cut
153 #todo Use traits for serializable attributes.
154 has attributes_to_serialize => (
155 is => 'ro'
156 , isa => 'ArrayRef'
157 , lazy => 1
158 , builder => '_build_attributes_to_serialize'
159 );
160 sub _build_attributes_to_serialize {
161 return [
162 grep { $_ ne 'attributes_to_serialize' }
163 map { $_->name }
164 shift->meta->get_all_attributes
165 ];
166 }
167  
168 =head1 METHODS
169  
170 =head2 to_hash( \@attributes? )
171  
172 Returns a reference to a hash of object attributes and values.
173  
174 By default, the attributes to serialize are obtained from the
175 `attributes_to_serialize` attribute of the object.
176  
177 A custom set of attributes can be retrieved by supplying the optional
178 \@attributes argument
179  
180 If you supply an empty arrayref for `\@attributes` then all public attributes of
181 the object will be supplied.
182  
183 =cut
184 #todo Again: use traits for serializable attributes.
185 sub to_hash {
186 my ( $self, $attrs ) = ( shift, @_ );
187  
188 if ( defined($attrs) && ! @$attrs ) {
189 @$attrs = grep { $_ ne 'attributes_to_serialize' && $_ !~ /^_/ }
190 map { $_->name }
191 $self->meta->get_all_attributes;
192 }
193  
194 $attrs ||= $self->attributes_to_serialize;
195  
196 # We can't use a hashslice because the API complains
197 # if we submit empty values.
198 my %hash;
199 my $val;
200 foreach ( @$attrs ) {
201 $val = $self->$_;
202 defined($val) and $hash{$_} = ''.$val;
203 }
204  
205 return \%hash;
206 }
207  
208  
209 __PACKAGE__->meta->make_immutable();
210 1;
211  
212 =head1 AUTHOR
213  
214 Nathaniel Heinrichs
215  
216 =head1 LICENSE
217  
218 Copyright (c) 2011, Nathaniel Heinrichs <nheinric-at-cpan.org>.
219 All rights reserved.
220  
221 This library is free software. You can redistribute it and/or modify
222 it under the same terms as Perl itself.
223  
224 =cut