| 1 | |||
| 2 | package UR::Singleton; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | require UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | UR::Object::Type->define( | ||
| 10 | class_name => 'UR::Singleton', | ||
| 11 | is => ['UR::Object'], | ||
| 12 | is_abstract => 1, | ||
| 13 | ); | ||
| 14 | |||
| 15 | sub id { | ||
| 16 | my $self = shift; | ||
| 17 | return (ref $self ? $self->SUPER::id(@_) : $self); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub _init_subclass { | ||
| 21 | my $class_name = shift; | ||
| 22 | 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 | my $src; | ||
| 28 | if ($class_meta_object->is_abstract) { | ||
| 29 | $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 | $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 | eval $src; | ||
| 49 | Carp::confess($@) if $@; | ||
| 50 | |||
| 51 | 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 | sub _abstract_load { | ||
| 59 | my $class = shift; | ||
| 60 | my $bx = $class->define_boolexpr(@_); | ||
| 61 | my $id = $bx->value_for_id; | ||
| 62 | unless (defined $id) { | ||
| 63 | use Data::Dumper; | ||
| 64 | my $params = { $bx->params_list }; | ||
| 65 | Carp::confess("Cannot load a singleton ($class) except by specific identity. " . Dumper($params)); | ||
| 66 | } | ||
| 67 | my $subclass_name = $class->_resolve_subclass_name_for_id($id); | ||
| 68 | eval "use $subclass_name"; | ||
| 69 | if ($@) { | ||
| 70 | undef $@; | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | 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 | sub _concrete_get { | ||
| 80 | if (@_ == 1 or (@_ == 2 and $_[0] eq $_[1])) { | ||
| 81 | my $self = $_[0]->_singleton_object; | ||
| 82 | return $self if $self; | ||
| 83 | } | ||
| 84 | return shift->_concrete_load(@_); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _concrete_is_loaded { | ||
| 88 | if (@_ == 1 or (@_ == 2 and $_[0] eq $_[1])) { | ||
| 89 | |||
| 90 | my $self = $_[0]->_singleton_object; | ||
| 91 | return $self if $self; | ||
| 92 | } | ||
| 93 | return shift->SUPER::is_loaded(@_); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _concrete_load { | ||
| 97 | my $class = shift; | ||
| 98 | |||
| 99 | $class = ref($class) || $class; | ||
| 100 | no strict 'refs'; | ||
| 101 | my $varref = \${ $class . "::singleton" }; | ||
| 102 | unless ($$varref) { | ||
| 103 | my $id = $class->_resolve_id_for_subclass_name($class); | ||
| 104 | |||
| 105 | my $class_object = $class->__meta__; | ||
| 106 | my @prop_names = $class_object->all_property_names; | ||
| 107 | my %default_values; | ||
| 108 | foreach my $prop_name ( @prop_names ) { | ||
| 109 | my $prop = $class_object->property_meta_for_name($prop_name); | ||
| 110 | next unless $prop; | ||
| 111 | my $val = $prop->{'default_value'}; | ||
| 112 | next unless defined $val; | ||
| 113 | $default_values{$prop_name} = $val; | ||
| 114 | } | ||
| 115 | |||
| 116 | $$varref = $UR::Context::current->_construct_object($class,%default_values, id => $id); | ||
| 117 | $$varref->{db_committed} = { %$$varref }; | ||
| 118 | $$varref->__signal_change__("load"); | ||
| 119 | Scalar::Util::weaken($$varref); | ||
| 120 | } | ||
| 121 | my $self = $class->_concrete_is_loaded(@_); | ||
| 122 | return unless $self; | ||
| 123 | unless ($self->init) { | ||
| 124 | Carp::confess("Failed to initialize singleton $class!"); | ||
| 125 | } | ||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | # This is implemented in the singleton to do any post-load processing. | ||
| 130 | |||
| 131 | sub init { | ||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | # All singletons require special deletion logic since they keep a | ||
| 136 | #weakened reference to the singleton. | ||
| 137 | |||
| 138 | sub delete { | ||
| 139 | my $self = shift; | ||
| 140 | my $class = $self->class; | ||
| 141 | $self->SUPER::delete(); | ||
| 142 | no strict 'refs'; | ||
| 143 | ${ $class . "::singleton" } = undef if ${ $class . "::singleton" } eq $self; | ||
| 144 | return $self; | ||
| 145 | } | ||
| 146 | |||
| 147 | # In most cases, the id is the class name itself, but this is not necessary. | ||
| 148 | |||
| 149 | sub _resolve_subclass_name_for_id { | ||
| 150 | my $class = shift; | ||
| 151 | my $id = shift; | ||
| 152 | return $id; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub _resolve_id_for_subclass_name { | ||
| 156 | my $class = shift; | ||
| 157 | my $subclass_name = shift; | ||
| 158 | return $subclass_name; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub create { | ||
| 162 | my $class = shift; | ||
| 163 | my $bx = $class->define_boolexpr(@_); | ||
| 164 | my $id = $bx->value_for_id; | ||
| 165 | unless (defined $id) { | ||
| 166 | Carp::confess("No singleton ID class specified for constructor?"); | ||
| 167 | } | ||
| 168 | my $subclass = $class->_resolve_subclass_name_for_id($id); | ||
| 169 | eval "use $subclass"; | ||
| 170 | unless ($subclass->isa(__PACKAGE__)) { | ||
| 171 | eval '@' . $subclass . "::ISA = ('" . __PACKAGE__ . "')"; | ||
| 172 | } | ||
| 173 | |||
| 174 | return $subclass->_concrete_get(); | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | 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 |