File Coverage

lib/Amazon/SimpleDB/Item.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Amazon::SimpleDB::Item;
2 1     1   1973 use strict;
  1         4  
  1         413  
3 1     1   7 use warnings;
  1         2  
  1         32  
4              
5 1     1   48 use Amazon::SimpleDB::Response;
  0            
  0            
6             use Carp qw( croak );
7              
8             sub new {
9             my $class = shift;
10             my $args = shift || {};
11             my $self = bless $args, $class;
12             croak "No account" unless $self->{account};
13             croak "No domain" unless $self->{domain};
14             croak "No (item) name" unless $self->{name};
15             return $self;
16             }
17              
18             sub account { return $_[0]->{account} }
19             sub name { return $_[0]->{name} }
20             sub domain { return $_[0]->{domain} }
21              
22             sub get_attributes {
23             my ($self, $name) = @_;
24             my $params = {DomainName => $self->{domain}, ItemName => $self->{name}};
25             $params->{AttributeName} = $name
26             if defined $name; # specific attribute request
27             my $account = $self->{account};
28             return
29             Amazon::SimpleDB::Response->new(
30             http_response => $account->request('GetAttributes', $params),
31             domain => $self,
32             account => $self->{account},
33             );
34             }
35              
36             sub put_attributes {
37             my ($self, $attr) = @_;
38             my $params = _process_attribute_params($self, $attr);
39             my $account = $self->{account};
40             return
41             Amazon::SimpleDB::Response->new(
42             http_response => $account->request('PutAttributes', $params),
43             domain => $self,
44             account => $self->{account},
45             );
46             }
47              
48             sub post_attributes {
49             my ($self, $attr) = @_;
50             my $params = _process_attribute_params($self, $attr, 1);
51             my $account = $self->{account};
52             return
53             Amazon::SimpleDB::Response->new(
54             http_response => $account->request('PutAttributes', $params),
55             domain => $self,
56             account => $self->{account},
57             );
58             }
59              
60             sub delete_attributes {
61             my ($self, $attr) = @_;
62             my $params = _process_attribute_params($self, $attr);
63             my $account = $self->{account};
64             return
65             Amazon::SimpleDB::Response->new(
66             http_response => $account->request('DeleteAttributes', $params),
67             domain => $self,
68             account => $self->{account},
69             );
70             }
71              
72             #--- utility
73              
74             sub _process_attribute_params {
75             my ($self, $attr, $replace) = @_;
76             my $params = {DomainName => $self->{domain}, ItemName => $self->{name}};
77             return $params unless $attr; # no attributes means the entire item.
78             if (ref $attr eq 'HASH') { # put/delete params with values.
79             my $i = 0;
80             for my $name (keys %$attr) {
81             $attr->{$name} = [$attr->{$name}]
82             unless ref $attr->{$name} eq 'ARRAY';
83             for (@{$attr->{$name}}) {
84             $params->{"Attribute.${i}.Name"} = $name;
85             $params->{"Attribute.${i}.Value"} = $_;
86             $params->{"Attribute.${i}.Replace"} = 'true'
87             if $replace;
88             $i++;
89             }
90             }
91             } elsif (ref $attr eq 'ARRAY') { # delete multiple attributes, all values
92             my $i = 0;
93             for (@$attr) {
94             $params->{"Attribute.${i}.Name"} = $_;
95             $i++;
96             }
97             } else { # delete single attribute, all values
98             $params->{AttributeName} = $attr;
99             }
100             return $params;
101             }
102              
103             1;
104              
105             __END__