File Coverage

blib/lib/Dancer/Plugin/Mongoose.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             # ABSTRACT: Mongoose interface for Dancer applications
2              
3             package Dancer::Plugin::Mongoose;
4             BEGIN {
5 2     2   400389 $Dancer::Plugin::Mongoose::VERSION = '0.00002';
6             }
7              
8 2     2   19 use strict;
  2         4  
  2         62  
9 2     2   11 use warnings;
  2         5  
  2         66  
10 2     2   2486 use Dancer::Plugin;
  2         3163  
  2         164  
11 2     2   195753 use Mongoose;
  0            
  0            
12              
13              
14             my $schemas = {};
15              
16             register schema => sub {
17             my $name = shift;
18             my $cfg = plugin_setting;
19              
20             if (not defined $name) {
21             ($name) = keys %$cfg or die "No schemas are configured";
22             }
23              
24             return $schemas->{$name} if $schemas->{$name};
25              
26             my $options = $cfg->{$name} or die "The schema $name is not configured";
27              
28             my $class = $options->{class};
29              
30             if ($class) {
31            
32             $class =~ s/-/::/g;
33            
34             eval "use $class";
35            
36             if ( my $err = $@ ) {
37             die "error while loading $class : $err";
38             }
39            
40             $schemas->{$name} = $class
41             }
42            
43             # explicitly set db with options
44             Mongoose->db(
45             ref $options->{database} ?
46             %{$options->{database}} : $options->{database}
47             ) if defined $options->{database};
48              
49             return $schemas->{$name};
50             };
51              
52             register_plugin;
53              
54             1;
55              
56             __END__
57             =pod
58              
59             =head1 NAME
60              
61             Dancer::Plugin::Mongoose - Mongoose interface for Dancer applications
62              
63             =head1 VERSION
64              
65             version 0.00002
66              
67             =head1 SYNOPSIS
68              
69             # Dancer Code File
70             use Dancer;
71             use Dancer::Plugin::Mongoose;
72              
73             get '/profile/:id' => sub {
74             my $user = schema->users->find_one(params->{id});
75            
76             # or explicitly ask for a schema by name:
77             $user = schema('foo')->users->find_one(params->{id});
78             template user_profile => { user => $user };
79             };
80              
81             dance;
82              
83             # Dancer Configuration File (minimumal settings)
84             plugins:
85             DBIC:
86             foo:
87             class: "Foo::Bar"
88              
89             Database connection details are read from your Dancer application config - see
90             below.
91              
92             =head1 DESCRIPTION
93              
94             This plugin provides an easy way to obtain L<Mongoose> derived class instances
95             via the the function schema(), which it automatically imports. You just need to
96             point to a class in your L<Dancer> configuration file.
97              
98             =head1 CONFIGURATION
99              
100             Connection details will be grabbed from your L<Dancer> config file.
101             For example:
102              
103             plugins:
104             Mongoose:
105             foo:
106             class: Foo
107             database:
108             db_name: "one"
109             host: "mongodb://localhost:27017"
110             bar:
111             class: Foo::Bar
112             database: "two"
113             baz:
114             class: Foo::Baz
115             database:
116             db_name: "three"
117             host: "mongodb://elsewhere:27017"
118             query_timeout: 60
119              
120             Each schema configuration *must* have a class option. If the host option is
121             omitted it is assumed that it is hard-coded in the class or that localhost
122             should be used. The host option should be the L<MongoDB> driver connection
123             string.
124              
125             The class option provided should be a proper Perl package name that
126             Dancer::Plugin::Mongoose will use as a Mongoose derived class.
127              
128             =head1 AUTHOR
129              
130             Al Newkirk <awncorp@cpan.org>
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2010 by awncorp.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut
140