Riak/Role/PBC/Object.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 113 CC 12 SCAR 33.9
CC
12
Cov
0%
populate_object
56.0
store_object
30.0
retrieve_sibling
6.0
CRAP 29.7
1 package Net::Riak::Role::PBC::Object;
2 {
3 $Net::Riak::Role::PBC::Object::VERSION = '0.1702';
4 }
5 use JSON;
6 use Moose::Role;
7 use List::Util 'first';
8  
9 sub store_object {
10 my ($self, $w, $dw, $object) = @_;
11  
12 die "Storing object without a key is not supported in the PBC interface" unless $object->key;
13  
14 my $value = (ref $object->data && $object->content_type eq 'application/json')
15 ? JSON::encode_json($object->data) : $object->data;
16  
17 my $content = {
18 content_type => $object->content_type,
19 value => $value,
20 };
21  
22 if ($object->has_links) {
23 $content->{links} = $self->_links_for_message($object);
24 }
25  
26 if ($object->has_meta) {
27 $content->{usermeta} = $self->_metas_for_message($object);
28 }
29  
30 $self->send_message(
31 PutReq => {
32 bucket => $object->bucket->name,
33 key => $object->key,
34 content => $content,
35 }
36 );
37 return $object;
38 }
39  
40 sub load_object {
41 my ( $self, $params, $object ) = @_;
42  
43 my $resp = $self->send_message(
44 GetReq => {
45 bucket => $object->bucket->name,
46 key => $object->key,
47 r => $params->{r},
48 }
49 );
50  
51 $self->populate_object($object, $resp);
52  
53 return $object;
54 }
55  
56 sub delete_object {
57 my ( $self, $params, $object ) = @_;
58  
59 my $resp = $self->send_message(
60 DelReq => {
61 bucket => $object->bucket->name,
62 key => $object->key,
63 rw => $params->{dw},
64 }
65 );
66  
67 $object;
68 }
69  
70 sub populate_object {
71 my ( $self, $object, $resp) = @_;
72  
73 $object->_clear_links;
74 $object->exists(0);
75  
76 if ( $resp->content && scalar (@{$resp->content}) > 1) {
77 my %seen;
78 my @vtags = grep { !$seen{$_}++ } map { $_->vtag } @{$resp->content};
79 $object->siblings(\@vtags);
80 }
81  
82 my $content = $resp->content ? $resp->content->[0] : undef;
83  
84 return unless $content and $resp->vclock;
85  
86 $object->vclock($resp->vclock);
87 $object->vtag($content->vtag);
88 $object->content_type($content->content_type);
89  
90 if($content->links) {
91 $self->_populate_links($object, $content->links);
92 }
93  
94 if($content->usermeta) {
95 $self->_populate_metas($object, $content->usermeta);
96 }
97  
98 my $data = ($object->content_type eq 'application/json')
99 ? JSON::decode_json($content->value) : $content->value;
100  
101 $object->exists(1);
102  
103 $object->data($data);
104 }
105  
106  
107 # This emulates the behavior of the existing REST client.
108 sub retrieve_sibling {
109 my ($self, $object, $params) = @_;
110  
111 my $resp = $self->send_message(
112 GetReq => {
113 bucket => $object->bucket->name,
114 key => $object->key,
115 r => $params->{r},
116 }
117 );
118  
119 # hack for loading 1 sibling
120 if ($params->{vtag}) {
121 $resp->{content} = [
122 first {
123 $_->vtag eq $params->{vtag}
124 } @{$resp->content}
125 ];
126 }
127  
128 my $sibling = Net::Riak::Object->new(
129 client => $self,
130 bucket => $object->bucket,
131 key => $object->key
132 );
133  
134 $sibling->_jsonize($object->_jsonize);
135  
136 $self->populate_object($sibling, $resp);
137  
138 $sibling;
139 }
140  
141 1;
142  
143 __END__