File Coverage

blib/lib/CouchDB/Client/DesignDoc.pm
Criterion Covered Total %
statement 15 52 28.8
branch 0 18 0.0
condition 0 8 0.0
subroutine 5 10 50.0
pod 5 5 100.0
total 25 93 26.8


line stmt bran cond sub pod time code
1              
2             package CouchDB::Client::DesignDoc;
3              
4 3     3   1594 use strict;
  3         8  
  3         128  
5 3     3   17 use warnings;
  3         6  
  3         154  
6              
7             our $VERSION = $CouchDB::Client::VERSION;
8 3     3   18 use base qw(CouchDB::Client::Doc);
  3         5  
  3         397  
9 3     3   18 use URI::Escape qw(uri_escape_utf8);
  3         5  
  3         181  
10              
11 3     3   17 use Carp qw(confess);
  3         18  
  3         3423  
12              
13             sub new {
14 0     0 1   my $class = shift;
15 0 0         my %opt = @_ == 1 ? %{$_[0]} : @_;
  0            
16              
17 0           my $self = $class->SUPER::new(\%opt);
18 0 0         confess "Design doc ID must start with '_design/'" unless $self->{id} =~ m{^_design/};
19 0   0       $self->{data}->{language} ||= 'javascript';
20 0           return bless $self, $class;
21             }
22              
23 0 0   0 1   sub views { @_ == 2 ? $_[0]->{data}->{views} = $_[1] : $_[0]->{data}->{views}; }
24              
25              
26             sub contentForSubmit {
27 0     0 1   my $self = shift;
28 0           my $content = $self->SUPER::contentForSubmit();
29 0           delete $content->{attachments};
30 0           return $content;
31             }
32              
33             sub listViews {
34 0     0 1   my $self = shift;
35 0           return keys %{$self->data->{views}};
  0            
36             }
37              
38             sub queryView {
39 0     0 1   my $self = shift;
40 0           my $view = shift;
41 0           my %args = @_;
42              
43 0 0         confess("No such view: '$view'") unless exists $self->views->{$view};
44 0           my $sn = $self->id;
45 0           $sn =~ s{^_design/}{};
46 0           $sn = uri_escape_utf8($sn);
47              
48             # The uri path for views changed and the parameter "count" has been renamed "limit"
49             # between v0.8 and v0.9 and above. (issue #48407 and #49759 in RT)
50 0           my ($M,$m,undef) = split(/\./,$self->{db}->{client}->serverInfo()->{version});
51              
52 0           my $vp;
53 0 0 0       if ($M > 0 || ($M == 0 && $m >= 9)) {
      0        
54 0           $vp = "/_design/$sn/_view/$view";
55 0 0         if (defined($args{count})) {
56 0           $args{limit} = $args{count};
57 0           delete $args{count};
58             }
59             }
60             else {
61 0           $vp = "/_view/$sn/$view";
62 0 0         if (defined($args{limit})) {
63 0           $args{count} = $args{limit};
64 0           delete $args{limit};
65             }
66             }
67              
68 0 0         my $qs = %args ? $self->{db}->argsToQuery(%args) : '';
69              
70 0           my $res = $self->{db}->{client}->req('GET', $self->{db}->uriName . $vp . $qs);
71 0 0         confess("Connection error: $res->{msg}") unless $res->{success};
72 0           return $res->{json};
73             }
74              
75             1;
76              
77             =pod
78              
79             =head1 NAME
80              
81             CouchDB::Client::DesignDoc - CouchDB::Client design documents (views)
82              
83             =head1 SYNOPSIS
84              
85             $dd->listViews;
86             # ...
87             my $res = $dd->queryView('all');
88              
89             =head1 DESCRIPTION
90              
91             This module represents design documents (containing views) in the CouchDB database.
92              
93             Design documents are basically documents that have some fields interpreted specifically
94             in CouchDB. Therefore, this is a subclass of C and has all of the
95             same functionality except that it will not save attachments.
96              
97             =head1 METHODS
98              
99             =over 8
100              
101             =item new
102              
103             Constructor. Same as its parent class but only accepts IDs that are valid for design
104             documents.
105              
106             =item views
107              
108             Read-write accessor for the views. It needs to be in the format that CouchDB expects.
109             Note that this only changes the views on the client side, you have to create/update
110             the object for it to be stored.
111              
112             =item contentForSubmit
113              
114             Same as its parent class but removes attachments.
115              
116             =item listViews
117              
118             Returns a list of all the views defined in this design document.
119              
120             =item queryView $VIEW_NAME, %ARGS?
121              
122             Takes the name of a view in this design document (an exception will be thrown if it isn't
123             there) and an optional hash of query arguments as supported by CouchDB (e.g. startkey,
124             descending, count, etc.) and returns the data structure that the server returns. It will
125             throw exceptions for connection errors too.
126              
127             The query parameters are expected to be expressed in a Perlish fashion. For instance if
128             one has a boolean value you should use Perl truth and it will work; likewise if you are
129             using multiply-valued keys then simply pass in an arrayref and it will be converted and
130             quoted properly.
131              
132             The data structure that is returned is a hashref that will contain C and
133             C keys, as well as a C field that contains an array ref being the
134             resultset.
135              
136             =back
137              
138             =head1 AUTHOR
139              
140             Robin Berjon,
141             Maverick Edwards, (current maintainer)
142              
143             =head1 BUGS
144              
145             Please report any bugs or feature requests to bug-couchdb-client at rt.cpan.org, or through the
146             web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CouchDB-Client.
147              
148             =head1 COPYRIGHT & LICENSE
149              
150             Copyright 2008 Robin Berjon, all rights reserved.
151              
152             This library is free software; you can redistribute it and/or modify it under the same terms as
153             Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may
154             have available.
155              
156             =cut