Meta/Role/Relationship/HasMany.pm

filter: Statementstmt 90.1% 55 / 61 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 82.3% 14 / 17 Podpod 100% 2 / 2 totaltotal 88.3% 76 / 86 SCAR 13.9
CC
4
Cov
90%
_build_associated_method
2.0
detach_from_class
2.0
_build_iterator_class
2.0
CRAP 4.0
« Meta/Class/Schema.pm ↑ Summary Meta/HasMany/ViaFK.pm »
1 package Fey::Meta::Role::Relationship::HasMany;
2 {
3 10 $Fey::Meta::Role::Relationship::HasMany::VERSION = '0.46';
4 }
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Fey::Object::Iterator::FromSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Fey::Object::Iterator::FromSelect::Caching;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Fey::ORM::Types qw( ClassDoesIterator );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 with 'Fey::Meta::Role::Relationship';
18  
19 10 has associated_method => (
20 is => 'rw',
21 isa => 'Moose::Meta::Method',
22 init_arg => undef,
23 lazy => 1,
24 builder => '_build_associated_method',
25 );
26  
27 10 has 'iterator_class' => (
28 is => 'ro',
29 isa => ClassDoesIterator,
30 lazy => 1,
31 builder => '_build_iterator_class',
32 );
33  
34 sub _build_iterator_class {
35 7 ▶ my $self = shift;
Subroutine _build_iterator_class: called CC=2 SCAR=6.9
36  
37 7 ▶ return $self->is_cached()
Branch: 1 branch
BranchTrueFalseTotal
$self->is_cached ? : 2 5 7
38 ? 'Fey::Object::Iterator::FromSelect::Caching'
39 : 'Fey::Object::Iterator::FromSelect';
40 }
41  
42 7 ▶sub _build_is_cached {0}
Subroutine _build_is_cached: called CC=1 SCAR=0.0
43  
44 sub _build_associated_method {
45 9 ▶ my $self = shift;
Subroutine _build_associated_method: called CC=2 SCAR=7.0
46  
47 9 my $method;
48 9 ▶ if ( $self->is_cached() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_cached) { } 2 7 9
49 2 my $iter_attr_name = q{___} . $self->name() . q{_iterator};
50  
51 2 $self->associated_class()->add_attribute(
52 $iter_attr_name,
53 is => 'ro',
54 isa => $self->iterator_class(),
55 lazy => 1,
56 default => $self->_make_iterator_maker(),
57 init_arg => undef,
58 );
59  
60 2 ▶ $method = sub { return $_[0]->$iter_attr_name()->clone() };
Subroutine __ANON__: not called CC=1 SCAR=2.6
Subroutine child_message_users: not called CC=1 SCAR=2.6
61 }
62 else {
63 7 $method = $self->_make_iterator_maker();
64 }
65  
66 7 return $self->associated_class()->method_metaclass()->wrap(
67 name => $self->name(),
68 package_name => $self->associated_class()->name(),
69 body => $method,
70 );
71 }
72  
73 sub _make_subref_for_sql {
74 7 ▶ my $self = shift;
Subroutine _make_subref_for_sql: called CC=1 SCAR=0.6
75 7 my $select = shift;
76 7 my $bind_sub = shift;
77  
78 7 my $target_table = $self->foreign_table();
79  
80 7 my $iterator_class = $self->iterator_class();
81  
82 return sub {
83 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
84  
85 0 my $class = $self->meta()->ClassForTable($target_table);
86  
87 0 my $dbh = $self->_dbh($select);
88  
89 0 return $iterator_class->new(
90 classes => $class,
91 dbh => $dbh,
92 select => $select,
93 bind_params => [ $self->$bind_sub() ],
94 );
95 7 };
96  
97 }
98  
99 sub attach_to_class {
100 9 ▶ my $self = shift;
Subroutine attach_to_class: called CC=1 SCAR=0.0
Pod documented
101 9 my $class = shift;
102  
103 9 $self->_set_associated_class($class);
104  
105 9 $class->add_method( $self->name() => $self->associated_method() );
106 }
107  
108 sub detach_from_class {
109 4 ▶ my $self = shift;
Subroutine detach_from_class: called CC=2 SCAR=7.0
Pod documented
110  
111 4 ▶ return unless $self->associated_class();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associated_class 0 4 4
112  
113 4 $self->associated_class->remove_method( $self->name() );
114  
115 4 $self->_clear_associated_class();
116 }
117  
118 10 1;
119  
120 # ABSTRACT: A role for has-many metaclasses
121  
122 __END__
123  
124 =pod
125  
126 =head1 NAME
127  
128 Fey::Meta::Role::Relationship::HasMany - A role for has-many metaclasses
129  
130 =head1 VERSION
131  
132 version 0.46
133  
134 =head1 DESCRIPTION
135  
136 This role provides shared functionality for the two has-many metaclasses,
137 L<Fey::Meta::HasMany::ViaFK> and L<Fey::Meta::HasMany::ViaSelect>.
138  
139 =head1 CONSTRUCTOR OPTIONS
140  
141 This role adds the following constructor options:
142  
143 =over 4
144  
145 =item * is_cached
146  
147 Defaults to false for this class.
148  
149 =item * iterator_class
150  
151 This is the class used for iterators over the objects in this
152 relationship. By default, if this relationship is cached, it uses
153 L<Fey::Object::Iterator::FromSelect::Caching>, otherwise it uses
154 L<Fey::Object::Iterator::FromSelect>
155  
156 =back
157  
158 =head1 METHODS
159  
160 This role provides the following methods:
161  
162 =head2 $hm->name()
163  
164 Corresponds to the value passed to the constructor.
165  
166 =head2 $hm->table()
167  
168 Corresponds to the value passed to the constructor.
169  
170 =head2 $hm->foreign_table()
171  
172 Corresponds to the value passed to the constructor.
173  
174 =head2 $ho->is_cached()
175  
176 Corresponds to the value passed to the constructor, or the calculated
177 default.
178  
179 =head2 $hm->iterator_class()
180  
181 Corresponds to the value passed to the constructor, or the calculated
182 default.
183  
184 =head2 $hm->attach_to_class($class)
185  
186 This method takes a F<Fey::Meta::Class::Table> object and attaches the
187 relationship to the associated class. Doing so will create a new
188 method in the associated class.
189  
190 =head2 $hm->associated_class()
191  
192 The class associated with this object. This is undefined until C<<
193 $hm->attach_to_class() >> is called.
194  
195 =head2 $hm->associated_method()
196  
197 Returns the method associated with this object, if any.
198  
199 =head2 $hm->detach_from_class()
200  
201 If this object was attached to a class, it removes the method it made,
202 and unsets the C<associated_class>.
203  
204 =head1 AUTHOR
205  
206 Dave Rolsky <autarch@urth.org>
207  
208 =head1 COPYRIGHT AND LICENSE
209  
210 This software is copyright (c) 2011 by Dave Rolsky.
211  
212 This is free software; you can redistribute it and/or modify it under
213 the same terms as the Perl 5 programming language system itself.
214  
215 =cut
« Meta/Class/Schema.pm ↑ Summary Meta/HasMany/ViaFK.pm »