blib/lib/UR/Object/Type/AccessorWriter.pm untested

filter: Statementstmt 0.0% 0 / 1137 Branchbran 0.0% 0 / 526 Conditioncond 0.0% 0 / 203 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 1921 SCAR 111.6
CC
264
Cov
0%
mk_object_set_accessors
6972.0
mk_indirect_rw_accessor
1122.0
initialize_direct_accessors
650.0
CRAP 69960.0
1  
2 package UR::Object::Type::AccessorWriter;
3  
4 package UR::Object::Type;
5  
6 use strict;
7 use warnings;
8 require UR;
9 our $VERSION = "0.46"; # UR $VERSION;
10 #use warnings FATAL => 'all';
11  
12 use Carp ();
13 use Sub::Name ();
14 use Sub::Install ();
15 use List::Util;
16  
17 sub mk_rw_accessor {
18 my ($self, $class_name, $accessor_name, $column_name, $property_name, $is_transient) = @_;
19 $property_name ||= $accessor_name;
20  
21 my $full_name = join( '::', $class_name, $accessor_name );
22 my $accessor = Sub::Name::subname $full_name => sub {
23 if (@_ > 1) {
24 my $old = $_[0]->{ $property_name };
25 my $new = $_[1];
26  
27 # The accessors may compare undef and an empty
28 # string. For speed, we turn warnings off rather
29 # than add extra code to make the warning disappear.
30 local $@;
31 my $different = eval { no warnings; $old ne $new };
32 if ($different or $@ =~ m/has no overloaded magic/)
33 {
34 $_[0]->{ $property_name } = $new;
35 $_[0]->__signal_change__( $property_name, $old, $new ) unless $is_transient; # FIXME is $is_transient right here? Maybe is_volatile instead (if at all)?
36 }
37 return $new;
38 }
39 $_[0]->{ $property_name }
40 };
41  
42 if (_class_is_singleton($class_name)) {
43 my $basic_accessor = $accessor;
44 $accessor = sub {
45 shift->_singleton_object->$basic_accessor(@_);
46 };
47 }
48  
49 Sub::Install::reinstall_sub({
50 into => $class_name,
51 as => $accessor_name,
52 code => $accessor,
53 });
54  
55 }
56  
57 sub mk_ro_accessor {
58 my ($self, $class_name, $accessor_name, $column_name, $property_name) = @_;
59 $property_name ||= $accessor_name;
60  
61 my $full_name = join( '::', $class_name, $accessor_name );
62 my $accessor = Sub::Name::subname $full_name => sub {
63 if (@_ > 1) {
64 my $old = $_[0]->{ $property_name};
65 my $new = $_[1];
66  
67 my $different;
68 my $exception = do {
69 local $@;
70 $different = eval { no warnings; $old ne $new };
71 $@;
72 };
73 if ($different or $exception =~ m/has no overloaded magic/)
74 {
75 Carp::croak("Cannot change read-only property $accessor_name for class $class_name!"
76 . " Failed to update " . $_[0]->__display_name__ . " property: $property_name from $old to $new");
77 }
78 return $new;
79 }
80 return $_[0]->{ $property_name };
81 };
82  
83 if (_class_is_singleton($class_name)) {
84 my $basic_accessor = $accessor;
85 $accessor = sub {
86 shift->_singleton_object->$basic_accessor(@_);
87 };
88 }
89  
90 Sub::Install::reinstall_sub({
91 into => $class_name,
92 as => $accessor_name,
93 code => $accessor,
94 });
95  
96 }
97  
98 sub _class_is_singleton {
99 my $class_name = shift;
100 return grep { $_->isa('UR::Singleton') } @{ $class_name->__meta__->{is} };
101 }
102  
103 sub mk_id_based_flex_accessor {
104 my ($self, $class_name, $accessor_name, $id_by, $r_class_name, $where, $id_class_by) = @_;
105  
106 unless (ref($id_by)) {
107 $id_by = [ $id_by ];
108 }
109  
110 my $id_resolver;
111 my $id_decomposer;
112 my @id;
113 my $id;
114 my $full_name = join( '::', $class_name, $accessor_name );
115 my $concrete_r_class_name = $r_class_name;
116 my $accessor = Sub::Name::subname $full_name => sub {
117 my $self = shift;
118 if (@_ == 1) {
119 # This one is to support syntax like this
120 # $cd->artist($different_artist);
121 # to switch which artist object this cd points to
122 my $object_value = shift;
123 if ($id_class_by and not ref $object_value) {
124 # when we have an id-class-by accessor and get a primitive, store it as a UR::Value
125 $object_value = UR::Value->get($object_value);
126 }
127 if (defined $object_value) {
128 if ($id_class_by) {
129 $concrete_r_class_name = ($object_value->can('class') ? $object_value->class : ref($object_value));
130 $id_decomposer = undef;
131 $id_resolver = undef;
132 $self->$id_class_by($concrete_r_class_name);
133 } elsif (! Scalar::Util::blessed($object_value) and ! $object_value->can('id')) {
134 Carp::croak("Can't call method \"id\" without a package or object reference. Expected an object as parameter to '$accessor_name', not the value '$object_value'");
135 }
136  
137 my $r_class_meta = do {
138 local $@;
139 eval { $concrete_r_class_name->__meta__ };
140 };
141 unless ($r_class_meta) {
142 Carp::croak("Can't get metadata for class $concrete_r_class_name. Is it a UR class?");
143 }
144  
145 $id_decomposer ||= $r_class_meta->get_composite_id_decomposer;
146 @id = $id_decomposer->($object_value->id);
147 if (@$id_by == 1) {
148 my $id_property_name = $id_by->[0];
149 $self->$id_property_name($object_value->id);
150 } else {
151 @id = $id_decomposer->($object_value->id);
152 Carp::croak("Cannot alter value for '$accessor_name' on $class_name: The passed-in object of type "
153 . $object_value->class . " has " . scalar(@id) . " id properties, but the accessor '$accessor_name' has "
154 . scalar(@$id_by) . " id_by properties");
155 for my $id_property_name (@$id_by) {
156 $self->$id_property_name(shift @id);
157 }
158 }
159 }
160 else {
161 if ($id_class_by) {
162 $self->$id_class_by(undef);
163 }
164 for my $id_property_name (@$id_by) {
165 $self->$id_property_name(undef);
166 }
167 }
168 return $object_value;
169 }
170 else {
171 if ($id_class_by) {
172 $concrete_r_class_name = $self->$id_class_by;
173 $id_decomposer = undef;
174 $id_resolver = undef;
175 return unless $concrete_r_class_name;
176 }
177 unless ($id_resolver) {
178 my $concrete_r_class_meta = UR::Object::Type->get($concrete_r_class_name);
179 unless ($concrete_r_class_meta) {
180 Carp::croak("Can't resolve value for '$accessor_name' on class $class_name id '".$self->id
181 . "': No class metadata for value '$concrete_r_class_name' referenced as property '$id_class_by'");
182 }
183 $id_resolver = $concrete_r_class_meta->get_composite_id_resolver;
184 }
185  
186 # eliminate the old map{} because of side effects with $_
187 # when the id_by property happens to be calculated
188 #@id = map { $self->$_ } @$id_by;
189 @id=();
190 for my $property_name (@$id_by) { # no implicit topic
191 my $value = $self->$property_name; # scalar context
192 push @id, $value;
193 }
194  
195 $id = $id_resolver->(@id);
196 return if not defined $id;
197 if ($concrete_r_class_name eq 'UR::Object') {
198 Carp::carp("Querying by using UR::Object class is deprecated.");
199 }
200  
201 if ($concrete_r_class_name->isa("UR::Value")) {
202 return $id;
203 }
204 else {
205 if (@_ || $where) {
206 # There were additional params passed in
207 return $concrete_r_class_name->get(id => $id, @_, @$where);
208 } else {
209 return $concrete_r_class_name->get($id);
210 }
211 }
212 }
213 };
214  
215 Sub::Install::reinstall_sub({
216 into => $class_name,
217 as => $accessor_name,
218 code => $accessor,
219 });
220 }
221  
222 sub mk_id_based_object_accessor {
223 my ($self, $class_name, $accessor_name, $id_by, $r_class_name, $where, $id_class_by) = @_;
224  
225 unless (ref($id_by)) {
226 $id_by = [ $id_by ];
227 }
228  
229 my $id_resolver;
230 my $id_decomposer;
231 my @id;
232 my $id;
233 my $full_name = join( '::', $class_name, $accessor_name );
234 my $concrete_r_class_name = $r_class_name;
235 my $accessor = Sub::Name::subname $full_name => sub {
236 my $self = shift;
237 if (@_ == 1) {
238 # This one is to support syntax like this
239 # $cd->artist($different_artist);
240 # to switch which artist object this cd points to
241 my $object_value = shift;
242 if (defined $object_value) {
243 if ($id_class_by) {
244 $concrete_r_class_name = ($object_value->can('class') ? $object_value->class : ref($object_value));
245 $id_decomposer = undef;
246 $id_resolver = undef;
247 $self->$id_class_by($concrete_r_class_name);
248 } elsif (! Scalar::Util::blessed($object_value) and ! $object_value->can('id')) {
249 Carp::croak("Can't call method \"id\" without a package or object reference. Expected an object as parameter to '$accessor_name', not the value '$object_value'");
250 }
251  
252 my $r_class_meta = do {
253 local $@;
254 eval { $concrete_r_class_name->__meta__ };
255 };
256 unless ($r_class_meta) {
257 Carp::croak("Can't get metadata for class $concrete_r_class_name. Is it a UR class?");
258 }
259  
260 $id_decomposer ||= $r_class_meta->get_composite_id_decomposer;
261 @id = $id_decomposer->($object_value->id);
262 if (@$id_by == 1) {
263 my $id_property_name = $id_by->[0];
264 $self->$id_property_name($object_value->id);
265 } else {
266 @id = $id_decomposer->($object_value->id);
267 Carp::croak("Cannot alter value for '$accessor_name' on $class_name: The passed-in object of type "
268 . $object_value->class . " has " . scalar(@id) . " id properties, but the accessor '$accessor_name' has "
269 . scalar(@$id_by) . " id_by properties");
270 for my $id_property_name (@$id_by) {
271 $self->$id_property_name(shift @id);
272 }
273 }
274 }
275 else {
276 if ($id_class_by) {
277 $self->$id_class_by(undef);
278 }
279 for my $id_property_name (@$id_by) {
280 $self->$id_property_name(undef);
281 }
282 }
283 return $object_value;
284 }
285 else {
286 if ($id_class_by) {
287 $concrete_r_class_name = $self->$id_class_by;
288 $id_decomposer = undef;
289 $id_resolver = undef;
290 return unless $concrete_r_class_name;
291 }
292 unless ($id_resolver) {
293 my $concrete_r_class_meta = UR::Object::Type->get($concrete_r_class_name);
294 unless ($concrete_r_class_meta) {
295 Carp::croak("Can't resolve value for '$accessor_name' on class $class_name id '".$self->id
296 . "': No class metadata for value '$concrete_r_class_name' referenced as property '$id_class_by'");
297 }
298 $id_resolver = $concrete_r_class_meta->get_composite_id_resolver;
299 }
300  
301 @id=();
302 for my $property_name (@$id_by) { # no implicit topic
303 my $value = $self->$property_name; # scalar context
304 push @id, $value;
305 }
306  
307 $id = @id > 1
308 ? $id_resolver->(@id)
309 : $id[0];
310  
311 return if not defined $id;
312 if ($concrete_r_class_name eq 'UR::Object') {
313 Carp::carp("Querying by using UR::Object class is deprecated.");
314 }
315 if (@_ || $where) {
316 # There were additional params passed in
317 return $concrete_r_class_name->get(id => $id, @_, @$where);
318 } else {
319 return $concrete_r_class_name->get($id);
320 }
321 }
322 };
323  
324 Sub::Install::reinstall_sub({
325 into => $class_name,
326 as => $accessor_name,
327 code => $accessor,
328 });
329 }
330  
331  
332 sub _resolve_bridge_logic_for_indirect_property {
333 my ($ur_object_type, $class_name, $accessor_name, $via, $to, $where) = @_;
334  
335 my $bridge_collector = sub {
336 my $self = shift;
337 my @results = $self->$via(@$where);
338 # Indirect has one properties must return a single undef value for an empty result, even in list context.
339 return if @results == 1 and not defined $results[0];
340 return @results;
341 };
342 my $bridge_crosser = sub {
343 my $bridges = shift;
344 return map { $_->$to(@_) } @$bridges;
345 };
346  
347 return($bridge_collector, $bridge_crosser) if ($UR::Object::Type::bootstrapping);
348  
349 # bail out and use the default subs if any of these fail
350 my ($my_class_meta, $my_property_meta, $via_property_meta, $to_property_meta);
351  
352 $my_class_meta = $class_name->__meta__;
353 $my_property_meta = $my_class_meta->property_meta_for_name($accessor_name) if ($my_class_meta);
354 $via_property_meta = $my_class_meta->property_meta_for_name($via) if ($my_class_meta);
355 $to_property_meta = $my_property_meta->to_property_meta() if ($my_property_meta);
356  
357 if (! $my_class_meta || ! $my_property_meta || ! $via_property_meta || ! $to_property_meta) {
358 # Something didn't link right, use the default methods
359 return ($bridge_collector, $bridge_crosser);
360 }
361  
362 if ($my_property_meta->is_delegated and $my_property_meta->is_many
363 and $via_property_meta->is_many and $via_property_meta->reverse_as
364 and $via_property_meta->data_type and $via_property_meta->data_type->isa('UR::Object')
365 ) {
366 my $bridge_class = $via_property_meta->data_type;
367  
368 my @via_join_properties = do {
369 local $@;
370 eval { $via_property_meta->get_property_name_pairs_for_join };
371 };
372 if (! @via_join_properties) {
373 # this can happen if the properties aren't linked together as expected.
374 # For example, a property involved in a many-to-many relationship, but is
375 # defined as a one-to-many with reverse_as.
376 return ($bridge_collector, $bridge_crosser);
377 }
378  
379 my (@my_join_properties,@their_join_properties);
380 for (my $i = 0; $i < @via_join_properties; $i++) {
381 ($my_join_properties[$i], $their_join_properties[$i]) = @{ $via_join_properties[$i] };
382 }
383  
384 my(@where_properties, @where_values, %bridge_meta_params);
385 if ($where or $via_property_meta->where) {
386 my @collected_where;
387 @collected_where = @$where if ($where);
388 push @collected_where, @{ $via_property_meta->where } if ($via_property_meta->where);
389 while (@collected_where) {
390 my $where_property = shift @collected_where;
391 my $where_value = shift @collected_where;
392  
393 if (UR::BoolExpr::Util::is_meta_param($where_property)) {
394 $bridge_meta_params{$where_property} = $where_value;
395  
396 } else {
397 if (ref($where_value) eq 'HASH' and $where_value->{'operator'}) {
398 $where_property .= ' ' .$where_value->{'operator'};
399 $where_value = $where_value->{'value'};
400 }
401 push @where_properties, $where_property;
402 push @where_values, $where_value;
403 }
404 }
405 }
406  
407 my $bridge_template = UR::BoolExpr::Template->resolve($bridge_class, @their_join_properties, @where_properties, %bridge_meta_params);
408  
409 $bridge_collector = sub {
410 my $self = shift;
411 my @my_values = map { $self->$_} @my_join_properties;
412 my $bx = $bridge_template->get_rule_for_values(@my_values, @where_values);
413 return $bridge_class->get($bx);
414 };
415  
416 if ($to_property_meta->is_delegated
417 and
418 my $doubly_deledated_bridge_crosser = _resolve_bridge_crosser_for_doubly_delegated_property($to_property_meta, \%bridge_meta_params)
419 ) {
420 $bridge_crosser = $doubly_deledated_bridge_crosser;
421 }
422 }
423 return ($bridge_collector, $bridge_crosser);
424 }
425  
426 sub _make_results_sorter_for_doubly_delegated_bridge_crosser {
427 my($bridges, $bridge_linker, $results_linker) = @_;
428  
429 my $rank = 0;
430 my %bridge_rankings = map { $bridge_linker->() => $rank++ } @$bridges;
431 return sub {
432 my $results = shift;
433  
434 return map { $_->[1] }
435 sort { $bridge_rankings{ $a->[0] } <=> $bridge_rankings{ $b->[0] } }
436 map { [ $results_linker->(), $_ ] }
437 @$results;
438 };
439 }
440  
441 sub _resolve_bridge_crosser_for_doubly_delegated_property {
442 my($to_property_meta, $bridge_meta_params) = @_;
443 # This property's value is doubly delegated. The simple thing to
444 # do is to collect the bridge objects, then call the second
445 # delegation method on each bridge in a loop to collect the final
446 # results, which may trigger one query per result. Depending on
447 # the type of delegation, the final results can be collected with
448 # one query
449  
450 my($result_class_resolver, $bridge_linking_properties, $final_result_property_name, $result_filtering_property);
451 if ($to_property_meta->via) {
452 # bridges through another via-to property
453 my $second_via_property_meta = $to_property_meta->via_property_meta;
454 my $final_class_name = $second_via_property_meta->data_type;
455 if ($final_class_name and $final_class_name ne 'UR::Value' and $final_class_name->isa('UR::Object')) {
456 if ( 1 == (my @via2_join_properties = $second_via_property_meta->get_property_name_pairs_for_join)) {
457 $bridge_linking_properties = [ $via2_join_properties[0]->[0] ];
458 $result_filtering_property = $via2_join_properties[0]->[1];
459 $result_class_resolver = sub { $final_class_name };
460  
461 $final_result_property_name = $to_property_meta->to;
462 }
463 }
464  
465 } elsif ($to_property_meta->id_by) {
466 $bridge_linking_properties = $to_property_meta->id_by;
467 $result_filtering_property = 'id';
468 if ($to_property_meta->id_class_by) {
469 # Bridging through an 'id_class_by' property
470 # bucket the bridge items by the result class and do a get for
471 # each of those classes with a listref of IDs
472 my $result_class_resolving_property = $to_property_meta->id_class_by;
473 $result_class_resolver = sub { shift->$result_class_resolving_property };
474  
475 } else {
476 # Bridging through a regular id-by property
477 my $result_class = $to_property_meta->data_type;
478 $result_class_resolver = sub { $result_class };
479 }
480  
481 } elsif ($to_property_meta->reverse_as) {
482 if (1 == (my @reverse_as_join_properties = $to_property_meta->get_property_name_pairs_for_join)) {
483 $bridge_linking_properties = [ map { $_->[0] } @reverse_as_join_properties ];
484 $result_filtering_property = $reverse_as_join_properties[0]->[1];
485  
486 my $result_class = $to_property_meta->data_type;
487 $result_class_resolver = sub { $result_class };
488 }
489 }
490  
491 if ($result_class_resolver) {
492 my $linking_id_value_for_bridge = do {
493 my %composite_id_resolver_for_class;
494  
495 sub {
496 my $bridge = shift;
497 my @id = map { $bridge->$_ } @$bridge_linking_properties;
498  
499 my $result_class = $result_class_resolver->($bridge);
500 my $id_resolver = $composite_id_resolver_for_class{ $result_class }
501 ||= $result_class->__meta__->get_composite_id_resolver;
502  
503 return $id_resolver->(@id);
504 };
505 };
506  
507 return sub {
508 my $bridges = shift;
509 my %result_class_names_and_ids;
510  
511 foreach my $bridge ( @$bridges ) {
512 my $result_class = $result_class_resolver->($bridge);
513 $result_class_names_and_ids{$result_class} ||= [];
514  
515 my $id = $linking_id_value_for_bridge->($bridge);
516 push @{ $result_class_names_and_ids{ $result_class } }, $id;
517 }
518  
519 my @results;
520 foreach my $result_class ( keys %result_class_names_and_ids ) {
521 if($result_class->isa('UR::Value')) { #can't group queries together for UR::Values
522 push @results, map { $result_class->get($result_filtering_property => $_, @_) } @{$result_class_names_and_ids{$result_class}};
523 } else {
524 push @results, $result_class->get($result_filtering_property => $result_class_names_and_ids{$result_class}, @_);
525 }
526 }
527  
528 if ($bridge_meta_params->{'-order'} || $bridge_meta_params->{'-order_by'}) {
529 my $results_sorter = _make_results_sorter_for_doubly_delegated_bridge_crosser(
530 $bridges,
531 sub { return $linking_id_value_for_bridge->($_) },
532 sub { $_->id } );
533 @results = $results_sorter->(\@results);
534 }
535  
536 @results = map { $_->$final_result_property_name } @results if ($to_property_meta->via);
537 return @results;
538 };
539 }
540  
541 return;
542 }
543  
544 sub _is_assignment_value {
545 return (
546 @_ == 1
547 and not (ref($_[0]) and Scalar::Util::blessed($_[0]) and $_[0]->isa("UR::BoolExpr"))
548 );
549 }
550  
551 sub mk_indirect_ro_accessor {
552 my ($ur_object_type, $class_name, $accessor_name, $via, $to, $where) = @_;
553 my @where = ($where ? @$where : ());
554 my $full_name = join( '::', $class_name, $accessor_name );
555 my $filterable_accessor_name = 'get_' . $accessor_name; # FIXME we need a better name for
556 my $filterable_full_name = join( '::', $class_name, $filterable_accessor_name );
557  
558 # This is part of an experimental refactoring of indirect accessors. The goal is to
559 # get rid of all the special cases inside of _resolve_bridge_logic_for_indirect_property()
560 # and do the right thing with the Join data
561 my (@collectors, @crossers);
562 my $accessor2 = Sub::Name::subname $full_name.'_new' => sub {
563 my $self = shift;
564 Carp::croak("Assignment value passed to read-only indirect accessor $accessor_name for class $class_name") if @_ and _is_assignment_value(@_);
565  
566 if ($class_name =~ m/^UR::/) {
567 # Some methods will recurse into here if called on a UR::* class (especially
568 # UR::BoolExpr), so do the dumb but safe thing
569 my $bridge_collector = sub {
570 my $self = shift;
571 my @results = $self->$via(@$where);
572 # Indirect has one properties must return a single undef value for an empty result, even in list context.
573 return if @results == 1 and not defined $results[0];
574 return @results;
575 };
576  
577 #TODO: move this crosser closure logic down and get rid of the closure
578 my @filter = @_;
579 my $bridge_crosser = sub { return map { $_->$to(@filter) } @_ };
580 my @bridges = $bridge_collector->($self);
581 return unless @bridges;
582 return $self->context_return(@bridges) if ($to eq '-filter');
583  
584 my @results = $bridge_crosser->(@bridges);
585 return $self->context_return(@results);
586 }
587  
588 unless (@collectors) {
589 require List::MoreUtils;
590  
591 my $prop_meta = $class_name->__meta__->property_meta_for_name($accessor_name);
592 my @join_list = $prop_meta->_resolve_join_chain();
593 foreach my $join ( @join_list ) {
594 my @source_property_names = @{$join->{source_property_names}};
595 my $collector = sub {
596 my @list = grep { defined && length } map { my $o = $_; map { $o->$_ } @source_property_names} @_;
597 return @list == 1 ? $list[0] : \@list;
598 };
599 push @collectors, $collector;
600  
601 my $foreign_class = $join->{foreign_class};
602 my $crosser;
603 if (! $foreign_class->isa('UR::Value')) {
604 my @foreign_property_names = @{$join->{foreign_property_names}};
605  
606 $crosser = sub { my @get_params = List::MoreUtils::pairwise
607 { $a => $b } @foreign_property_names, @_;
608 return $foreign_class->get(@get_params); };
609 }
610 push @crossers, $crosser;
611 }
612 }
613  
614 my @working = ($self);
615  
616 # This can probably be rewritten with List::Util::reduce
617 for (my $i = 0; $i < @collectors; $i++) {
618 last unless @working;
619 my @working = $collectors[$i]->(@working);
620 next unless $crossers[$i];
621 @working = $crossers[$i]->(@working);
622 }
623 $self->context_return(@working);
624 };
625 #Sub::Install::reinstall_sub({
626 # into => $class_name,
627 # as => $accessor_name.'_new',
628 # code => $accessor2,
629 #});
630  
631  
632 my($bridge_collector, $bridge_crosser);
633  
634 my $accessor = Sub::Name::subname $full_name => sub {
635 my $self = shift;
636 Carp::croak("Assignment value passed to read-only indirect accessor $accessor_name for class $class_name") if @_ == 1 and _is_assignment_value(@_);
637  
638 unless ($bridge_collector) {
639 ($bridge_collector, $bridge_crosser)
640 = $ur_object_type->_resolve_bridge_logic_for_indirect_property($class_name, $accessor_name, $via, $to, \@where);
641 }
642  
643 my @bridges = $bridge_collector->($self);
644  
645 return unless @bridges;
646 return $self->context_return(@bridges) if ($to eq '-filter');
647  
648 my @results = $bridge_crosser->(\@bridges, @_);
649 $self->context_return(@results);
650 };
651  
652 unless ($accessor_name) {
653 Carp::croak("No accessor name specified for read-only indirect accessor $accessor_name for class $class_name");
654 }
655  
656 Sub::Install::reinstall_sub({
657 into => $class_name,
658 as => $accessor_name,
659 code => $accessor,
660 });
661  
662 my $r_class_name;
663 my $r_class_name_resolver = sub {
664 return $r_class_name if $r_class_name;
665  
666 my $linking_property = UR::Object::Property->get(class_name => $class_name, property_name => $via);
667 unless ($linking_property->data_type) {
668 Carp::croak "Property ${class_name}::${accessor_name}: via refers to a property with no data_type. Can't process filter";
669 }
670 my $final_property = UR::Object::Property->get(class_name => $linking_property->data_type,
671 property_name => $to);
672 unless ($final_property->data_type) {
673 Carp::croak "Property ${class_name}::${accessor_name}: to refers to a property with no data_type. Can't process filter";
674 }
675 $r_class_name = $final_property->data_type;
676 };
677  
678 my $filterable_accessor = Sub::Name::subname $filterable_full_name => sub {
679 my $self = shift;
680 my @results = $self->$accessor_name();
681 if (@_) {
682 my $rule;
683 if (@_ == 1 and ref($_[0]) and $_[0]->isa('UR::BoolExpr')) {
684 $rule = shift;
685 } else {
686 $r_class_name ||= $r_class_name_resolver->();
687 $rule = UR::BoolExpr->resolve_normalized($r_class_name, @_);
688 }
689 @results = grep { $rule->evaluate($_) } @results;
690 }
691 $self->context_return(@results);
692 };
693 Sub::Install::reinstall_sub({
694 into => $class_name,
695 as => $filterable_accessor_name,
696 code => $filterable_accessor,
697 });
698  
699 }
700  
701  
702 sub mk_indirect_rw_accessor {
703 my ($ur_object_type, $class_name, $accessor_name, $via, $to, $where, $singular_name, $property_name) = @_;
704 $property_name ||= $accessor_name;
705 my @where = ($where ? @$where : ());
706 my $full_name = join( '::', $class_name, $accessor_name );
707  
708 my $update_strategy; # defined the first time we "set" a value through this
709 my $adder;
710 my $via_property_meta;
711 my $r_class_name;
712 my $is_many;
713  
714 my $resolve_update_strategy = sub {
715 unless (defined $update_strategy) {
716 # Resolve the strategy. We need to figure out if $to
717 # refers to an id-property. This is only called once, when the
718 # accessor is first used.
719  
720 # If we reference a remote object, and go to one of its id properties
721 # we must do a delete/create instead of property change. Note that
722 # this is only allowed when the remote object has no direct properties
723 # which are not id properties.
724  
725 my $my_property_meta = $class_name->__meta__->property_meta_for_name($property_name);
726 unless ($my_property_meta) {
727 Carp::croak("Failed to find property meta for '$property_name' on class $class_name");
728 }
729 $is_many = $my_property_meta->is_many;
730  
731 $via_property_meta ||= $class_name->__meta__->property_meta_for_name($via);
732 unless ($via_property_meta) {
733 Carp::croak("Failed to find property metadata for via property '$via' while resolving property '$property_name' on class $class_name");
734 }
735  
736 $r_class_name ||= $via_property_meta->data_type;
737 unless ($r_class_name) {
738 Carp::croak("Cannot resolve property '$property_name' on class $class_name: It is via property '$via' which has no data_type");
739 }
740 my $r_class_meta = $r_class_name->__meta__;
741 unless ($r_class_meta) {
742 Carp::croak("Cannot resolve property '$property_name' on class $class_name: It is via property '$via' with data_type $r_class_name which is not a valid class name");
743 }
744  
745 $adder = "add_" . $via_property_meta->singular_name;
746  
747 if ($my_property_meta->_involves_id_property) {
748 $update_strategy = 'delete-create'
749 }
750 else {
751 $update_strategy = 'change';
752 }
753 }
754 return $update_strategy;
755 };
756  
757 my ($bridge_collector, $bridge_crosser);
758 my $accessor = Sub::Name::subname $full_name => sub {
759 my $self = shift;
760  
761 unless ($bridge_collector) {
762 ($bridge_collector, $bridge_crosser)
763 = $ur_object_type->_resolve_bridge_logic_for_indirect_property($class_name, $accessor_name, $via, $to, \@where);
764 }
765  
766 my @bridges = $bridge_collector->($self);
767  
768 if ( @_ == 1 and _is_assignment_value(@_) ) {
769 $resolve_update_strategy->() unless (defined $update_strategy);
770  
771 if ($update_strategy eq 'change') {
772 if (@bridges == 0) {
773 #print "adding via $adder @where :::> $to @_\n";
774 my $exception = do {
775 local $@;
776 @bridges = eval { $self->$adder(@where, $to => $_[0]) };
777 $@;
778 };
779 if ($exception) {
780 my $r_class_meta = $r_class_name->__meta__;
781 my $property_meta = $r_class_meta->property($to);
782 if ($property_meta) {
783 # Re-throw the original exception
784 die $exception;
785 } else {
786 Carp::croak("Couldn't create a new object through indirect property "
787 . "'$accessor_name' on $class_name. 'to' is $to which is not a property on $r_class_name.");
788 }
789 }
790 #WAS > Carp::confess("Cannot set $accessor_name on $class_name $self->{id}: property is via $via which is not set!");
791 }
792 elsif (@bridges > 1) {
793 Carp::croak("Cannot set '$accessor_name' on $class_name id '$self->{id}': multiple instances of '$via' found, via which the property is set");
794 }
795 #print "updating $bridges[0] $to to @_\n";
796 return $bridges[0]->$to(@_);
797 }
798 elsif ($update_strategy eq 'delete-create') {
799 if (@bridges > 1) {
800 Carp::croak("Cannot set '$accessor_name' on $class_name $self->{id}: multiple instances of '$via' found, via which the property is set");
801 }
802 else {
803 if (@bridges) {
804 #print "deleting $bridges[0]\n";
805 $bridges[0]->delete;
806 }
807 #print "adding via $adder @where :::> $to @_\n";
808 @bridges = $self->$adder(@where, $to => $_[0]);
809 unless (@bridges) {
810 Carp::croak("Failed to add bridge for '$accessor_name' on $class_name if '$self->{id}': method $adder returned false");
811 }
812 }
813 }
814 }
815 if (not defined $is_many) {
816 $resolve_update_strategy->();
817 }
818  
819 if ($is_many) {
820 return unless @bridges;
821 my @results = $bridge_crosser->(\@bridges, @_);
822 $self->context_return(@results);
823 } else {
824 return undef unless @bridges;
825 my @results = map { $_->$to(@_) } @bridges;
826 $self->context_return(@results);
827 }
828 };
829  
830 Sub::Install::reinstall_sub({
831 into => $class_name,
832 as => $accessor_name,
833 code => $accessor,
834 });
835  
836 if ($singular_name) { # True if we're defining an is_many indirect property
837 # Add
838 my $via_adder;
839 my $adder_method_name = 'add_' . $singular_name;
840 if ($class_name->can($adder_method_name)) {
841 $adder_method_name = '__' . $adder_method_name;
842 }
843 my $adder_method = Sub::Name::subname $class_name . '::' . $adder_method_name => sub {
844 my($self) = shift;
845  
846  
847 $resolve_update_strategy->() unless (defined $update_strategy);
848 unless (defined $via_adder) {
849 $via_adder = "add_" . $via_property_meta->singular_name;
850 }
851  
852 # By default, a single value will come in which is the remote value
853 # we just add the appropriate property name to it. If multiple
854 # values come in we trust the caller to be giving additional params.
855 if (@_ == 1) {
856 unshift @_, $to;
857 }
858 $self->$via_adder(@where,@_);
859 };
860 Sub::Install::reinstall_sub({
861 into => $class_name,
862 as => $adder_method_name,
863 code => $adder_method,
864 });
865  
866 # Remove
867 my $via_remover;
868 my $remover_method_name = 'remove_' . $singular_name;
869 if ($class_name->can($remover_method_name)) {
870 $remover_method_name = '__' . $remover_method_name;
871 }
872 my $remover_method = Sub::Name::subname $class_name . '::' . $remover_method_name => sub {
873 my($self) = shift;
874  
875 $resolve_update_strategy->() unless (defined $update_strategy);
876 unless (defined $via_remover) {
877 $via_remover = "remove_" . $via_property_meta->singular_name;
878 }
879  
880 # By default, a single value will come in which is the remote value
881 # we just remove the appropriate property name to it. If multiple
882 # values come in we trust the caller to be giving removeitional params.
883 if (@_ == 1) {
884 unshift @_, $to;
885 }
886 $self->$via_remover(@where,@_);
887 };
888 Sub::Install::reinstall_sub({
889 into => $class_name,
890 as => $remover_method_name,
891 code => $remover_method,
892 });
893 }
894  
895 }
896  
897  
898 sub mk_calculation_accessor {
899 my ($self, $class_name, $accessor_name, $calculation_src, $calculate_from, $params, $is_cached, $column_name) = @_;
900  
901 my $accessor;
902 my @src;
903  
904 if (not defined $calculation_src or $calculation_src eq '') {
905 $accessor = \&{ $class_name . '::' . $accessor_name };
906 unless ($accessor) {
907 Carp::croak "$accessor_name not defined in $class_name! Define it, or specify a calculate => sub{} or calculate => \$perl_src in the class definition.";
908 }
909 }
910 elsif (ref($calculation_src) eq 'CODE') {
911 $accessor = sub {
912 my $self = shift;
913 if (@_) {
914 Carp::croak("$class_name $accessor_name is a read-only property derived from @$calculate_from");
915 }
916 return $calculation_src->(map { $self->$_ } @$calculate_from);
917 };
918 }
919 elsif ($calculation_src =~ /^[^\:\W]+$/) {
920 # built-in formula like 'sum' or 'product'
921 my $module_name = "UR::Object::Type::AccessorWriter::" . ucfirst(lc($calculation_src));
922 my $exception = do {
923 local $@;
924 eval "use $module_name";
925 $@;
926 };
927 die $exception if $exception;
928 @src = (
929 "sub ${class_name}::${accessor_name} {",
930 'my $self = $_[0];',
931 "${module_name}->calculate(\$self, [" . join(",", map { "'$_'" } @$calculate_from) . "], \@_)",
932 '}'
933 );
934 }
935 else {
936 @src = (
937 "sub ${class_name}::${accessor_name} {",
938 ($params ? 'my ($self,%params) = @_;' : 'my $self = $_[0];'),
939 (map { "my \$$_ = \$self->$_;" } @$calculate_from),
940 ($params ? (map { "my \$$_ = delete \$params{'$_'};" } @$params) : ()),
941 $calculation_src,
942 '}'
943 );
944 }
945  
946 if (!$accessor) {
947 if (@src) {
948 my $src = join("\n",@src);
949 #print ">>$src<<\n";
950 my $exception = do {
951 local $@;
952 eval $src;
953 $@;
954 };
955 if ($exception) {
956 Carp::croak "ERROR IN CALCULATED PROPERTY SOURCE: $class_name $accessor_name\n$exception\n";
957 }
958 $accessor = \&{ $class_name . '::' . $accessor_name };
959 unless ($accessor) {
960 Carp::confess("Failed to generate code body for calculated property ${class_name}::${accessor_name}!");
961 }
962 }
963 else {
964 Carp::croak "Error implementing calcuation accessor for $class_name $accessor_name!";
965 }
966 }
967  
968 if ($accessor and $is_cached) {
969 # Wrap the already-compiled accessor in another function to memoize the
970 # result and save the data into the object
971 my $calculator_sub = $accessor;
972 $accessor = sub {
973 if (@_ > 1) {
974 Carp::croak("Cannot change property $accessor_name for class $class_name: cached calculated properties are read-only");
975 }
976 unless (exists $_[0]->{$accessor_name}) {
977 $_[0]->{$accessor_name} = $calculator_sub->(@_);
978 }
979 return $_[0]->{$accessor_name};
980 };
981  
982 # Make a method to clear the cached value and force another calculation
983 my $invalidator_name;
984 ($invalidator_name = $accessor_name) =~ s/^_+//;
985 $invalidator_name = "__invalidate_${invalidator_name}__";
986 Sub::Install::reinstall_sub({
987 into => $class_name,
988 as => $invalidator_name,
989 code => sub { delete $_[0]->{$accessor_name} },
990 });
991 }
992  
993 my $full_name = join( '::', $class_name, $accessor_name );
994 $accessor = Sub::Name::subname $full_name => $accessor;
995 Sub::Install::reinstall_sub({
996 into => $class_name,
997 as => $accessor_name,
998 code => $accessor,
999 });
1000  
1001 return $accessor;
1002 }
1003  
1004 sub mk_dimension_delegate_accessors {
1005 my ($self, $accessor_name, $ref_class_name, $non_id_properties, $other_accessor_name, $is_transient) = @_;
1006  
1007 # Like mk_rw_accessor, but knows that this accessor is a foreign
1008 # key to a dimension table, and configures additional accessors.
1009 # Also makes this accessor "smart", to resolve the dimension
1010 # id only when needed.
1011  
1012 # Make EAV-like accessors for all of the remote properties
1013 my $class_name = $self->class_name;
1014  
1015 my $full_name = join( '::', $class_name, $other_accessor_name );
1016 my $other_accessor = Sub::Name::subname $full_name => sub {
1017 my $self = shift;
1018 my $delegate_id = $self->{$accessor_name};
1019 if (defined($delegate_id)) {
1020 # We're currently delegating.
1021 my $delegate = $ref_class_name->get($delegate_id);
1022 if (not @_) {
1023 # A simple get. Delegate.
1024 return $delegate->$other_accessor_name(@_);
1025 }
1026 else {
1027 # We're setting a value.
1028 # Switch from delegating to local access.
1029 # We'll switch back next-time the dimension ID
1030 # is actually requested by its accessor
1031 # (farther below).
1032 my $old = $delegate->$other_accessor_name;
1033 my $new = shift;
1034 my $different;
1035 my $exception = do {
1036 local $@;
1037 $different = eval { no warnings; $old ne $new };
1038 $@;
1039 };
1040 if ($different or $exception =~ m/has no overloaded magic/) {
1041 $self->{$accessor_name} = undef;
1042 for my $property (@$non_id_properties) {
1043 if ($property eq $other_accessor_name) {
1044 # set the value locally
1045 $self->{$property} = $new;
1046 }
1047 else {
1048 # grab the data from the (now previous) delegate
1049 $self->{$property} = $delegate->$property;
1050 }
1051 }
1052 $self->__signal_change__( $other_accessor_name, $old, $new ) unless $is_transient;
1053 return $new;
1054 }
1055 }
1056 }
1057 else {
1058 # We are not currently delegating.
1059 if (@_) {
1060 # set
1061 my $old = $self->{ $other_accessor_name };
1062 my $new = shift;
1063 my $different;
1064 my $exception = do {
1065 local $@;
1066 $different = eval { no warnings; $old ne $new };
1067 $@;
1068 };
1069 if ($different or $exception =~ m/has no overloaded magic/) {
1070 $self->{ $other_accessor_name } = $new;
1071 $self->__signal_change__( $other_accessor_name, $old, $new ) unless $is_transient;
1072 }
1073 return $new;
1074 }
1075 else {
1076 # get
1077 return $self->{ $other_accessor_name };
1078 }
1079 }
1080 };
1081  
1082 Sub::Install::reinstall_sub({
1083 into => $class_name,
1084 as => $other_accessor_name,
1085 code => $other_accessor,
1086 });
1087 }
1088  
1089 sub mk_dimension_identifying_accessor {
1090 my ($self, $accessor_name, $ref_class_name, $non_id_properties, $is_transient) = @_;
1091  
1092 # Like mk_rw_accessor, but knows that this accessor is a foreign
1093 # key to a dimension table, and configures additional accessors.
1094 # Also makes this accessor "smart", to resolve the dimension
1095 # id only when needed.
1096  
1097 # Make EAV-like accessors for all of the remote properties
1098 my $class_name = $self->class_name;
1099  
1100 # Make the actual accessor for the id_by property
1101 my $full_name = join( '::', $class_name, $accessor_name );
1102 my $accessor = Sub::Name::subname $full_name => sub {
1103 if (@_ > 1) {
1104 my $old = $_[0]->{ $accessor_name };
1105 my $new = $_[1];
1106 my $different;
1107 my $exception = do {
1108 local $@;
1109 $different = eval { no warnings; $old ne $new };
1110 $@;
1111 };
1112 if ($different or $exception =~ m/has no overloaded magic/) {
1113 $_[0]->{ $accessor_name } = $new;
1114 $_[0]->__signal_change__( $accessor_name, $old, $new ) unless $is_transient;
1115 }
1116 return $new;
1117 }
1118 if (not defined $_[0]->{ $accessor_name }) {
1119 # Resolve an ID for the current set of values
1120 # Switch to delegating to that object.
1121 my %params;
1122 my $self = $_[0];
1123 @params{@$non_id_properties} = delete @$self{@$non_id_properties};
1124 my $delegate = $ref_class_name->get_or_create(%params);
1125 return undef unless $delegate;
1126 $_[0]->{ $accessor_name } = $delegate->id;
1127 }
1128 return $_[0]->{ $accessor_name };
1129 };
1130  
1131 Sub::Install::reinstall_sub({
1132 into => $class_name,
1133 as => $accessor_name,
1134 code => $accessor,
1135 });
1136 }
1137  
1138 sub mk_rw_class_accessor
1139 {
1140 my ($self, $class_name, $accessor_name, $column_name, $is_transient, $variable_value, $calc_default) = @_;
1141  
1142 my $full_accessor_name = $class_name . "::" . $accessor_name;
1143 my $accessor = Sub::Name::subname $full_accessor_name => sub {
1144 if (@_ > 1) {
1145 my $old = $variable_value;
1146 $variable_value = $_[1];
1147  
1148 my $different;
1149 my $exception = do {
1150 local $@;
1151 $different = eval { no warnings; $old ne $variable_value };
1152 $@;
1153 };
1154 if ($different or $exception =~ m/has no overloaded magic/) {
1155 $_[0]->__signal_change__( $accessor_name, $old, $variable_value ) unless $is_transient;
1156 }
1157 } elsif (defined $calc_default) {
1158 $variable_value = $calc_default->();
1159 }
1160 undef $calc_default;
1161 return $variable_value;
1162 };
1163 Sub::Install::reinstall_sub({
1164 into => $class_name,
1165 as => $accessor_name,
1166 code => $accessor,
1167 });
1168  
1169 }
1170  
1171 sub mk_ro_class_accessor {
1172 my($self, $class_name, $accessor_name, $column_name, $variable_value, $calc_default) = @_;
1173  
1174 my $full_accessor_name = $class_name . "::" . $accessor_name;
1175 my $accessor = Sub::Name::subname $full_accessor_name => sub {
1176 if (@_ > 1) {
1177 my $new = $_[1];
1178  
1179 my $different;
1180 my $exception = do{
1181 local $@;
1182 $different = eval { no warnings; $variable_value ne $new };
1183 $@;
1184 };
1185 if ($different or $exception =~ m/has no overloaded magic/) {
1186 $new = defined($new) ? $new : '(undef)';
1187 my $report_variable_value = defined($variable_value) ? $variable_value : '(undef)';
1188 Carp::croak("Cannot change read-only class-wide property $accessor_name for class $class_name from $report_variable_value to $new!");
1189 }
1190 } elsif (defined $calc_default) {
1191 $variable_value = $calc_default->();
1192 }
1193 undef $calc_default;
1194 return $variable_value;
1195 };
1196 Sub::Install::reinstall_sub({
1197 into => $class_name,
1198 as => $accessor_name,
1199 code => $accessor,
1200 });
1201 }
1202  
1203  
1204  
1205  
1206 sub mk_object_set_accessors {
1207 my ($self, $class_name, $singular_name, $plural_name, $reverse_as, $r_class_name, $where) = @_;
1208  
1209 # These are set by the resolver closure below, and kept in scope by the other closures
1210 my $rule_template;
1211 my $r_class_meta;
1212 my @property_names;
1213 my @where = ($where ? @$where : ());
1214  
1215 my $rule_resolver = sub {
1216 my ($obj) = @_;
1217 my $loading_r_class_error = '';
1218 if (defined $r_class_name) {
1219 my $exception = do {
1220 local $@;
1221 eval {
1222 $r_class_meta = UR::Object::Type->is_loaded($r_class_name);
1223 unless ($r_class_meta or __PACKAGE__->use_module_with_namespace_constraints($r_class_name)) {
1224 # Don't die yet. The named class may not have a file associated with it
1225 $loading_r_class_error = "Couldn't load class $r_class_name: $@";
1226 $@ = '';
1227 }
1228  
1229 unless ($r_class_meta) {
1230 $r_class_name->class;
1231 $r_class_meta = UR::Object::Type->get(class_name => $r_class_name);
1232 }
1233 };
1234 $@;
1235 };
1236 if ($exception) {
1237 $loading_r_class_error .= "Couldn't get class object for $r_class_name: $exception";
1238 }
1239 }
1240 if ($r_class_meta and not $reverse_as) {
1241 # We have a real class on the other end, and it did not specify know to link back to us.
1242 # Try to infer how, otherwise fall back to the same logic we use with "primitives".
1243 my @possible_relationships = grep { $_->data_type eq $class_name }
1244 grep { defined $_->data_type }
1245 $r_class_meta->all_property_metas();
1246  
1247 if (@possible_relationships > 1) {
1248 Carp::croak "$class_name has an ambiguous definition for property \"$singular_name\"."
1249 . " The target class $r_class_name has " . scalar(@possible_relationships)
1250 . " relationships which reference back to $class_name."
1251 . " Correct by adding \"reverse_as => X\" to ${class_name}'s \"$singular_name\" definition one of the following values: "
1252 . join(",",map { '"' . $_->delegation_name . '"' } @possible_relationships) . ".\n";
1253 }
1254 elsif (@possible_relationships == 1) {
1255 $reverse_as = $possible_relationships[0]->property_name;
1256 }
1257 elsif (@possible_relationships == 0) {
1258 # we now fall through to the logic below and try direct arrayref storage
1259 #die "No relationships found between $r_class_name and $class_name. Error in definition for $class_name $singular_name!"
1260 }
1261 }
1262 if ($reverse_as and ! $r_class_meta) {
1263 # we've resolved reverse_as, but there's not r_class_meta?!
1264 $self->error_message("Can't resolve reverse relationship $class_name -> $plural_name. No class metadata for $r_class_name");
1265 if ($loading_r_class_error) {
1266 Carp::croak "While loading $r_class_name: $loading_r_class_error";
1267 } else {
1268 Carp::croak "Is class $r_class_name defined anywhere?";
1269 }
1270 }
1271  
1272 if ($reverse_as) {
1273 # join to get the data...
1274 unless ($r_class_meta) {
1275 Carp::croak("No remote class metadata found for class $r_class_name while resolving property '$singular_name' of class $class_name");
1276 }
1277 my $property_meta = $r_class_meta->property_meta_for_name($reverse_as);
1278 unless ($property_meta) {
1279 Carp::croak "Can't resolve reverse relationship $class_name -> $plural_name. Remote class $r_class_name has no property $reverse_as";
1280 }
1281 my @get_params;
1282 if ($property_meta->via) {
1283 # get_property_name_pairs_for_join() only works for properties connected directly.
1284 # we still need to use it during initialization, but for more complicated relationships
1285 # this should do the right thing
1286 push @get_params, $property_meta->property_name . '.id' => $obj->id;
1287 push @property_names, 'id';
1288 } else {
1289 my @property_links = $property_meta->get_property_name_pairs_for_join;
1290 for my $link (@property_links) {
1291 my $my_property_name = $link->[1];
1292 push @property_names, $my_property_name;
1293 unless ($obj->can($my_property_name)) {
1294 Carp::croak "Cannot handle indirect relationship $r_class_name -> $reverse_as. Class $class_name has no property named $my_property_name";
1295 }
1296 push @get_params, $link->[0], ($obj->$my_property_name || undef);
1297 }
1298 }
1299  
1300 if (my $id_class_by = $property_meta->id_class_by) {
1301 push @get_params, $id_class_by, $obj->class;
1302 push @property_names, 'class';
1303 }
1304 my $tmp_rule = $r_class_name->define_boolexpr(@get_params,@where);
1305 if (my $order_by = $property_meta->order_by) {
1306 push @get_params, $order_by;
1307 }
1308 $rule_template = $tmp_rule->template;
1309 unless ($rule_template) {
1310 Carp::croak "Error generating rule template to handle indirect relationship $class_name $singular_name referencing $r_class_name!";
1311 }
1312 return $tmp_rule;
1313 }
1314 else {
1315 # data is stored locally on the hashref
1316 #die "No relationships found between $r_class_name and $class_name. Error in definition for $class_name $singular_name!"
1317 }
1318 };
1319  
1320 my @where_values;
1321 for (my $i = 1; $i < @where; $i+=2) {
1322 if (ref($where[$i]) eq 'HASH' and exists($where[$i]->{'operator'})) {
1323 push @where_values, $where[$i]->{'value'}; # the operator is already stored in the template
1324 } else {
1325 push @where_values, $where[$i];
1326 }
1327 }
1328  
1329 # These will behave specially if the rule does not specify the ID, or all of the ID.
1330 my @params_prefix;
1331 my $params_prefix_resolved = 0;
1332 my $params_prefix_resolver = sub {
1333 # handle the case of has-many primitives
1334 return unless $r_class_meta;
1335  
1336 my $r_ids = $r_class_meta->property_meta_for_name($reverse_as)->{id_by};
1337  
1338 my $cmeta = UR::Object::Type->get($class_name);
1339 my $pmeta = $plural_name ? $cmeta->{has}{$plural_name} : $cmeta->{has}{$singular_name};
1340 if (my $specify_by = $pmeta->{specify_by}) {
1341 @params_prefix = ($specify_by);
1342 }
1343 else {
1344 # TODO: should this really be an auto-setting of the specify_by meta property?
1345 my @id_property_names = $r_class_name->__meta__->id_property_names;
1346 @params_prefix =
1347 grep {
1348 my $id_property_name = $_;
1349 ( (grep { $id_property_name eq $_ } @$r_ids) ? 0 : 1)
1350 }
1351 @id_property_names;
1352  
1353 # We only do the special single-value spec when there is one property not specified by the rule.
1354 # This is common for a multi-column primary key where all columns reference a parent object, except an index value, etc.
1355 @params_prefix = () unless scalar(@params_prefix) == 1;
1356 }
1357 $params_prefix_resolved = 1;
1358 };
1359  
1360 if (!$plural_name || $singular_name ne $plural_name) {
1361 my $single_accessor = Sub::Name::subname $class_name ."::$singular_name" => sub {
1362 my $self = shift;
1363 my $rule;
1364 $rule = $rule_resolver->($self) unless (defined $rule_template);
1365 if ($rule_template) {
1366 $rule = $rule_template->get_rule_for_values((map { $self->$_ } @property_names), @where_values) unless (defined $rule);
1367 $params_prefix_resolver->() unless $params_prefix_resolved;
1368 unshift @_, @params_prefix if @_ == 1;
1369 if (@_) {
1370 return my $obj = $r_class_name->get($rule->params_list,@_);
1371 }
1372 else {
1373 return my $obj = $r_class_name->get($rule);
1374 }
1375 }
1376 else {
1377 return unless $self->{$plural_name};
1378 return unless @_; # Can't compare our list to nothing...
1379 if (@_ > 1) {
1380 Carp::croak "rule-based selection of single-item accessor not supported. Instead of single value, got @_";
1381 }
1382 unless (ref($self->{$plural_name}) eq 'ARRAY') {
1383 Carp::croak("${class_name}::$singular_name($_[0]): $plural_name does not contain an arrayref");
1384 }
1385 no warnings 'uninitialized';
1386 my @matches = grep { $_ eq $_[0] } @{ $self->{$plural_name} };
1387 return $matches[0] if @matches < 2;
1388 return $self->context_return(@matches);
1389 }
1390 };
1391 Sub::Install::reinstall_sub({
1392 into => $class_name,
1393 as => $singular_name,
1394 code => $single_accessor,
1395 });
1396  
1397 # return now for reverse_as but not is_many
1398 unless ($plural_name) {
1399 return;
1400 }
1401 }
1402  
1403 my $rule_name = $self->rule_accessor_name_for_is_many_accessor($plural_name);
1404 my $rule_accessor = Sub::Name::subname $class_name ."::$rule_name" => sub {
1405 my $self = shift;
1406 $rule_resolver->($self) unless ($rule_template);
1407 unless ($rule_template) {
1408 Carp::croak "No indirect rule available for locally-stored 'has-many' relationship";
1409 }
1410 if (@_) {
1411 my $tmp_rule = $rule_template->get_rule_for_values((map { $self->$_ } @property_names), @where_values);
1412 return $r_class_name->define_boolexpr($tmp_rule->params_list, @_);
1413 }
1414 else {
1415 return $rule_template->get_rule_for_values((map { $self->$_ } @property_names),@where_values);
1416 }
1417 };
1418  
1419 Sub::Install::reinstall_sub({
1420 into => $class_name,
1421 as => $rule_name,
1422 code => $rule_accessor,
1423 });
1424  
1425 my $list_accessor = Sub::Name::subname $class_name ."::$plural_name" => sub {
1426 my $self = shift;
1427 my $rule;
1428 $rule = $rule_resolver->($self) unless (defined $rule_template);
1429 if ($rule_template) {
1430 $rule = $rule_template->get_rule_for_values((map { $self->$_ } @property_names), @where_values) unless (defined $rule);
1431 if (@_) {
1432 return $UR::Context::current->query($r_class_name, $rule->params_list,@_);
1433 }
1434 else {
1435 return $UR::Context::current->query($r_class_name, $rule);
1436 }
1437 }
1438 else {
1439 if (@_) {
1440 if (@_ != 1) {
1441 Carp::croak "expected a single arrayref when setting a multi-value $class_name $plural_name! Got " . scalar(@_) . " args";
1442 } elsif ( ref($_[0]) ne 'ARRAY' ) {
1443 $self->{$plural_name} = [ $_[0] ];
1444 } else {
1445 $self->{$plural_name} = [ @{$_[0]} ];
1446 }
1447 return @{ $self->{$plural_name} };
1448 }
1449 else {
1450 return unless $self->{$plural_name};
1451 if (ref($self->{$plural_name}) ne 'ARRAY') {
1452 Carp::carp("$class_name with id ".$self->id." does not hold an arrayref in its $plural_name property");
1453 $self->{$plural_name} = [ $self->{$plural_name} ];
1454 }
1455 return @{ $self->{$plural_name} };
1456 }
1457 }
1458 };
1459  
1460 Sub::Install::reinstall_sub({
1461 into => $class_name,
1462 as => $plural_name,
1463 code => $list_accessor,
1464 });
1465  
1466 Sub::Install::reinstall_sub({
1467 into => $class_name,
1468 as => $singular_name . '_list',
1469 code => $list_accessor,
1470 });
1471  
1472 my $arrayref_name = $self->arrayref_accessor_name_for_is_many_accessor($plural_name);
1473 my $arrayref_accessor = Sub::Name::subname $class_name ."::$arrayref_name" => sub {
1474 return [ $list_accessor->(@_) ];
1475 };
1476  
1477 Sub::Install::reinstall_sub({
1478 into => $class_name,
1479 as => $arrayref_name,
1480 code => $arrayref_accessor,
1481 });
1482  
1483 my $iterator_name = $self->iterator_accessor_name_for_is_many_accessor($plural_name);
1484 my $iterator_accessor = Sub::Name::subname $class_name ."::$iterator_name" => sub {
1485 my $self = shift;
1486 my $rule;
1487 $rule = $rule_resolver->($self) unless (defined $rule_template);
1488 if ($rule_template) {
1489 $rule = $rule_template->get_rule_for_values((map { $self->$_ } @property_names), @where_values) unless (defined $rule);
1490 if (@_) {
1491 return $r_class_name->create_iterator($rule->params_list,@_);
1492 } else {
1493 return UR::Object::Iterator->create_for_filter_rule($rule);
1494 }
1495 }
1496 else {
1497 return UR::Value::Iterator->create_for_value_arrayref($self->{$plural_name} || []);
1498 }
1499 };
1500 Sub::Install::reinstall_sub({
1501 into => $class_name,
1502 as => $iterator_name,
1503 code => $iterator_accessor,
1504 });
1505  
1506 my $set_name = $self->set_accessor_name_for_is_many_accessor($plural_name);
1507 my $set_accessor = Sub::Name::subname $class_name ."::$set_name" => sub {
1508 my $self = shift;
1509 my $rule;
1510 $rule = $rule_resolver->($self) unless (defined $rule_template);
1511 if ($rule_template) {
1512 $rule = $rule_template->get_rule_for_values((map { $self->$_ } @property_names),@where_values) unless (defined $rule);
1513 return $r_class_name->define_set($rule->params_list,@_);
1514 }
1515 else {
1516 # this is a bit inside-out, but works for primitives
1517 my @members = $self->$plural_name;
1518 return UR::Value->define_set(id => \@members);
1519 }
1520 };
1521 Sub::Install::reinstall_sub({
1522 into => $class_name,
1523 as => $set_name,
1524 code => $set_accessor,
1525 });
1526  
1527 my $adder_method_name = $self->adder_name_for_is_many_accessor($plural_name);
1528 if ($class_name->can($adder_method_name)) {
1529 $adder_method_name = '__' . $adder_method_name;
1530 }
1531 my $adder_method = Sub::Name::subname $class_name . '::' . $adder_method_name => sub {
1532 # TODO: this handles only a single item when making objects: support a list of hashrefs
1533 my $self = shift;
1534 my $rule;
1535 $rule = $rule_resolver->($self) unless (defined $rule_template);
1536 if ($rule_template) {
1537 $params_prefix_resolver->() unless $params_prefix_resolved;
1538 unshift @_, @params_prefix if @_ == 1;
1539 $rule = $rule_template->get_rule_for_values((map { $self->$_ } @property_names), @where_values) unless (defined $rule);
1540 $r_class_name->create($rule->params_list,@_);
1541 }
1542 else {
1543 if ($r_class_meta) {
1544 my $obj;
1545 if (@_ == 1 and $_[0]->isa($r_class_name)) {
1546 $obj = $_[0];
1547 }
1548 else {
1549 $obj = $r_class_name->create(@where,@_);
1550 unless ($obj) {
1551 $self->error_message("Failed to add $singular_name:" . $r_class_name->error_message);
1552 return;
1553 }
1554 }
1555 push @{ $self->{$plural_name} ||= [] }, $obj;
1556 }
1557 else {
1558 if (@_ != 1) {
1559 Carp::croak "$class_name $adder_method_name expects a single value to add. Got " . scalar(@_) . " args";
1560 }
1561 push @{ $self->{$plural_name} ||= [] }, $_[0];
1562 return $_[0];
1563 }
1564 }
1565 };
1566 Sub::Install::reinstall_sub({
1567 into => $class_name,
1568 as => $adder_method_name,
1569 code => $adder_method,
1570 });
1571  
1572 my $remover_method_name = $self->remover_name_for_is_many_accessor($plural_name);
1573 if ($class_name->can($remover_method_name)) {
1574 $remover_method_name = '__' . $remover_method_name;
1575 }
1576 my $remover_method = Sub::Name::subname $class_name . '::' . $remover_method_name => sub {
1577 my $self = shift;
1578 my $rule;
1579 $rule = $rule_resolver->($self) unless (defined $rule_template);
1580 if ($rule_template) {
1581 # an id-linked "has-many"
1582 $rule = $rule_template->get_rule_for_values((map { $self->$_ } @property_names), @where_values) unless (defined $rule);
1583 $params_prefix_resolver->() unless $params_prefix_resolved;
1584 my @matches;
1585 if (@_ == 1 and ref($_[0])) {
1586 # the object to remove was passed-in
1587 unless ($rule->evaluate($_[0])) {
1588 Carp::croak "Object " . $_[0]->__display_name__ . " is not a member of the $singular_name set!";
1589 }
1590 @matches = ($_[0]);
1591 }
1592 else {
1593 # the parameters to find objects to remove were passed-in
1594 unshift @_, @params_prefix if @_ == 1; # a single "id" is the remainder of the id of the object
1595 @matches = $r_class_name->get($rule->params_list,@_);
1596 }
1597 my $trans = UR::Context::Transaction->begin;
1598 @matches = map {
1599 $_->delete or Carp::croak "Error deleting $r_class_name " . $_->id . " for $remover_method_name!: " . $_->error_message;
1600 } @matches;
1601 $trans->commit;
1602 return @matches;
1603 }
1604 else {
1605 # direct storage in an arrayref
1606 $self->{$plural_name} ||= [];
1607 if ($r_class_meta) {
1608 # object
1609 my @remove;
1610 my @keep;
1611 my $rule = $r_class_name->define_boolexpr(@_);
1612 for my $value (@{ $self->{$plural_name} }) {
1613 if ($rule->evaluate($value)) {
1614 push @keep, $value;
1615 }
1616 else {
1617 push @remove, $value;
1618 }
1619 }
1620 if (@remove) {
1621 @{ $self->{$plural_name} } = @keep;
1622 }
1623 return @remove;
1624 }
1625 else {
1626 # value (or non-ur object)
1627 if (@_ == 1) {
1628 # remove specific value
1629 my $removed;
1630 my $n = 0;
1631 for my $value (@{ $self->{$plural_name} }) {
1632 if ($value eq $_[0]) {
1633 $removed = splice(@{ $self->{$plural_name} }, $n, 1);
1634 Carp::croak("Internal object inconsistency removing value '$value'. Value '$removed' was removed instead!?") unless $removed eq $value;
1635 return $removed;
1636 }
1637 $n++;
1638 }
1639 Carp::croak("Failed to find item $_[0] in $class_name $plural_name. Object has " . scalar(@{$self->{$plural_name}}) . " values: ".join(', ', @{$self->{$plural_name}}));
1640 }
1641 elsif (@_ == 0) {
1642 # remove all if no params are specified
1643 @{ $self->{$plural_name} ||= [] } = ();
1644 }
1645 else {
1646 Carp::croak("$class_name $remover_method_name should be called with zero or one arg, got ".scalar(@_));
1647 }
1648 }
1649 }
1650 };
1651  
1652 # check here
1653 Sub::Install::reinstall_sub({
1654 into => $class_name,
1655 as => $remover_method_name,
1656 code => $remover_method,
1657 });
1658  
1659 }
1660  
1661 use Data::Dumper;
1662  
1663 sub initialize_direct_accessors {
1664 my $self = shift;
1665 my $class_name = $self->{class_name};
1666  
1667 my %id_property_names;
1668 for my $property_name (@{ $self->{id_by} }) {
1669 $id_property_names{$property_name} = 1;
1670 next if $property_name eq "id";
1671 }
1672  
1673 my %dimensions_by_fk;
1674 for my $property_name (sort keys %{ $self->{has} }) {
1675 my $property_data = $self->{has}{$property_name};
1676 if ($property_data->{is_dimension}) {
1677 my $id_by = $property_data->{id_by};
1678 unless ($id_by) {
1679 Carp::croak "No id_by specified for dimension $property_name?";
1680 }
1681 if (@$id_by != 1) {
1682 Carp::croak "The id_by specified for dimension $property_name must list a single property name!";
1683 }
1684  
1685 my $dimension_class_name = $property_data->{data_type};
1686 $dimensions_by_fk{$id_by->[0]} = $dimension_class_name;
1687  
1688 my $ref_class_meta = $dimension_class_name->__meta__;
1689 my %remote_id_properties = map { $_ => 1 } $ref_class_meta->id_property_names;
1690 my @non_id_properties = grep { not $remote_id_properties{$_} } $ref_class_meta->all_property_names;
1691 for my $expected_delegate_property_name (@non_id_properties) {
1692 unless ($self->{has}{$expected_delegate_property_name}) {
1693 $self->{has}{$expected_delegate_property_name} = {
1694 $self->_normalize_property_description(
1695 $expected_delegate_property_name,
1696 { via => $property_name, to => $expected_delegate_property_name, implied_by => $property_name }
1697 )
1698 }
1699 }
1700 }
1701 }
1702 }
1703  
1704 for my $pname (sort keys %{ $self->{has} }) {
1705 my $property_name = $pname; # mutable
1706 my $accessor_name = $pname;
1707  
1708 my $property_data = $self->{has}{$property_name};
1709  
1710 # handle aliases
1711 # the underlying property_name and data will change, though the accessor will not
1712 my $n = 0;
1713 while ($property_data->{via} and $property_data->{via} eq '__self__') {
1714 $property_name = $property_data->{to};
1715 $property_data = $self->{has}{$property_name};
1716 unless ($property_data) {
1717 Carp::confess("Property $accessor_name is an alias for $property_name, which does not exist!")
1718 }
1719 if ($n > 100) {
1720 Carp::confess("Deep recursion in property aliases behind $accessor_name!");
1721 }
1722 }
1723  
1724 my $column_name = $property_data->{column_name};
1725 my $is_transient = $property_data->{is_transient};
1726 my $where = $property_data->{where};
1727  
1728 do {
1729 # Handle the case where the software module has an explicit
1730 # override for one of the accessors.
1731 no strict 'refs';
1732 my $isa = \@{ $class_name . "::ISA" };
1733 my @old_isa = @$isa;
1734 @$isa = ();
1735 if ($class_name->can($accessor_name)) {
1736 #warn "property $class_name $accessor_name exists!";
1737 $accessor_name = "__$accessor_name";
1738 }
1739 @$isa = @old_isa;
1740 };
1741  
1742 unless ($accessor_name) {
1743 Carp::croak("No accessor name for property '$property_name' of class $class_name");
1744 }
1745  
1746 my $accessor_type;
1747 my @calculation_fields = (qw/calculate calc_perl calc_sql calculate_from/);
1748 if (my $id_by = $property_data->{id_by}) {
1749 my $r_class_name = $property_data->{data_type};
1750 #$self->mk_id_based_object_accessor($class_name, $accessor_name, $id_by, $r_class_name,$where);
1751 my $id_class_by = $property_data->{id_class_by};
1752 if ($property_data->{access_as} and $property_data->{access_as} eq 'auto') {
1753 $self->mk_id_based_flex_accessor($class_name, $accessor_name, $id_by, $r_class_name,$where, $id_class_by);
1754 $self->mk_id_based_object_accessor($class_name, $accessor_name . ($property_data->{is_many} ? '_objs' : '_obj'), $id_by, $r_class_name,$where, $id_class_by);
1755 }
1756 else {
1757 $self->mk_id_based_object_accessor($class_name, $accessor_name, $id_by, $r_class_name,$where, $id_class_by);
1758 }
1759 }
1760 elsif ($property_data->{'is_calculated'} and ! $property_data->{'is_mutable'}) {# and $property_data->{'column_name'}) {
1761 # For calculated + immutable properties, their calculation function is called
1762 # by UR::Context->create_entity(), which then stores the value in the object's
1763 # hash. So, the accessor just needs to pull the data like a regular r/o accessor
1764 #$self->mk_ro_accessor($class_name, $accessor_name, $property_data->{'column_name'});
1765 $self->mk_calculation_accessor(
1766 $class_name,
1767 $accessor_name,
1768 $property_data->{'calculate'},
1769 $property_data->{calculate_from},
1770 $property_data->{calculate_params},
1771 1, # the value should be cached
1772 $property_data->{'column_name'},
1773 );
1774 }
1775 elsif (my $via = $property_data->{via}) {
1776 my $to = $property_data->{to} || $property_data->{property_name};
1777 if ($via eq '__self__') {
1778 Carp::croak "aliases should be caught above!";
1779  
1780 }
1781 if ($property_data->{is_mutable}) {
1782 my $singular_name;
1783 if ($property_data->{'is_many'}) {
1784 require Lingua::EN::Inflect;
1785 $singular_name = Lingua::EN::Inflect::PL_V($accessor_name);
1786 }
1787 $self->mk_indirect_rw_accessor($class_name,$accessor_name,$via,$to,$where,$property_data->{'is_many'} && $singular_name, $property_name);
1788 }
1789 else {
1790 $self->mk_indirect_ro_accessor($class_name,$accessor_name,$via,$to,$where);
1791 }
1792 }
1793 elsif (my $calculate = $property_data->{calculate}) {
1794 $self->mk_calculation_accessor(
1795 $class_name,
1796 $accessor_name,
1797 $property_data->{calculate},
1798 $property_data->{calculate_from},
1799 $property_data->{calculate_params},
1800 $property_data->{is_constant},
1801 $property_data->{column_name},
1802 );
1803 }
1804 elsif (my $calculate_sql = $property_data->{'calculate_sql'}) {
1805 # The data gets filled in by the object loader behind the scenes.
1806 # To the user, it's a read-only property
1807 $self->mk_ro_accessor($class_name, $accessor_name, $calculate_sql);
1808  
1809 }
1810 elsif ($property_data->{is_many} or $property_data->{reverse_as}){
1811 my $reverse_as = $property_data->{reverse_as};
1812 my $r_class_name = $property_data->{data_type};
1813 my $singular_name;
1814 my $plural_name;
1815 if ($property_data->{is_many}) {
1816 $plural_name = $accessor_name;
1817 $singular_name = $self->singular_accessor_name_for_is_many_accessor($accessor_name);
1818 }
1819 else {
1820 $singular_name = $accessor_name;
1821 }
1822 $self->mk_object_set_accessors($class_name, $singular_name, $plural_name, $reverse_as, $r_class_name, $where);
1823 }
1824 elsif ($property_data->{'is_classwide'}) {
1825 my($value, $column_name, $is_transient, $calc_default)
1826 = @$property_data{'default_value','column_name','is_transient', 'calculated_default'};
1827 if ($property_data->{'is_constant'}) {
1828 $self->mk_ro_class_accessor($class_name,$accessor_name,$column_name,$value, $calc_default);
1829 } else {
1830 $self->mk_rw_class_accessor($class_name,$accessor_name,$column_name,$is_transient,$value, $calc_default);
1831 }
1832 }
1833 else {
1834 # Just use key/value pairs in the hash for normal
1835 # table stuff, and also non-database stuff.
1836  
1837 #if ($column_name) {
1838 # push @$props, $property_name;
1839 # push @$cols, $column_name;
1840 #}
1841  
1842 my $maker;
1843 if ($id_property_names{$property_name} or not $property_data->{is_mutable}) {
1844 $maker = 'mk_ro_accessor';
1845 }
1846 else {
1847 $maker = 'mk_rw_accessor';
1848 }
1849 $self->$maker($class_name, $accessor_name, $column_name, $property_name,$is_transient);
1850 }
1851 }
1852  
1853 # right now we just stomp on the default accessors constructed above where they are:
1854 # 1. the fk behind a dimensional relationships
1855 # 2. the indirect properties created for the dimensional relationship
1856 for my $dimension_id (keys %dimensions_by_fk) {
1857 my $dimension_class_name = $dimensions_by_fk{$dimension_id};
1858 my $ref_class_meta = $dimension_class_name->__meta__;
1859 my %remote_id_properties = map { $_ => 1 } $ref_class_meta->id_property_names;
1860 my @non_id_properties = grep { not $remote_id_properties{$_} } $ref_class_meta->all_property_names;
1861 for my $added_property_name (@non_id_properties) {
1862 $self->mk_dimension_delegate_accessors($dimension_id,$dimension_class_name, \@non_id_properties, $added_property_name);
1863 }
1864 $self->mk_dimension_identifying_accessor($dimension_id,$dimension_class_name, \@non_id_properties);
1865 }
1866  
1867 return 1;
1868 }
1869  
1870  
1871 1;
1872  
1873 =pod
1874  
1875 =head1 NAME
1876  
1877 UR::Object::Type::AccessorWriter - Helper module for UR::Object::Type responsible for creating accessors for properties
1878  
1879 =head1 DESCRIPTION
1880  
1881 Subroutines within this module actually live in the UR::Object::Type
1882 namespace; this module is just a convienent place to collect them. The
1883 class initializer uses these subroutines when it's time to create accessor
1884 methods for a newly defined class. Each accessor is implemented by a closure
1885 that is then assigned a name by Sub::Name and inserted into the defined
1886 class's namespace by Sub::Install.
1887  
1888 =head1 METHODS
1889  
1890 =over 4
1891  
1892 =item initialize_direct_accessors
1893  
1894 $classobj->initialize_direct_accessors();
1895  
1896 This is the entry point into the accessor writing system. It inspects each
1897 item in the 'has' key of the class object's hashref, and creates methods for
1898 each property.
1899  
1900 =item mk_rw_accessor
1901  
1902 $classobj->mk_rw_accessor($class_name, $accessor_name, $column_name, $property_name, $is_transient);
1903  
1904 Creates a mutable accessor named $accessor_name which stores its value in
1905 the $property_name key of the object's hashref.
1906  
1907 =item mk_ro_accessor
1908  
1909 $classobj->mk_ro_accessor($class_name, $accessor_name, $column_name, $property_name);
1910  
1911 Creates a read-only accessor named $accessor_name which retrieves its value
1912 in the $property_name key of the object's hashref. If the method is used
1913 as a mutator by passing in a value to the method, it will throw an exception
1914 with Carp::croak.
1915  
1916 =item mk_id_based_object_accessor
1917  
1918 $classobj->mk_id_based_object_accessor($class_name, $accessor_name, $id_by,
1919 $r_class_name, $where);
1920  
1921 Creates an object accessor named $accessor_name. It returns objects of type
1922 $r_class_name, id-ed by the parameters named in the $id_by arrayref. $where
1923 is an optional listref of additional filters to apply when retrieving
1924 objects.
1925  
1926 The behavior of the created accessor depends on the number of parameters
1927 passed to it. For 0 params, it retrieves the object pointed to by
1928 $r_class_name and $id_by. For 1 param, it looks up the ID param values
1929 of the passed-in object-parameter, and reassigns value stored in the $id_by
1930 properties of the acted-upon object, effectively acting as a mutator.
1931  
1932 For more than 1 param, the additional parameters are taken as
1933 properties/values to filter the returned objects on
1934  
1935 =item mk_indirect_ro_accessor
1936  
1937 $classobj->mk_indirect_ro_accessor($class_name, $accessor_name, $via, $to, $where);
1938  
1939 Creates a read-only via accessor named $accessor_name. Its value is
1940 obtained by calling the object accessor named $via, and then calling
1941 the method $to on that object. The optional $where listref is used
1942 as additional filters when calling $via.
1943  
1944 =item mk_indirect_rw_accessor
1945  
1946 $classobj->mk_indirect_rw_accessor($class_name, $accessor_name, $via, $to,
1947 $where, $singular_name);
1948  
1949 Creates a via accessor named $accessor_name that is able to change the
1950 property it points to with $to when called as a mutator. If the $to property
1951 on the remote object is an ID property of its class, it deletes the refered-to
1952 object and creates a new one with the appropriate properties. Otherwise, it
1953 updates the $to property on the refered-to object.
1954  
1955 =item mk_calculation_accessor
1956  
1957 $classobj->mk_calculation_accessor($class_name, $accessor_name, $calculation_src,
1958 $calculate_from, $params, $is_constant, $column_name);
1959  
1960 Creates a calculated accessor called $accessor_name. If the $is_constant
1961 flag is true, then the accessor runs the calculation once, caches the result,
1962 and returns that result for subsequent calls to the accessor.
1963  
1964 $calculation_src can be one of: coderef, string containing Perl code, or
1965 the name of a module under UR::Object::Type::AccessorWriter which has a
1966 method called C<calculate>. If $calculation_src is empty, then $accessor_name
1967 must be the name of an already-existing subroutine in the class's namespace.
1968  
1969 =item mk_dimension_delegate_accessors
1970  
1971 =item mk_dimension_identifying_accessor
1972  
1973 These create accessors for dealing with dimension tables in OLAP-type schemas.
1974 They need more documentation.
1975  
1976 =item mk_rw_class_accessor
1977  
1978 $classobj->mk_rw_class_accessor($class_name, $accessor_name, $column_name, $is_transient, $variable_value);
1979  
1980 Creates a read-write accessor called $accessor_name which stores its value
1981 in a scalar captured by the accessor's closure. Since the closure is
1982 inserted into the class's namespace, all instances of the class share the
1983 same closure (and therefore the same scalar), and the property effectively
1984 acts as a class-wide property.
1985  
1986 =item mk_ro_class_accessor
1987  
1988 $classobj->mk_ro_class_accessor($class_name, $accessor_name, $column_name, $variable_value);
1989  
1990 Creates a read-only accessor called $accessor_name which retrieves its value
1991 from a scalar captured by the accessor's closure. The value is initialized
1992 to $variable_value. If called as a mutator, it throws an exception through
1993 Carp::croak
1994  
1995 =back
1996  
1997 =head1 SEE ALSO
1998  
1999 UR::Object::Type::AccessorWriter, UR::Object::Type
2000  
2001 =cut