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

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 173 SCAR 56.1
CC
16
Cov
0%
_properties
90.0
_legacy_properties
30.0
property
12.0
CRAP 272.0
1 package UR::Object::Type;
2  
3 use warnings;
4 use strict;
5 require UR;
6  
7 # Used during bootstrapping.
8 our @ISA = qw(UR::Object);
9 our $VERSION = "0.46"; # UR $VERSION;;
10  
11 our @CARP_NOT = qw( UR::Object UR::Context UR::ModuleLoader Class::Autouse UR::BoolExpr );
12  
13 # Most of the API for this module are legacy internals required by UR.
14 use UR::Object::Type::InternalAPI;
15  
16 # This module implements define(), and most everything behind it.
17 use UR::Object::Type::Initializer;
18  
19 # The methods used by the initializer to write accessors in perl.
20 use UR::Object::Type::AccessorWriter;
21  
22 # The methods to extract/(re)create definition text in the module source file.
23 use UR::Object::Type::ModuleWriter;
24  
25 # Present the internal definer as an external method
26 sub define { shift->__define__(@_) }
27  
28 # For efficiency, certain hash keys inside the class cache property metadata
29 # These go in this array, and are cleared when property metadata is mutated
30 our @cache_keys;
31  
32 # This is the function behind $class_meta->properties(...)
33 # It mimics the has-many object accessor, but handles inheritance
34 # Once we have "isa" and "is-parent-of" operator we can do this with regular operators.
35 push @cache_keys, '_properties';
36 sub _properties {
37 my $self = shift;
38 my $all = $self->{_properties} ||= do {
39 # start with everything, as it's a small list
40 my $map = $self->_property_name_class_map;
41 my @all;
42 for my $property_name (sort keys %$map) {
43 my $class_names = $map->{$property_name};
44 my $class_name = $class_names->[0];
45 my $id = $class_name . "\t" . $property_name;
46 my $property_meta = UR::Object::Property->get($id);
47 unless ($property_meta) {
48 Carp::confess("Failed to find property meta for $class_name $property_name?");
49 }
50 push @all, $property_meta;
51 }
52 \@all;
53 };
54 if (@_) {
55 my ($bx, %extra) = UR::Object::Property->define_boolexpr(@_);
56 my @matches = grep { $bx->evaluate($_) } @$all;
57 if (%extra) {
58 # Additional meta-properties on meta-properties are not queryable until we
59 # put the UR::Object::Property into a private sub-class.
60 # This will give us most of the functionality.
61 for my $key (keys %extra) {
62 my ($name,$op) = ($key =~ /(\w+)\s*(.*)/);
63 unless (defined $self->{attributes_have}->{$name}) {
64 die "unknown property $name used to query properties of " . $self->class_name;
65 }
66 if ($op and $op ne '==' and $op ne 'eq') {
67 die "operations besides equals are not supported currently for added meta-properties like $name on class " . $self->class_name;
68 }
69 my $value = $extra{$key};
70 no warnings;
71 @matches = grep { $_->can($name) and $_->$name eq $value } @matches;
72 }
73 }
74 return if not defined wantarray;
75 return @matches if wantarray;
76 die "Matched multiple meta-properties, but called in scalar context!" . Data::Dumper::Dumper(\@matches) if @matches > 1;
77 return $matches[0];
78 }
79 else {
80 @$all;
81 }
82 }
83  
84 sub property {
85 if (@_ == 2) {
86 # optimize for the common case
87 my ($self, $property_name) = @_;
88 my $class_names = $self->_property_name_class_map->{$property_name};
89 return unless $class_names and @$class_names;
90 my $id = $class_names->[0] . "\t" . $property_name;
91 return UR::Object::Property->get($id);
92 }
93 else {
94 # this forces scalar context, raising an exception if
95 # the params used result in more than one match
96 my $one = shift->properties(@_);
97 return $one;
98 }
99 }
100  
101 push @cache_keys, '_property_names';
102 sub property_names {
103 my $self = $_[0];
104 my $names = $self->{_property_names} ||= do {
105 my @names = sort keys %{ shift->_property_name_class_map };
106 \@names;
107 };
108 return @$names;
109 }
110  
111 push @cache_keys, '_property_name_class_map';
112 sub _property_name_class_map {
113 my $self = shift;
114 my $map = $self->{_property_name_class_map} ||= do {
115 my %map = ();
116 for my $class_name ($self->class_name, $self->ancestry_class_names) {
117 my $class_meta = UR::Object::Type->get($class_name);
118 if (my $has = $class_meta->{has}) {
119 for my $key (sort keys %$has) {
120 my $classes = $map{$key} ||= [];
121 push @$classes, $class_name;
122 }
123 }
124 }
125 \%map;
126 };
127 return $map;
128 }
129  
130 # The prior implementation of _properties() (behind ->properties())
131 # filtered out certain property meta. This is the old version.
132 # The new version above will return one object per property name in
133 # the meta ancestry.
134 sub _legacy_properties {
135 my $self = shift;
136 if (@_) {
137 my $bx = UR::Object::Property->define_boolexpr(@_);
138 my @matches = grep { $bx->evaluate($_) } $self->property_metas;
139 return if not defined wantarray;
140 return @matches if wantarray;
141 die "Matched multiple meta-properties, but called in scalar context!" . Data::Dumper::Dumper(\@matches) if @matches > 1;
142 return $matches[0];
143 }
144 else {
145 $self->property_metas;
146 }
147 }
148  
149 1;
150