File Coverage

blib/lib/Mojito/Model/Doc/Mongo.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 3     3   17 use strictures 1;
  3         19  
  3         71  
2             package Mojito::Model::Doc::Mongo;
3             {
4             $Mojito::Model::Doc::Mongo::VERSION = '0.24';
5             }
6 3     3   431 use Moo;
  3         6  
  3         55  
7 3     3   6904 use MongoDB::OID;
  0            
  0            
8             use Data::Dumper::Concise;
9              
10             with('Mojito::Role::DB::Mongo');
11              
12             =head1 Methods
13              
14             =head2 get_most_recent_docs
15              
16             Get the documents sorted by date in reverse chrono order.
17             Returns a cursor to them.
18              
19             =cut
20              
21             sub get_most_recent_docs {
22             my $self = shift;
23             return $self->collection->find->sort( { last_modified => -1 } );
24             }
25              
26             =head2 get_feed_docs
27              
28             Get the documents for a particular feed sorted by date in reverse chrono order.
29             Returns a cursor to them.
30              
31             =cut
32              
33             sub get_feed_docs {
34             my ($self, $feed) = @_;
35             return $self->collection->find({feeds => $feed})->sort( { last_modified => -1 } );
36             }
37              
38             =head2 get_collections
39              
40             Get the collections by name sorted by date in reverse chrono order.
41             Returns a cursor to them.
42              
43             =cut
44              
45             sub get_collections {
46             my $self = shift;
47             $self->clear_collection_name;
48             $self->clear_collection;
49             $self->collection_name('collection');
50             return $self->collection->find->sort( { last_modified => -1 } );
51             }
52              
53             =head2 get_collection_pages
54              
55             Get the pages belonging to a particular collection.
56             NOTE: We get the list of page ids from the collection collected_page_ids value.
57             Then we find all documents corresponding to those ids.
58              
59             Return an (collection_name, ArrayRef of pages);
60             =cut
61              
62             sub get_collection_pages {
63             my ($self, $collection_id) = (shift, shift);
64              
65              
66             $self->clear_collection_name;
67             $self->clear_collection;
68             $self->collection_name('collection');
69             my $oid = MongoDB::OID->new( value => $collection_id );
70             my $collection = $self->collection->find_one( { _id => $oid } );
71             my $page_ids = $collection->{collected_page_ids};
72             my @page_oids = map { MongoDB::OID->new( value => $_ ) } @{$page_ids};
73            
74             # Change to notes collection
75             $self->clear_collection_name;
76             $self->clear_collection;
77             $self->collection_name('notes');
78             my @pages;
79             foreach my $oid (@page_oids) {
80             my $page = $self->collection->find_one( { _id => $oid } );
81             push @pages, $page;
82             }
83             return ($collection->{collection_name}, \@pages);
84             }
85              
86             sub get_docs_for_month {
87             my ($self, $month, $year) = @_;
88              
89             my %monthly_data = ();
90             my $start_epoch = DateTime->new(
91             year => $year,
92             month => $month,
93             day => 1,
94             hour => 0,
95             minute => 0,
96             second => 0,
97             )->epoch;
98             my $next_month = ($month == 12) ? 1 : $month + 1;
99             my $year_of_next_month = ($month == 12) ? $year + 1 : $year;
100             my $end_epoch = DateTime->new(
101             year => $year_of_next_month,
102             month => $next_month,
103             day => 1,
104             hour => 0,
105             minute => 0,
106             second => 0,
107             )->epoch;
108             my $docs = $self->collection->find(
109             { 'last_modified' => { '$gte' => $start_epoch, '$lt' => $end_epoch } });
110             while (my $doc = $docs->next) {
111             my $day = DateTime->from_epoch(epoch => $doc->{last_modified})->day;
112             push @{ $monthly_data{$day} },
113             { id => $doc->{_id}->value, title => $doc->{title} };
114             }
115             return %monthly_data;
116             }
117              
118             1
119              
120             __END__
121             my $oid = MongoDB::OID->new( value => $id );
122             return $self->collection->find_one( { _id => $oid } );