File Coverage

blib/lib/LittleORM.pm
Criterion Covered Total %
statement 36 68 52.9
branch 0 12 0.0
condition n/a
subroutine 12 15 80.0
pod 0 2 0.0
total 48 97 49.4


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 1     1   15321 use strict;
  1         3  
  1         58  
4              
5              
6             package LittleORM::Meta::Role;
7              
8 1     1   378 use Moose::Role;
  1         350685  
  1         5  
9              
10             has 'found_orm' => ( is => 'rw', isa => 'Bool', default => 0 );
11              
12 1     1   4873 no Moose::Role;
  1         8  
  1         4  
13              
14             package LittleORM;
15              
16             our $VERSION = '0.24';
17              
18             =head1 NAME
19              
20             LittleORM - ORM for Perl with Moose.
21              
22             =head1 VERSION
23              
24             Version 0.24
25              
26             =cut
27              
28             =head1 SYNOPSIS
29              
30             Please refer to L<LittleORM::Tutorial> for thorough description of LittleORM.
31              
32             =cut
33              
34             =head1 AUTHOR
35              
36             Eugene Kuzin, C<< <eugenek at 45-98.org> >>, JID: C<< <gnudist at jabber.ru> >>
37             with significant contributions by
38             Kain Winterheart, C<< <kain.winterheart at gmail.com> >>
39              
40              
41             =head1 BUGS
42              
43             Please report any bugs or feature requests to C<bug-littleorm at
44             rt.cpan.org>, or through the web interface at
45             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LittleORM>. I will
46             be notified, and then you'll automatically be notified of progress on
47             your bug as I make changes.
48              
49             =head1 SUPPORT
50              
51             You can find documentation for this module with the perldoc command.
52              
53             perldoc LittleORM
54              
55              
56             You can also look for information at:
57              
58             =over 4
59              
60             =item * Project home:
61              
62             L<https://github.com/gnudist/littleorm>
63              
64             =item * RT: CPAN's request tracker (report bugs here)
65              
66             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LittleORM>
67              
68             =item * AnnoCPAN: Annotated CPAN documentation
69              
70             L<http://annocpan.org/dist/LittleORM>
71              
72             =item * CPAN Ratings
73              
74             L<http://cpanratings.perl.org/d/LittleORM>
75              
76             =item * Search CPAN
77              
78             L<http://search.cpan.org/dist/LittleORM/>
79              
80             =back
81              
82              
83             =head1 ACKNOWLEDGEMENTS
84              
85              
86             =head1 LICENSE AND COPYRIGHT
87              
88             Copyright 2013 Eugene Kuzin.
89              
90             This program is free software; you can redistribute it and/or modify it
91             under the terms of the the Artistic License (2.0). You may obtain a
92             copy of the full license at:
93              
94             L<http://www.perlfoundation.org/artistic_license_2_0>
95              
96             Any use, modification, and distribution of the Standard or Modified
97             Versions is governed by this Artistic License. By using, modifying or
98             distributing the Package, you accept this license. Do not use, modify,
99             or distribute the Package, if you do not accept this license.
100              
101             If your Modified Version has been derived from a Modified Version made
102             by someone other than you, you are nevertheless required to ensure that
103             your Modified Version complies with the requirements of this license.
104              
105             This license does not grant you the right to use any trademark, service
106             mark, tradename, or logo of the Copyright Holder.
107              
108             This license includes the non-exclusive, worldwide, free-of-charge
109             patent license to make, have made, use, offer to sell, sell, import and
110             otherwise transfer the Package with respect to any patent claims
111             licensable by the Copyright Holder that are necessarily infringed by the
112             Package. If you institute patent litigation (including a cross-claim or
113             counterclaim) against any party alleging that the Package constitutes
114             direct or contributory patent infringement, then this Artistic License
115             to you shall terminate on the date that such litigation is filed.
116              
117             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
118             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
119             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
120             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
121             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
122             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
123             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
124             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125              
126              
127             =cut
128              
129              
130 1     1   161 use Moose;
  1         1  
  1         5  
