| 1 | package Data::Riak::Result::WithLocation; | ||
| 2 | { | ||
| 3 | $Data::Riak::Result::WithLocation::VERSION = '2.0'; | ||
| 4 | } | ||
| 5 | # ABSTRACT: Results with a Location | ||
| 6 | |||
| 7 | use Moose::Role; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | |||
| 11 | has location => ( | ||
| 12 | is => 'ro', | ||
| 13 | isa => 'URI', | ||
| 14 | required => 1, | ||
| 15 | ); | ||
| 16 | |||
| 17 | |||
| 18 | has bucket => ( | ||
| 19 | is => 'ro', | ||
| 20 | does => 'Data::Riak::Role::Bucket', | ||
| 21 | lazy => 1, | ||
| 22 | default => sub { | ||
| 23 | my $self = shift; | ||
| 24 | $self->riak->bucket( $self->bucket_name ) | ||
| 25 | } | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | has bucket_name => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'Str', | ||
| 32 | lazy => 1, | ||
| 33 | default => sub { | ||
| 34 | my $self = shift; | ||
| 35 | my @uri_parts = split /\//, $self->location->path; | ||
| 36 | return $uri_parts[$#uri_parts - 2]; | ||
| 37 | } | ||
| 38 | ); | ||
| 39 | |||
| 40 | |||
| 41 | has key => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => 'Str', | ||
| 44 | lazy => 1, | ||
| 45 | default => sub { | ||
| 46 | my $self = shift; | ||
| 47 | my @uri_parts = split /\//, $self->location->path; | ||
| 48 | return $uri_parts[$#uri_parts]; | ||
| 49 | } | ||
| 50 | ); | ||
| 51 | |||
| 52 | sub BUILD {} | ||
| 53 | after BUILD => sub { | ||
| 54 | my ($self) = @_; | ||
| 55 | $self->bucket_name; | ||
| 56 | $self->key; | ||
| 57 | }; | ||
| 58 | |||
| 59 | |||
| 60 | # if it's been changed on the server, discard those changes and update the | ||
| 61 | # object | ||
| 62 | my %warned_for; | ||
| 63 | sub sync { | ||
| 64 | my ($self, %opts) = @_; | ||
| 65 | |||
| 66 | my $new_result = $self->bucket->get( $self->key, \%opts ); | ||
| 67 | if (!defined wantarray) { | ||
| 68 | my $caller = caller; | ||
| 69 | warn "${caller} is using the deprecated ->sync in void context" | ||
| 70 | unless $warned_for{$caller}; | ||
| 71 | $_[0] = $new_result; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $new_result; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | # if it's been changed locally by cloning, save those changes to the server | ||
| 79 | sub save { | ||
| 80 | my ($self, %opts) = @_; | ||
| 81 | return $self->bucket->add( | ||
| 82 | $self->key, (exists $opts{new_value} ? $opts{new_value} : $self->value), | ||
| 83 | { | ||
| 84 | links => (exists $opts{new_links} ? $opts{new_links} : $self->links), | ||
| 85 | return_body => 1, | ||
| 86 | vector_clock => $self->vector_clock, | ||
| 87 | (exists $opts{cb} ? (cb => $opts{cb}) : ()), | ||
| 88 | (exists $opts{error_cb} ? (error_cb => $opts{error_cb}) : ()), | ||
| 89 | }, | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub save_unless_modified { | ||
| 95 | my ($self, %opts) = @_; | ||
| 96 | return $self->bucket->add( | ||
| 97 | $self->key, (exists $opts{new_value} ? $opts{new_value} : $self->value), | ||
| 98 | { | ||
| 99 | links => (exists $opts{new_links} ? $opts{new_links} : $self->links), | ||
| 100 | return_body => 1, | ||
| 101 | vector_clock => $self->vector_clock, | ||
| 102 | if_unmodified_since => $self->last_modified . '', | ||
| 103 | if_match => $self->etag, | ||
| 104 | (exists $opts{cb} ? (cb => $opts{cb}) : ()), | ||
| 105 | (exists $opts{error_cb} ? (error_cb => $opts{error_cb}) : ()), | ||
| 106 | }, | ||
| 107 | ); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub linkwalk { | ||
| 112 | my ($self, $params, $cb, $error_cb) = @_; | ||
| 113 | return $self->riak->linkwalk({ | ||
| 114 | bucket => $self->bucket_name, | ||
| 115 | object => $self->key, | ||
| 116 | params => $params, | ||
| 117 | cb => $cb, | ||
| 118 | error_cb => $error_cb, | ||
| 119 | }); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | __END__ |