Mandel/Model.pm

filter: Statementstmt 91.2% 114 / 125 Branchbran 77.2% 34 / 44 Conditioncond 66.6% 8 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 24 / 24 Podpod 80.0% 4 / 5 totaltotal 85.6% 185 / 216 CC 45 SCAR 12.5
CC
45
Cov
86%
_add_field
7.4
names
5.8
num
5.8
CRAP 3.5
« andel.pm ↑ Summary Mandel/Iterator.pm »
1 package Mandel::Model;
2 27 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 27 ▶use Mojo::Loader 'load_class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use Mandel::Model::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 26 my $ANON = 1;
9  
10 7 ▶has collection_name => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
11 7 my $self = shift;
12 7 my $name = $self->name;
13  
14 7 ▶ return $name =~ /s$/ ? $name : $name . 's' if $name;
Branch: 2 branches
BranchTrueFalseTotal
if $name 7 0 7
$name =~ /s$/u ? : 1 6 7
15 0 confess "collection_name or name required in constructor";
16 26 };
17  
18 26 has collection_class => 'Mandel::Collection';
19  
20 10 ▶has document_class => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
21 10 my $self = shift;
22 10 ▶ my $name = ucfirst $self->name || 'AnonDoc';
Condition50%1/2
ucfirst $self->name || 'AnonDoc'
l!l
100
MC/DC0%0/1
ucfirst $self->name || 'AnonDoc'
ucfirst $self->name
Truth table50%1/2
A
A= ucfirst $self->name
Aresult
11
00
23 10 my $class = "Mandel::Document::__ANON_${ANON}__::$name"; # this might change
24  
25 5 ▶ eval <<" PACKAGE" or confess $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine model: called CC=1 SCAR=0.0
Subroutine model: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval qq[ package $class;\n use Mojo::Base "Mandel::Document";\n sub model { \$self }\n \$INC{"Mandel/Document/__ANON__$ANON.pm"} = "GENERATED";\n] or confess($@) 10 0 10
26 package $class;
27 use Mojo::Base "Mandel::Document";
28 sub model { \$self }
29 \$INC{"Mandel/Document/__ANON__$ANON.pm"} = "GENERATED";
30 PACKAGE
31  
32 10 $ANON++;
33 10 $class;
34 26 };
35  
36 28 has name => '';
37  
38 17 ▶sub field {
Subroutine field: called CC=5 SCAR=16.3
Pod documented
39 17 my ($self, $name, $meta) = @_;
40  
41 17 ▶ if ($meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta) 12 5 17
42 12 return $self->_add_field($name => $meta); # $name might be an array-ref
43 }
44  
45 5 ▶ for (@{$self->{fields} || []}) {
Condition50%1/2
$self->{'fields'} || []
l!l
50
MC/DC0%0/1
$self->{'fields'} || []
$self->{'fields'}
Truth table50%1/2
A
A= $self->{'fields'}
Aresult
11
00
46 15 ▶ return $_ if $name eq $_->name;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $_->name 5 10 15
47 }
48  
49 0 return;
50 }
51  
52 12 ▶sub _add_field {
Subroutine _add_field: called CC=7 SCAR=20.0
53 12 my ($self, $fields, $meta) = @_;
54 12 my $class = $self->document_class;
55  
56 # Compile fieldibutes
57 12 ▶ for my $name (@{ref $fields eq 'ARRAY' ? $fields : [$fields]}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 1 11 12
58 13 local $meta->{name} = $name;
59 13 my $field = Mandel::Model::Field->new($meta);
60 13 my $builder = $field->builder;
61 13 my $code = "";
62  
63 13 $code .= "package $class;\nsub $name {\n my \$raw = \$_[0]->data;\n";
64  
65 13 ▶ if ($builder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($builder) { } 2 11 13
66 2 $code
67 .= "return exists \$raw->{'$name'} ? (\$raw->{'$name'}) : (\$raw->{'$name'} = \$_[0]->\$builder) if \@_ == 1;\n";
68 }
69 else {
70 11 $code .= "return \$raw->{'$name'} if \@_ == 1;\n";
71 }
72  
73 13 $code .= "local \$_ = \$_[1];\n";
74 13 ▶ $code .= $self->_field_type($meta->{isa}) if $meta->{isa};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"isa"} 5 8 13
75 13 $code .= "\$_[0]->{dirty}{$name} = 1;";
76 13 $code .= "\$raw->{'$name'} = \$_;\n";
77 13 $code .= "return \$_[0];\n}";
78  
79 # We compile custom attribute code for speed
80 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 13 ▶ warn "-- Attribute $name in $class\n$code\n\n" if $ENV{MOJO_BASE_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJO_BASE_DEBUG"} 0 13 13
82 0 ▶ Carp::croak "Mandel::Document error: $@ ($code)" unless eval "$code;1";
Subroutine soup: called CC=2 SCAR=7.6
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine names: called CC=5 SCAR=17.6
Subroutine bar: called CC=2 SCAR=7.6
Subroutine baz: called CC=2 SCAR=7.6
Pod undocumented
Branch: 8 branches
BranchTrueFalseTotal
unless eval "$code;1" 0 13 13
if @_ == 1 4 2 6
if @_ == 1 1 2 3
Type::Tiny::XS::AUTO::TC1($_) ? : 2 0 2
Type::Tiny::XS::Str($_) ? : 1 0 1
Type::Tiny::XS::AUTO::TC1($_) or _failed_check(41, "ArrayRef[Str]", $_, "exception_class", "Error::TypeTiny::Assertion") 0 1 1
if @_ == 1 1 0 1
if @_ == 1 0 1 1
83  
84 13 push @{$self->{fields}}, $field;
85 }
86  
87 12 $self;
88 }
89  
90 4 ▶sub fields {
Subroutine fields: called CC=2 SCAR=6.9
Pod documented
91 4 ▶ @{$_[0]->{fields} || []};
Condition100%3/3
$_[0]{'fields'} || []
l!l
111
MC/DC0%0/1
$_[0]{'fields'} || []
$_[0]{'fields'}
Truth table50%1/2
A
A= $_[0]{'fields'}
Aresult
11
00
92 }
93  
94 8 ▶sub _field_type {
Subroutine _field_type: called CC=5 SCAR=16.1
95 7 my ($self, $type) = @_;
96 7 my $code = "";
97  
98 26 ▶ use Types::Standard 'Num';
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 7 ▶ if ($type->has_coercion and $type->coercion->can_be_inlined) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion and $type->coercion->can_be_inlined) 2 6 8
Condition66%2/3
$type->has_coercion and $type->coercion->can_be_inlined
!ll&&!rl&&r
403
MC/DC50%1/2
$type->has_coercion and $type->coercion->can_be_inlined
$type->has_coercion $type->coercion->can_be_inlined
Truth table66%2/3
A and B
A= $type->has_coercionB= $type->coercion->can_be_inlined
ABresult
0X0
100
111
101 3 $code .= '$_ = ' . $type->coercion->inline_coercion('$_') . ";\n";
102 }
103 7 ▶ if ($type->can_be_inlined) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->can_be_inlined) 5 4 9
104 7 $code .= $type->inline_assert('$_') . "\n";
105 }
106 7 ▶ if ($type->is_a_type_of(Num)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_a_type_of(&Num())) 4 5 9
107 6 $code .= "\$_ += 0;\n";
108 }
109  
110 9 return $code;
111 }
112  
113 4 ▶sub relationship {
Subroutine relationship: called CC=3 SCAR=11.0
Pod documented
114 7 my $self = shift;
115  
116 7 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 1 4 5
117 3 return $self->{relationship}{$_[0]};
118 }
119  
120 5 my ($type, $field, $other, %args) = @_;
121 5 my $class = 'Mandel::Relationship::' . Mojo::Util::camelize($type);
122 5 my $e = load_class $class;
123  
124 5 ▶ confess $e if ref $e;
Branch: 1 branch
BranchTrueFalseTotal
if ref $e 0 6 6
125  
126 5 $self->{relationship}{$field}
127 = $class->new(accessor => $field, document_class => $self->document_class, related_class => $other, %args);
128 }
129  
130 5 ▶sub new_collection {
Subroutine new_collection: called CC=2 SCAR=7.2
Pod documented
131 3 my ($self, $connection, %args) = @_;
132  
133 3 ▶ $self->collection_class->new(
Condition50%1/2
$connection || confess('$model->new_collection($connection)')
l!l
30
MC/DC0%0/1
$connection || confess('$model->new_collection($connection)')
$connection
Truth table50%1/2
A
A= $connection
Aresult
11
00
134 {connection => $connection || confess('$model->new_collection($connection)'), model => $self, %args});
135 }
136  
137 28 1;
138  
139 =encoding utf8
140  
141 =head1 NAME
142  
143 Mandel::Model - An object modelling a document
144  
145 =head1 DESCRIPTION
146  
147 This class is used to descrieb the structure of L<document|Mandel::Document>
148 in mongodb.
149  
150 =head1 ATTRIBUTES
151  
152 =head2 collection_name
153  
154 The name of the collection in the database. Default is the plural form of L</name>.
155  
156 =head2 collection_class
157  
158 The class name of the collection class. This default to L<Mandel::Collection>.
159  
160 =head2 document_class
161  
162 The class name of the document this description is attached to. Default to
163 an autogenerated class name.
164  
165 =head2 name
166  
167 The name of this model. Same as given to L<Mandel/model> and
168 L<Mandel/collection>.
169  
170 =head1 METHODS
171  
172 =head2 field
173  
174 $field_obj = $self->field('name');
175 $self = $self->field(name => \%meta);
176 $self = $self->field(['name1', 'name2'], \%meta);
177  
178 Used to define new field(s) or retrieve a defined L<Mandel::Model::Field>
179 object.
180  
181 =head2 fields
182  
183 @fields = $self->fields;
184  
185 Get list of L<Mandel::Model::Field> objects in the order they were added to
186 thie model.
187  
188 =head2 relationship
189  
190 $rel_obj = $self->relationship($type => $accessor => 'Other::Document::Class', %args);
191 $rel_obj = $self->relationship($accessor);
192  
193 This method is used to describe a relationship between two documents.
194  
195 See L<Mandel::Relationship::BelongsTo>, L<Mandel::Relationship::HasMany> or
196 L<Mandel::Relationship::HasOne>.
197  
198 C<$accessor> will be used as l<Mandel::Relationship/accessor>,
199 "Other::Document::Class" will be used as L<Mandel::Relationship/related_class>
200 and C<Mandel::Model/document_class> will be used as
201 L<Mandel::Relationship/document_class>.
202  
203 C<%args> is passed on the the L<relationship|Mandel::Relationship> constructor.
204  
205 =head2 new_collection
206  
207 $self->new_collection($connection);
208  
209 Returns a new instance of L</collection_class>.
210  
211 =head1 SEE ALSO
212  
213 L<Mojolicious>, L<Mango>, L<Mandel>
214  
215 =head1 AUTHOR
216  
217 Jan Henning Thorsen - C<jhthorsen@cpan.org>
218  
219 =cut
« andel.pm ↑ Summary Mandel/Iterator.pm »