lib/UR/Singleton.pm

filter: Statementstmt 19.3% 19 / 98 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 31.2% 5 / 16 Podpod 75.0% 3 / 4 totaltotal 15.4% 27 / 175 CC 24 SCAR 31.4
CC
24
Cov
13%
_concrete_load
58.7
_concrete_get
30.0
_concrete_is_loaded
30.0
CRAP 23.0
« blib/lib/UR/Namespace/Command/Update/TabCompletionSpec.pm ↑ Summary blib/lib/UR/Value.pm »
1  
2 package UR::Singleton;
3  
4 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 266 require UR;
7 266 our $VERSION = "0.47"; # UR $VERSION;
8  
9 266 UR::Object::Type->define(
10 class_name => 'UR::Singleton',
11 is => ['UR::Object'],
12 is_abstract => 1,
13 );
14  
15 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
16 0 my $self = shift;
17 0 ▶ return (ref $self ? $self->SUPER::id(@_) : $self);
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
18 }
19  
20 0 ▶sub _init_subclass {
Subroutine _init_subclass: not called CC=3 SCAR=24.8
21 0 my $class_name = shift;
22 0 my $class_meta_object = $class_name->__meta__;
23  
24 # Write into the class's namespace the correct singleton overrides
25 # to standard UR::Object methods.
26
27 0 my $src;
28 0 ▶ if ($class_meta_object->is_abstract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_meta_object->is_abstract) { } 0 0 0
29 0 $src = qq|sub ${class_name}::_singleton_object { Carp::confess("${class_name} is an abstract singleton! Select a concrete sub-class.") }|
30 . "\n"
31 . qq|sub ${class_name}::_singleton_class_name { Carp::confess("${class_name} is an abstract singleton! Select a concrete sub-class.") }|
32 . "\n"
33 . qq|sub ${class_name}::_load { shift->_abstract_load(\@_) }|
34 }
35 else {
36 0 $src = qq|sub ${class_name}::_singleton_object { \$${class_name}::singleton or shift->_concrete_load() }|
37 . "\n"
38 . qq|sub ${class_name}::_singleton_class_name { '${class_name}' }|
39 . "\n"
40 . qq|sub ${class_name}::_load { shift->_concrete_load(\@_) }|
41 . "\n"
42 . qq|sub ${class_name}::get { shift->_concrete_get(\@_) }|
43 . "\n"
44 . qq|sub ${class_name}::is_loaded { shift->_concrete_is_loaded(\@_) }|
45 ;
46 }
47
48 0 eval $src;
49 0 ▶ Carp::confess($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
50  
51 0 return 1;
52 }
53  
54 # Abstract singletons havd a different load() method than concrete ones.
55 # We could do this with forking logic, but since many of the concrete methods
56 # get non-default handling, it's more efficient to do it this way.
57  
58 0 ▶sub _abstract_load {
Subroutine _abstract_load: not called CC=3 SCAR=21.3
59 0 my $class = shift;
60 0 my $bx = $class->define_boolexpr(@_);
61 0 my $id = $bx->value_for_id;
62 0 ▶ unless (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
63 266 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 0 my $params = { $bx->params_list };
65 0 Carp::confess("Cannot load a singleton ($class) except by specific identity. " . Dumper($params));
66 }
67 0 my $subclass_name = $class->_resolve_subclass_name_for_id($id);
68 0 eval "use $subclass_name";
69 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
70 0 undef $@;
71 0 return;
72 }
73 0 return $subclass_name->get();
74 }
75  
76 # Concrete singletons have overrides to the most basic acccessors to
77 # accomplish class/object duality smoothly.
78  
79 0 ▶sub _concrete_get {
Subroutine _concrete_get: not called CC=5 SCAR=34.0
80 0 ▶ if (@_ == 1 or (@_ == 2 and $_[0] eq $_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 or @_ == 2 and $_[0] eq $_[1]) 0 0 0
Condition0%0/6
@_ == 1 or @_ == 2 and $_[0] eq $_[1]
l!l&&r!l&&!r
000
@_ == 2 and $_[0] eq $_[1]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 or @_ == 2 and $_[0] eq $_[1]
@_ == 1 @_ == 2 $_[0] eq $_[1]
Truth table0%0/4
A or B and C
A= @_ == 1B= @_ == 2C= $_[0] eq $_[1]
ABCresult
1XX1
0111
00X0
0100
81 0 my $self = $_[0]->_singleton_object;
82 0 ▶ return $self if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
83 }
84 0 return shift->_concrete_load(@_);
85 }
86  
87 0 ▶sub _concrete_is_loaded {
Subroutine _concrete_is_loaded: not called CC=5 SCAR=34.0
88 0 ▶ if (@_ == 1 or (@_ == 2 and $_[0] eq $_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 or @_ == 2 and $_[0] eq $_[1]) 0 0 0
Condition0%0/6
@_ == 1 or @_ == 2 and $_[0] eq $_[1]
l!l&&r!l&&!r
000
@_ == 2 and $_[0] eq $_[1]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 or @_ == 2 and $_[0] eq $_[1]
@_ == 1 @_ == 2 $_[0] eq $_[1]
Truth table0%0/4
A or B and C
A= @_ == 1B= @_ == 2C= $_[0] eq $_[1]
ABCresult
1XX1
0111
00X0
0100
89
90 0 my $self = $_[0]->_singleton_object;
91 0 ▶ return $self if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
92 }
93 0 return shift->SUPER::is_loaded(@_);
94 }
95  
96 0 ▶sub _concrete_load {
Subroutine _concrete_load: not called CC=8 SCAR=40.7
97 0 my $class = shift;
98  
99 0 ▶ $class = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
100 266 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 0 my $varref = \${ $class . "::singleton" };
102 0 ▶ unless ($$varref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$varref) 0 0 0
103 0 my $id = $class->_resolve_id_for_subclass_name($class);
104  
105 0 my $class_object = $class->__meta__;
106 0 my @prop_names = $class_object->all_property_names;
107 0 my %default_values;
108 0 foreach my $prop_name ( @prop_names ) {
109 0 my $prop = $class_object->property_meta_for_name($prop_name);
110 0 ▶ next unless $prop;
Branch: 1 branch
BranchTrueFalseTotal
unless $prop 0 0 0
111 0 my $val = $prop->{'default_value'};
112 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
113 0 $default_values{$prop_name} = $val;
114 }
115
116 0 $$varref = $UR::Context::current->_construct_object($class,%default_values, id => $id);
117 0 $$varref->{db_committed} = { %$$varref };
118 0 $$varref->__signal_change__("load");
119 0 Scalar::Util::weaken($$varref);
120 }
121 0 my $self = $class->_concrete_is_loaded(@_);
122 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
123 0 ▶ unless ($self->init) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->init) 0 0 0
124 0 Carp::confess("Failed to initialize singleton $class!");
125 }
126 0 return $self;
127 }
128  
129 # This is implemented in the singleton to do any post-load processing.
130  
131 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
132 0 return 1;
133 }
134  
135 # All singletons require special deletion logic since they keep a
136 #weakened reference to the singleton.
137  
138 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=13.0
Pod documented
139 0 my $self = shift;
140 0 my $class = $self->class;
141 0 $self->SUPER::delete();
142 266 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
143 0 ▶ ${ $class . "::singleton" } = undef if ${ $class . "::singleton" } eq $self;
Branch: 1 branch
BranchTrueFalseTotal
if ${$class . '::singleton';} eq $self 0 0 0
144 0 return $self;
145 }
146  
147 # In most cases, the id is the class name itself, but this is not necessary.
148  
149 0 ▶sub _resolve_subclass_name_for_id {
Subroutine _resolve_subclass_name_for_id: not called CC=1 SCAR=6.9
150 0 my $class = shift;
151 0 my $id = shift;
152 0 return $id;
153 }
154  
155 0 ▶sub _resolve_id_for_subclass_name {
Subroutine _resolve_id_for_subclass_name: not called CC=1 SCAR=6.9
156 0 my $class = shift;
157 0 my $subclass_name = shift;
158 0 return $subclass_name;
159 }
160  
161 0 ▶sub create {
Subroutine create: not called CC=3 SCAR=24.8
Pod documented
162 0 my $class = shift;
163 0 my $bx = $class->define_boolexpr(@_);
164 0 my $id = $bx->value_for_id;
165 0 ▶ unless (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
166 0 Carp::confess("No singleton ID class specified for constructor?");
167 }
168 0 my $subclass = $class->_resolve_subclass_name_for_id($id);
169 0 eval "use $subclass";
170 0 ▶ unless ($subclass->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subclass->isa(__PACKAGE__)) 0 0 0
171 0 eval '@' . $subclass . "::ISA = ('" . __PACKAGE__ . "')";
172 }
173
174 0 return $subclass->_concrete_get();
175 }
176  
177  
178 266 1;
179  
180  
181 =pod
182  
183 =head1 NAME
184  
185 UR::Singleton - Abstract class for implementing singleton objects
186  
187 =head1 SYNOPSIS
188  
189 package MyApp::SomeClass;
190 use UR;
191 class MyApp::SomeClass {
192 is => 'UR::Singleton',
193 has => [
194 foo => { is => 'Number' },
195 ]
196 };
197  
198 $obj = MyApp::SomeClass->get();
199 $obj->foo(1);
200  
201 =head1 DESCRIPTION
202  
203 This class provides the infrastructure for singleton classes. Singletons
204 are classes of which there can only be one instance, and that instance's ID
205 is the class name.
206  
207 If a class inherits from UR::Singleton, it overrides the default
208 implementation of C<get()> and C<is_loaded()> in UR::Object with code that
209 fabricates an appropriate object the first time it's needed.
210  
211 Singletons are most often used as one of the parent classes for data sources
212 within a Namespace. This makes it convienent to refer to them using only
213 their name, as in a class definition.
214  
215 =head1 METHODS
216  
217 =over 4
218  
219 =item _singleton_object
220  
221 $obj = Class::Name->_singleton_object;
222  
223 $obj = $obj->_singleton_object;
224  
225 Returns the object instance whether it is called as a class or object method.
226  
227 =item _singleton_class_name
228  
229 $class_name = Class::Name->_singleton_class_name;
230  
231 $class_name = $obj->_singleton_class_name;
232  
233 Returns the class name whether it is called as a class or object method.
234  
235 =back
236  
237 =head1 SEE ALSO
238  
239 UR::Object
240  
241 =cut
« blib/lib/UR/Namespace/Command/Update/TabCompletionSpec.pm ↑ Summary blib/lib/UR/Value.pm »