File Coverage

blib/lib/KiokuX/Model.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package KiokuX::Model;
2 1     1   28161 use Moose;
  0            
  0            
3             use MooseX::StrictConstructor;
4              
5             use Carp qw(croak);
6              
7             use KiokuDB;
8              
9             use namespace::clean -except => 'meta';
10              
11             our $VERSION = "0.02";
12              
13             sub BUILD {
14             my $self = shift;
15              
16             $self->directory;
17             }
18              
19             has dsn => (
20             isa => "Str",
21             is => "ro",
22             );
23              
24             has extra_args => (
25             isa => "HashRef|ArrayRef",
26             is => "ro",
27             predicate => "has_extra_args",
28             );
29              
30             has typemap => (
31             isa => "KiokuDB::TypeMap",
32             is => "ro",
33             predicate => "has_typemap",
34             );
35              
36             has directory => (
37             isa => "KiokuDB",
38             lazy_build => 1,
39             handles => 'KiokuDB::Role::API',
40             );
41              
42             sub _build_directory {
43             my $self = shift;
44              
45             KiokuDB->connect(@{ $self->_connect_args });
46             }
47              
48             has _connect_args => (
49             isa => "ArrayRef",
50             is => "ro",
51             lazy_build => 1,
52             );
53              
54             sub _build__connect_args {
55             my $self = shift;
56              
57             my @args = ( $self->dsn || croak "dsn is required" );
58              
59             if ( $self->has_typemap ) {
60             push @args, typemap => $self->typemap;
61             }
62              
63             if ( $self->has_extra_args ) {
64             my $extra = $self->extra_args;
65              
66             if ( ref($extra) eq 'ARRAY' ) {
67             push @args, @$extra;
68             } else {
69             push @args, %$extra;
70             }
71             }
72              
73             \@args;
74             }
75              
76             sub connect {
77             my ( $class, $dsn, @args ) = @_;
78              
79             $class->new( dsn => $dsn, extra_args => \@args );
80             }
81              
82             __PACKAGE__->meta->make_immutable;
83              
84             __PACKAGE__
85              
86             __END__
87              
88             =pod
89              
90             =head1 NAME
91              
92             KiokuX::Model - A simple application specific wrapper for L<KiokuDB>.
93              
94             =head1 SYNOPSIS
95              
96             # start with the base class:
97              
98             KiokuX::Model->new( dsn => "bdb:dir=/var/myapp/db" );
99              
100              
101              
102             # later you can add convenience methods by subclassing:
103              
104             package MyApp::DB;
105             use Moose;
106              
107             extends qw(KiokuX::Model);
108              
109             sub add_user {
110             my ( $self, @args ) = @_;
111              
112             my $user = MyApp::User->new(@args);
113              
114             $self->txn_do(sub {
115             $self->insert($user);
116             });
117              
118             return $user;
119             }
120              
121              
122             # Then just use it like this:
123              
124             MyApp::DB->new( dsn => "bdb:dir=/var/myapp/db" );
125              
126             # or automatically using e.g. L<Catalyst::Model::KiokuDB>:
127              
128             $c->model("kiokudb");
129              
130             =head1 DESCRIPTION
131              
132             This base class makes it easy to create L<KiokuDB> database instances in your
133             application. It provides a standard way to instantiate and use a L<KiokuDB>
134             object in your apps.
135              
136             As your app grows you can subclass it and provide additional convenience
137             methods, without changing the structure of the code, but simply swapping your
138             subclass for L<KiokuX::Model> in e.g. L<Catalyst::Model::KiokuDB> or whatever
139             you use to glue it in.
140              
141             =head1 ATTRIBUTES
142              
143             =over 4
144              
145             =item directory
146              
147             The instantiated directory.
148              
149             Created using the other attributes at C<BUILD> time.
150              
151             This attribute has delegations set up for all the methods of the L<KiokuDB>
152             class.
153              
154             =item dsn
155              
156             e.g. C<bdb:dir=root/db>. See L<KiokuDB/connect>.
157              
158             =item extra_args
159              
160             Additional arguments to pass to C<connect>.
161              
162             Can be a hash reference or an array reference.
163              
164             =item typemap
165              
166             An optional custom typemap to add. See L<KiokuDB::Typemap> and
167             L<KiokuDB/typemap>.
168              
169             =back
170              
171             =head1 SEE ALSO
172              
173             L<KiokuDB>, L<KiokuDB::Role::API>, L<Catalyst::Model::KiokuDB>
174              
175             =head1 VERSION CONTROL
176              
177             KiokuDB is maintained using Git. Information about the repository is available
178             on L<http://www.iinteractive.com/kiokudb/>
179              
180             =head1 AUTHOR
181              
182             Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
183              
184             =head1 COPYRIGHT
185              
186             Copyright (c) 2009 Yuval Kogman, Infinity Interactive. All
187             rights reserved This program is free software; you can redistribute
188             it and/or modify it under the same terms as Perl itself.
189              
190             =