File | /data/SimpleDB-Class/author.t/../lib/SimpleDB/Class/Role/Itemized.pm |
Statements Executed | 627 |
Statement Execution Time | 1.85ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 2.15ms | 53.4ms | BEGIN@4 | SimpleDB::Class::Role::Itemized::
30 | 3 | 2 | 671µs | 50.9ms | instantiate_item | SimpleDB::Class::Role::Itemized::
35 | 2 | 1 | 538µs | 728µs | determine_item_class | SimpleDB::Class::Role::Itemized::
5 | 2 | 2 | 320µs | 42.4ms | parse_item | SimpleDB::Class::Role::Itemized::
1 | 1 | 1 | 39µs | 1.85ms | BEGIN@3 | SimpleDB::Class::Role::Itemized::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package SimpleDB::Class::Role::Itemized; | ||||
2 | |||||
3 | 3 | 45µs | 2 | 3.67ms | # spent 1.85ms (39µs+1.81) within SimpleDB::Class::Role::Itemized::BEGIN@3 which was called
# once (39µs+1.81ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 3 # spent 1.85ms making 1 call to SimpleDB::Class::Role::Itemized::BEGIN@3
# spent 1.81ms making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:389] |
4 | 3 | 477µs | 2 | 56.8ms | # spent 53.4ms (2.15+51.3) within SimpleDB::Class::Role::Itemized::BEGIN@4 which was called
# once (2.15ms+51.3ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 4 # spent 53.4ms making 1 call to SimpleDB::Class::Role::Itemized::BEGIN@4
# spent 3.39ms making 1 call to MooseX::Types::Base::import |
5 | |||||
6 | 1 | 3µs | 1 | 144µs | requires 'item_class'; # spent 144µs making 1 call to Moose::Role::requires |
7 | |||||
8 | =head1 NAME | ||||
9 | |||||
10 | SimpleDB::Class::Role::Itemized - Provides utility methods to classes that need to instantiate items. | ||||
11 | |||||
12 | =head1 SYNOPSIS | ||||
13 | |||||
14 | my $class = $self->determine_item_class(\%attributes); | ||||
15 | |||||
16 | my $item = $self->instantiate_item(\%attributes, $id); | ||||
17 | |||||
18 | my $item = $self->parse_item($id, \@attributes); | ||||
19 | |||||
20 | =head1 DESCRIPTION | ||||
21 | |||||
22 | This is a L<Moose::Role> that provides utility methods for instantiating L<SimpleDB::Class::Item>s. | ||||
23 | |||||
24 | =head1 METHODS | ||||
25 | |||||
26 | The following methods are available from this role. | ||||
27 | |||||
28 | =cut | ||||
29 | |||||
30 | #-------------------------------------------------------- | ||||
31 | |||||
32 | =head2 instantiate_item ( attributes, [ id ] ) | ||||
33 | |||||
34 | Instantiates an item based upon it's proper classname and then calls C<update> to populate it's attributes with data. | ||||
35 | |||||
36 | =head3 attributes | ||||
37 | |||||
38 | A hash reference of attribute data. | ||||
39 | |||||
40 | =head3 id | ||||
41 | |||||
42 | An optional id to instantiate the item with. | ||||
43 | |||||
44 | =cut | ||||
45 | |||||
46 | # spent 50.9ms (671µs+50.2) within SimpleDB::Class::Role::Itemized::instantiate_item which was called 30 times, avg 1.70ms/call:
# 15 times (299µs+26.2ms) by SimpleDB::Class::ResultSet::next at line 467 of ../lib/SimpleDB/Class/ResultSet.pm, avg 1.77ms/call
# 11 times (289µs+16.3ms) by SimpleDB::Class::Domain::insert at line 218 of ../lib/SimpleDB/Class/Domain.pm, avg 1.51ms/call
# 4 times (83µs+7.66ms) by SimpleDB::Class::Domain::find at line 158 of ../lib/SimpleDB/Class/Domain.pm, avg 1.94ms/call | ||||
47 | 30 | 65µs | my ($self, $attributes, $id) = @_; | ||
48 | 30 | 115µs | 30 | 88µs | $attributes->{simpledb} = $self->simpledb; # spent 59µs making 15 calls to SimpleDB::Class::Domain::simpledb, avg 4µs/call
# spent 29µs making 15 calls to SimpleDB::Class::ResultSet::simpledb, avg 2µs/call |
49 | 30 | 65µs | if (defined $id && $id ne '') { | ||
50 | $attributes->{id} = $id; | ||||
51 | } | ||||
52 | 30 | 331µs | 60 | 50.1ms | return $self->determine_item_class($attributes)->new($attributes); # spent 49.5ms making 30 calls to Moose::Object::new, avg 1.65ms/call
# spent 622µs making 30 calls to SimpleDB::Class::Role::Itemized::determine_item_class, avg 21µs/call |
53 | } | ||||
54 | |||||
55 | #-------------------------------------------------------- | ||||
56 | |||||
57 | =head2 determine_item_class ( attributes ) | ||||
58 | |||||
59 | Given an attribute list we can determine if an item needs to be recast as a different class. | ||||
60 | |||||
61 | =head3 attributes | ||||
62 | |||||
63 | A hash ref of attributes. | ||||
64 | |||||
65 | =cut | ||||
66 | |||||
67 | # spent 728µs (538+190) within SimpleDB::Class::Role::Itemized::determine_item_class which was called 35 times, avg 21µs/call:
# 30 times (462µs+161µs) by SimpleDB::Class::Role::Itemized::instantiate_item at line 52, avg 21µs/call
# 5 times (77µs+29µs) by SimpleDB::Class::Role::Itemized::parse_item at line 129, avg 21µs/call | ||||
68 | 35 | 42µs | my ($self, $attributes) = @_; | ||
69 | 35 | 108µs | 35 | 95µs | my $class = $self->item_class; # spent 65µs making 17 calls to SimpleDB::Class::Domain::item_class, avg 4µs/call
# spent 30µs making 18 calls to SimpleDB::Class::ResultSet::item_class, avg 2µs/call |
70 | 35 | 163µs | 35 | 94µs | my $castor = $class->_castor_attribute; # spent 83µs making 32 calls to SimpleDB::Class::Item::_castor_attribute, avg 3µs/call
# spent 11µs making 3 calls to Foo::Child::_castor_attribute, avg 4µs/call |
71 | 35 | 15µs | if ($castor) { | ||
72 | 3 | 4µs | my $reclass = $attributes->{$castor}; | ||
73 | 3 | 3µs | if (ref $reclass eq 'ARRAY') { | ||
74 | $reclass = $reclass->[0]; | ||||
75 | } | ||||
76 | 3 | 6µs | if ($reclass) { | ||
77 | return $reclass; | ||||
78 | } | ||||
79 | } | ||||
80 | 34 | 133µs | return $class; | ||
81 | } | ||||
82 | |||||
83 | #-------------------------------------------------------- | ||||
84 | |||||
85 | =head2 parse_item ( id , attributes ) | ||||
86 | |||||
87 | Converts the attributes section of an item document returned from SimpleDB into a L<SimpleDB::Class::Item> object. | ||||
88 | |||||
89 | =head3 id | ||||
90 | |||||
91 | The ItemName of the item to create. | ||||
92 | |||||
93 | =head3 attributes | ||||
94 | |||||
95 | An array of attributes as returned by L<SimpleDB::Client>. | ||||
96 | |||||
97 | =cut | ||||
98 | |||||
99 | # spent 42.4ms (320µs+42.0) within SimpleDB::Class::Role::Itemized::parse_item which was called 5 times, avg 8.47ms/call:
# 3 times (139µs+12.6ms) by SimpleDB::Class::ResultSet::next at line 468 of ../lib/SimpleDB/Class/ResultSet.pm, avg 4.23ms/call
# 2 times (180µs+29.5ms) by SimpleDB::Class::Domain::find at line 167 of ../lib/SimpleDB/Class/Domain.pm, avg 14.8ms/call | ||||
100 | 5 | 8µs | my ($self, $id, $list) = @_; | ||
101 | 5 | 6µs | unless (ref $list eq 'ARRAY') { | ||
102 | $list = [$list]; | ||||
103 | } | ||||
104 | |||||
105 | # format the data into a reasonable structure | ||||
106 | 5 | 4µs | my $attributes = {}; | ||
107 | 5 | 8µs | foreach my $attribute (@{$list}) { | ||
108 | |||||
109 | # get attribute name | ||||
110 | 46 | 16µs | unless (exists $attribute->{Name}) { | ||
111 | return undef; # empty result set | ||||
112 | } | ||||
113 | 46 | 13µs | my $name = $attribute->{Name}; | ||
114 | |||||
115 | # skip handling the 'id' field | ||||
116 | 46 | 16µs | next if $name eq 'id'; | ||
117 | |||||
118 | # get value | ||||
119 | 46 | 15µs | my $value = $attribute->{Value}; | ||
120 | 46 | 9µs | if (ref $value eq 'HASH' && !(keys %{$value})) { # undef comes back as an empty hash for some reason | ||
121 | next; # no need to store undef | ||||
122 | } | ||||
123 | |||||
124 | # store attribute list | ||||
125 | 46 | 73µs | push @{$attributes->{$name}}, $value; | ||
126 | } | ||||
127 | |||||
128 | # now we can determine the item's class from attributes if necessary | ||||
129 | 5 | 15µs | 5 | 106µs | my $item_class = $self->determine_item_class($attributes); # spent 106µs making 5 calls to SimpleDB::Class::Role::Itemized::determine_item_class, avg 21µs/call |
130 | |||||
131 | # now we're ready to instantiate | ||||
132 | 5 | 17µs | 5 | 13µs | $attributes->{simpledb} = $self->simpledb; # spent 8µs making 2 calls to SimpleDB::Class::Domain::simpledb, avg 4µs/call
# spent 5µs making 3 calls to SimpleDB::Class::ResultSet::simpledb, avg 2µs/call |
133 | 5 | 6µs | $attributes->{id} = $id; | ||
134 | 5 | 63µs | 5 | 41.9ms | return $item_class->new($attributes); # spent 41.9ms making 5 calls to Moose::Object::new, avg 8.39ms/call |
135 | } | ||||
136 | |||||
137 | =head1 LEGAL | ||||
138 | |||||
139 | SimpleDB::Class is Copyright 2009-2010 Plain Black Corporation (L<http://www.plainblack.com/>) and is licensed under the same terms as Perl itself. | ||||
140 | |||||
141 | =cut | ||||
142 | |||||
143 | |||||
144 | 1 | 5µs | 1; |