131 1     1   5767 use Moose::Exporter;
  1         3  
  1         4  
132 1     1   78 use Moose::Util::MetaRole;
  1         2  
  1         26  
133              
134 1     1   564 use Carp::Assert 'assert';
  1         1095  
  1         6  
135 1     1   112 use Scalar::Util ();
  1         1  
  1         356  
136              
137             Moose::Exporter -> setup_import_methods( with_meta => [ 'has_field' ],
138             also => 'Moose' );
139             sub init_meta
140             {
141 1     1 0 106 my ( $class, %args ) = @_;
142            
143 1         7 Moose -> init_meta( %args );
144            
145 1         3993 return &Moose::Util::MetaRole::apply_metaroles(
146             for => $args{ 'for_class' },
147             class_metaroles => {
148             class => [ 'LittleORM::Meta::Role' ]
149             }
150             );
151             }
152              
153             sub has_field
154             {
155 0     0 0   my ( $meta, $name, %args ) = @_;
156            
157 0 0         unless( $meta -> found_orm() )
158             {
159 0           my @isa = $meta -> linearized_isa();
160 0           my $ok = 0;
161            
162 0           foreach my $class ( @isa )
163             {
164 0 0         if( $class -> isa( 'LittleORM::Model' ) )
165             {
166 0           $meta -> found_orm( $ok = 1 );
167            
168 0           last;
169             }
170             }
171            
172 0           assert( $ok, sprintf( 'Class "%s" must extend LittleORM::Model', $isa[ 0 ] ) );
173             }
174              
175 0           return &__has_field_no_check( $meta, $name, %args );
176             }
177            
178             sub __has_field_no_check
179             {
180 0     0     my ( $meta, $name, %args ) = @_;
181              
182 0 0         if( ref( $args{ 'traits' } ) eq 'ARRAY' )
183             {
184 0           push @{ $args{ 'traits' } }, 'LittleORM::Meta::Trait';
  0            
185             } else
186             {
187 0           $args{ 'traits' } = [ 'LittleORM::Meta::Trait' ];
188             }
189            
190 0 0         unless( ref( $args{ 'description' } ) eq 'HASH' )
191             {
192 0           $args{ 'description' } = {};
193             }
194              
195 0           my $attr = undef;
196              
197 0 0         unless( $args{ 'description' } -> { 'ignore' } )
198             {
199 0           $args{ 'is' } = 'rw';
200 0           $args{ 'lazy' } = 1;
201              
202 0           foreach my $key ( 'builder', 'default' )
203             {
204 0           assert( not( exists $args{ $key } ), sprintf( 'There is a problem with attribute "%s": you should not use "%s" in LittleORM attribute. Consider using "description => { coerce_from => sub{ ... } }" instead, or just add "description => { ignore => 1 }".', $name, $key ) );
205             }
206              
207             $args{ 'default' } = sub
208             {
209 0     0     my $self = shift;
210              
211 0 0         if( $attr -> isa( 'Moose::Meta::Role::Attribute' ) )
212             {
213 0           $attr = $self -> meta() -> find_attribute_by_name( $name );
214             }
215              
216 0           return $self -> __lazy_build_value( $attr );
217 0           };
218 0           $args{ 'description' } -> { '__orm_initialized_attr_has_field_' } = 'yes';
219             }
220              
221 0           $attr = $meta -> add_attribute( $name, %args );
222              
223 0           Scalar::Util::weaken( $attr );
224              
225 0           return 1;
226             }
227              
228 1     1   6 no Moose::Util::MetaRole;
  1         2  
  1         35  
229 1     1   7 no Moose::Exporter;
  1         1  
  1         36  
230 1     1   5 no Moose;
  1         1  
  1         9  
231              
232             -1;
233