File Coverage

blib/lib/Data/Riak/Fast/Result.pm
Criterion Covered Total %
statement 15 47 31.9
branch 0 12 0.0
condition 0 15 0.0
subroutine 5 12 41.6
pod 1 7 14.2
total 21 93 22.5


line stmt bran cond sub pod time code
1             package Data::Riak::Fast::Result;
2              
3 22     22   866 use Mouse;
  22         28656  
  22         266  
4              
5 22     22   20033 use Data::Riak::Fast::Link;
  22         72  
  22         919  
6              
7 22     22   930 use URI;
  22         2439  
  22         502  
8 22     22   23331 use HTTP::Headers::ActionPack;
  22         87998  
  22         22562  
9              
10             with 'Data::Riak::Fast::Role::HasRiak';
11              
12             has bucket => (
13             is => 'ro',
14             isa => 'Data::Riak::Fast::Bucket',
15             lazy => 1,
16             default => sub {
17             my $self = shift;
18             $self->riak->bucket( $self->bucket_name )
19             }
20             );
21              
22             has location => (
23             is => 'ro',
24             isa => 'URI',
25             lazy => 1,
26             clearer => '_clear_location',
27             default => sub {
28             my $self = shift;
29             return $self->http_message->request->uri if $self->http_message->can('request');
30             return URI->new( $self->http_message->header('location') || die "Cannot determine location from " . $self->http_message );
31             }
32             );
33              
34             has bucket_name => (
35             is => 'ro',
36             isa => 'Str',
37             lazy => 1,
38             default => sub {
39             my $self = shift;
40             my @uri_parts = split /\//, $self->location->path;
41             return $uri_parts[$#uri_parts - 2];
42             }
43             );
44              
45             has key => (
46             is => 'ro',
47             isa => 'Str',
48             lazy => 1,
49             default => sub {
50             my $self = shift;
51             my @uri_parts = split /\//, $self->location->path;
52             return $uri_parts[$#uri_parts];
53             }
54             );
55              
56             has links => (
57             is => 'rw',
58             isa => 'ArrayRef[Data::Riak::Fast::Link]',
59             lazy => 1,
60             clearer => '_clear_links',
61             default => sub {
62             my $self = shift;
63             my $links = $self->http_message->header('link');
64             return [] unless $links;
65             return [ map {
66             Data::Riak::Fast::Link->from_link_header( $_ )
67             } $links->iterable ];
68             }
69             );
70              
71             has http_message => (
72             is => 'rw',
73             isa => 'HTTP::Message',
74             required => 1,
75             handles => {
76             'status_code' => 'code',
77             'value' => 'content',
78             'header' => 'header',
79             'headers' => 'headers',
80             # curried delegation
81             'etag' => [ 'header' => 'etag' ],
82             'content_type' => [ 'header' => 'content-type' ],
83             'vector_clock' => [ 'header' => 'x-riak-vclock' ],
84             'last_modified' => [ 'header' => 'last_modified' ]
85             }
86             );
87              
88             sub BUILD {
89 0     0 1   my $self = shift;
90 0           HTTP::Headers::ActionPack->new->inflate( $self->http_message->headers );
91             }
92              
93             sub create_link {
94 0     0 0   my ($self, %opts) = @_;
95 0 0         return Data::Riak::Fast::Link->new({
96             bucket => $self->bucket_name,
97             key => $self->key,
98             riaktag => $opts{riaktag},
99             (exists $opts{params} ? (params => $opts{params}) : ())
100             });
101             }
102              
103             # if it's been changed on the server, discard those changes and update the object
104             sub sync {
105 0     0 0   $_[0] = $_[0]->bucket->get($_[0]->key)
106             }
107              
108             # if it's been changed locally, save those changes to the server
109             sub save {
110 0     0 0   my $self = shift;
111 0           return $self->bucket->add($self->key, $self->value, { links => $self->links });
112             }
113              
114             sub linkwalk {
115 0     0 0   my ($self, $params) = @_;
116 0 0         return unless $params;
117 0           return $self->riak->linkwalk({
118             bucket => $self->bucket_name,
119             object => $self->key,
120             params => $params
121             });
122             }
123              
124             sub add_link {
125 0     0 0   my ($self, $link) = @_;
126 0 0         return unless $link;
127 0           my $links = $self->links;
128 0           push @{$links}, $link;
  0            
129 0           $self->links($links);
130 0           return $self;
131             }
132              
133             sub remove_link {
134 0     0 0   my ($self, $args) = @_;
135 0           my $key = $args->{key};
136 0           my $riaktag = $args->{riaktag};
137 0           my $bucket = $args->{bucket};
138 0           my $links = $self->links;
139 0           my $new_links;
140 0           foreach my $link (@{$links}) {
  0            
141 0 0 0       next if($bucket && ($bucket eq $link->bucket));
142 0 0 0       next if($key && $link->has_key && ($key eq $link->key));
      0        
143 0 0 0       next if($riaktag && $link->has_riaktag && ($riaktag eq $link->riaktag));
      0        
144 0           push @{$new_links}, $link;
  0            
145             }
146 0           $self->links($new_links);
147 0           return $self;
148             }
149              
150              
151             __PACKAGE__->meta->make_immutable;
152 22     22   198 no Mouse;
  22         50  
  22         349  
153              
154             1;
155              
156             __END__