| 1 | package UR::Value; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | require UR; | ||
| 7 | |||
| 8 | use List::MoreUtils; | ||
| 9 | |||
| 10 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 11 | |||
| 12 | our @CARP_NOT = qw( UR::Context ); | ||
| 13 | |||
| 14 | UR::Object::Type->define( | ||
| 15 | class_name => 'UR::Value', | ||
| 16 | is => 'UR::Object', | ||
| 17 | has => ['id'], | ||
| 18 | data_source => 'UR::DataSource::Default', | ||
| 19 | ); | ||
| 20 | |||
| 21 | sub __display_name__ { | ||
| 22 | return shift->id; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub __load__ { | ||
| 26 | my $class = shift; | ||
| 27 | my $rule = shift; | ||
| 28 | my $expected_headers = shift; | ||
| 29 | |||
| 30 | my $class_meta = $class->__meta__; | ||
| 31 | unless ($class_meta->{_value_loader}) { | ||
| 32 | my @id_property_names = $class_meta->all_id_property_names; | ||
| 33 | my %id_property_names = map { $_ => 1 } @id_property_names; | ||
| 34 | |||
| 35 | my $loader = sub { | ||
| 36 | my $bx = shift; | ||
| 37 | my $id = $bx->value_for_id; | ||
| 38 | unless (defined $id) { | ||
| 39 | Carp::croak "Can't load an infinite set of " | ||
| 40 | . $bx->subject_class_name | ||
| 41 | . ". Some id properties were not specified in the rule $bx"; | ||
| 42 | } | ||
| 43 | my @rows; | ||
| 44 | if (ref($id) and ref($id) eq 'ARRAY') { | ||
| 45 | # Multiple IDs passed in - return rows for multiple objects | ||
| 46 | my @non_id = grep { ! $id_property_names{$_} } $bx->template->_property_names; | ||
| 47 | if (@non_id) { | ||
| 48 | Carp::croak("Cannot load class " | ||
| 49 | . $bx->subject_class_name | ||
| 50 | . " via UR::DataSource::Default when 'id' is a listref and non-id" | ||
| 51 | . " properties appear in the rule: " | ||
| 52 | . join(', ', @non_id)); | ||
| 53 | } | ||
| 54 | |||
| 55 | # Get the 1st value from each list, then the second, then the third, etc | ||
| 56 | my $iter = List::MoreUtils::each_arrayref | ||
| 57 | map { | ||
| 58 | my $v = $bx->value_for($_); | ||
| 59 | (ref($v) eq 'ARRAY') | ||
| 60 | ? $v | ||
| 61 | : [ $v ] | ||
| 62 | } | ||
| 63 | @$expected_headers; | ||
| 64 | while(my @row = $iter->()) { | ||
| 65 | push @rows, \@row; | ||
| 66 | } | ||
| 67 | |||
| 68 | } else { | ||
| 69 | # single ID - return a single row | ||
| 70 | my @row = map { $bx->value_for($_) } @$expected_headers; | ||
| 71 | @rows = ( \@row ); | ||
| 72 | } | ||
| 73 | return ($expected_headers, \@rows); | ||
| 74 | }; | ||
| 75 | |||
| 76 | $class_meta->{_value_loader} = $loader; | ||
| 77 | } | ||
| 78 | |||
| 79 | return $class_meta->{_value_loader}->($rule); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub underlying_data_types { | ||
| 83 | return (); | ||
| 84 | } | ||
| 85 | |||
| 86 | package UR::Value::Type; | ||
| 87 | |||
| 88 | sub get_composite_id_decomposer { | ||
| 89 | my $class_meta = shift; | ||
| 90 | |||
| 91 | unless ($class_meta->{get_composite_id_decomposer}) { | ||
| 92 | my @id_property_names = $class_meta->id_property_names; | ||
| 93 | my $instance_class = $class_meta->class_name; | ||
| 94 | if (my $decomposer = $instance_class->can('__deserialize_id__')) { | ||
| 95 | $class_meta->{get_composite_id_decomposer} = sub { | ||
| 96 | my @ids = (ref($_[0]) and ref($_[0]) eq 'ARRAY') | ||
| 97 | ? @{$_[0]} | ||
| 98 | : ( $_[0] ); | ||
| 99 | my @retval; | ||
| 100 | if (@ids == 1) { | ||
| 101 | my $h = $instance_class->$decomposer($ids[0]); | ||
| 102 | @retval = @$h{@id_property_names}; | ||
| 103 | |||
| 104 | } else { | ||
| 105 | # Get the 1st value from each list, then the second, then the third, etc | ||
| 106 | my @decomposed = map { | ||
| 107 | my $h = $instance_class->$decomposer($_); | ||
| 108 | [ @$h{@id_property_names} ] | ||
| 109 | } | ||
| 110 | @ids; | ||
| 111 | my $iter = List::MoreUtils::each_arrayref @decomposed; | ||
| 112 | |||
| 113 | while( my @row = $iter->() ) { | ||
| 114 | push @retval, \@row; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | return @retval; | ||
| 118 | }; | ||
| 119 | |||
| 120 | } else { | ||
| 121 | $decomposer = $class_meta->SUPER::get_composite_id_decomposer(); | ||
| 122 | $class_meta->{get_composite_id_decomposer} = $decomposer; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | return $class_meta->{get_composite_id_decomposer}; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub get_composite_id_resolver { | ||
| 129 | my $class_meta = shift; | ||
| 130 | unless ($class_meta->{get_composite_id_resolver}) { | ||
| 131 | my @id_property_names = $class_meta->id_property_names; | ||
| 132 | my $instance_class = $class_meta->class_name; | ||
| 133 | if (my $resolver = $instance_class->can('__serialize_id__')) { | ||
| 134 | $class_meta->{get_composite_id_resolver} = sub { | ||
| 135 | my %h = map { $_ => shift } @id_property_names; | ||
| 136 | return $instance_class->__serialize_id__(\%h); | ||
| 137 | }; | ||
| 138 | |||
| 139 | } else { | ||
| 140 | $resolver = $class_meta->SUPER::get_composite_id_resolver(); | ||
| 141 | $class_meta->{get_composite_id_resolver} = $resolver; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | return $class_meta->{get_composite_id_resolver}; | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | 1; |