| 1 | package Data::CouchDB::Role::VersionedData; | ||
| 2 | use strict; use warnings; | ||
| 3 | |||
| 4 | our $VERSION = '0.14'; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Data::CouchDB::Role::VersionedData | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Handles pulling in symbol-related data versioned by date. | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | use Net::SSL; | ||
| 17 | use JSON qw(to_json from_json); | ||
| 18 | use List::Util qw(min max); | ||
| 19 | use Moose::Role; | ||
| 20 | use Carp; | ||
| 21 | use URL::Encode qw(:all); | ||
| 22 | use Try::Tiny; | ||
| 23 | |||
| 24 | use Date::Utility; | ||
| 25 | use Data::CouchDB::Handler; | ||
| 26 | |||
| 27 | requires 'symbol', '_data_location', '_document_content'; | ||
| 28 | |||
| 29 | =head1 ATTRIBUTES | ||
| 30 | |||
| 31 | =head2 for_date | ||
| 32 | |||
| 33 | The date for which we wish data | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | has for_date => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'Maybe[Date::Utility]', | ||
| 40 | default => undef, | ||
| 41 | ); | ||
| 42 | |||
| 43 | =head2 current_document_id | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | has current_document_id => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => 'Str', | ||
| 50 | lazy_build => 1, | ||
| 51 | ); | ||
| 52 | |||
| 53 | sub _build_current_document_id { | ||
| 54 | my $self = shift; | ||
| 55 | return $self->symbol; | ||
| 56 | } | ||
| 57 | |||
| 58 | =head2 document | ||
| 59 | |||
| 60 | The CouchDB document that this object is tied to. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | has document => ( | ||
| 65 | is => 'rw', | ||
| 66 | lazy_build => 1, | ||
| 67 | ); | ||
| 68 | |||
| 69 | sub _build_document { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | my $document = $self->_couchdb->document($self->current_document_id); | ||
| 73 | if ($self->for_date and $self->for_date->datetime_iso8601 lt $document->{date}) { | ||
| 74 | my $params = { | ||
| 75 | startkey => [$self->symbol, $self->for_date->datetime_iso8601], | ||
| 76 | endkey => [$self->symbol], | ||
| 77 | descending => 1, | ||
| 78 | limit => 1, | ||
| 79 | }; | ||
| 80 | |||
| 81 | my $document_id = $self->_couchdb->view('by_date', $params)->[0]; | ||
| 82 | if ($document_id) { | ||
| 83 | $document = $self->_couchdb->document($document_id); | ||
| 84 | } else { | ||
| 85 | $params = { | ||
| 86 | startkey => [$self->symbol], | ||
| 87 | endkey => [$self->symbol, {}], | ||
| 88 | limit => 1, | ||
| 89 | }; | ||
| 90 | |||
| 91 | $document_id = $self->_couchdb->view('by_date', $params)->[0]; | ||
| 92 | if ($document_id) { | ||
| 93 | $document = $self->_couchdb->document($document_id); | ||
| 94 | } | ||
| 95 | |||
| 96 | $document //= {}; | ||
| 97 | $document->{date} = $self->for_date->datetime_iso8601; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return $document; | ||
| 102 | } | ||
| 103 | |||
| 104 | has _couchdb => ( | ||
| 105 | is => 'ro', | ||
| 106 | isa => 'Data::CouchDB', | ||
| 107 | lazy_build => 1, | ||
| 108 | ); | ||
| 109 | |||
| 110 | sub _build__couchdb { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | my $handler = Data::CouchDB::Handler->new(); | ||
| 114 | if (exists $ENV{COUCHDB_DATABASES}) { | ||
| 115 | $handler->couchdb_databases($ENV{COUCHDB_DATABASES}); | ||
| 116 | } else { | ||
| 117 | $handler->couchdb_databases({ | ||
| 118 | $self->_data_location => $self->_data_location, | ||
| 119 | }); | ||
| 120 | } | ||
| 121 | |||
| 122 | return $handler->couchdb($self->_data_location); | ||
| 123 | } | ||
| 124 | |||
| 125 | =head2 add_to_history | ||
| 126 | |||
| 127 | Add the current live document to the searchable history | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub add_to_history { | ||
| 132 | my ($self) = @_; | ||
| 133 | |||
| 134 | # If its already a historical document hopefully its id will look like a uuid. | ||
| 135 | if ($self->_looks_like_uuid($self->document->{_id})) { | ||
| 136 | croak 'Saving historical document not permitted.'; | ||
| 137 | } | ||
| 138 | |||
| 139 | delete $self->document->{_rev}; | ||
| 140 | my $historical_doc = $self->_couchdb->create_document(); | ||
| 141 | return $self->_couchdb->document($historical_doc, $self->document); | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 save | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub save { | ||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | if (not $self->_couchdb->document_present($self->current_document_id)) { | ||
| 152 | $self->_couchdb->create_document($self->current_document_id); | ||
| 153 | } else { | ||
| 154 | $self->add_to_history; | ||
| 155 | } | ||
| 156 | |||
| 157 | my $new_document = $self->_document_content; | ||
| 158 | $self->document($new_document); | ||
| 159 | return $self->_couchdb->document($self->current_document_id, $new_document); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _looks_like_uuid { | ||
| 163 | my ($self, $candidate) = @_; | ||
| 164 | return ($candidate and $candidate =~ /^\w{32}$/); | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; |