| 1 | package UR::Observer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 266 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 266 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 266 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 266 | require UR; | |||||||||||||||||||||||||||
| 8 | 266 | require UR::Context::Transaction; | |||||||||||||||||||||||||||
| 9 | }; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 266 | our $VERSION = "0.47"; # UR $VERSION; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 266 | UR::Object::Type->define( | |||||||||||||||||||||||||||
| 14 | class_name => __PACKAGE__, | ||||||||||||||||||||||||||||
| 15 | has => [ | ||||||||||||||||||||||||||||
| 16 | subject_class_name => { is => 'Text', is_optional => 1, default_value => 'UR::Object' }, | ||||||||||||||||||||||||||||
| 17 | subject_id => { is => 'SCALAR', is_optional => 1, default_value => '' }, | ||||||||||||||||||||||||||||
| 18 | aspect => { is => 'String', is_optional => 1, default_value => '' }, | ||||||||||||||||||||||||||||
| 19 | priority => { is => 'Number', is_optional => 1, default_value => 1 }, | ||||||||||||||||||||||||||||
| 20 | note => { is => 'String', is_optional => 1, default_value => '' }, | ||||||||||||||||||||||||||||
| 21 | once => { is => 'Boolean', is_optional => 1, default_value => 0 }, | ||||||||||||||||||||||||||||
| 22 | subject => { is => 'UR::Object', id_by => 'subject_id', id_class_by => 'subject_class_name' }, | ||||||||||||||||||||||||||||
| 23 | ], | ||||||||||||||||||||||||||||
| 24 | is_transactional => 1, | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # This is not implemented as a "real" observer via create() because at the point during bootstrapping | ||||||||||||||||||||||||||||
| 28 | # that this module is loaded, we're not yet ready to start creating objects | ||||||||||||||||||||||||||||
| 29 | 266 | __PACKAGE__->_insert_record_into_all_change_subscriptions('UR::Observer', 'priority', '', | |||||||||||||||||||||||||||
| 30 | [\&_modify_priority, '', 0, UR::Object::Type->autogenerate_new_object_id_uuid]); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub create { | ||||||||||||||||||||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | $class->_create_or_define('create', @_); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub __define__ { | ||||||||||||||||||||||||||
|
Subroutine
__define__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | $class->_create_or_define('__define__', @_); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 266 | my @required_params_for_register = qw(aspect callback note once priority subject_class_name subject_id id); | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub required_params_for_register { @required_params_for_register } | ||||||||||||||||||||||||||
|
Subroutine
required_params_for_register: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub _create_or_define { | ||||||||||||||||||||||||||
|
Subroutine
_create_or_define: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 49 | 0 | my $method = shift; | |||||||||||||||||||||||||||
| 50 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | my $callback = delete $params{callback}; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $self = UR::Context::Transaction::do { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 55 | 0 | my $inner_self; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($method eq 'create') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | 0 | $inner_self = $class->SUPER::create(%params); | |||||||||||||||||||||||||||
| 58 | } elsif ($method eq '__define__') { | ||||||||||||||||||||||||||||
| 59 | 0 | $inner_self = $class->SUPER::__define__(%params); | |||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||
| 61 | 0 | Carp::croak('Instantiating a UR::Observer with some method other than create() or __define__() is not supported'); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | $inner_self->{callback} = $callback; | |||||||||||||||||||||||||||
| 64 | 0 | $inner_self->register_callback(map { $_ => $inner_self->$_ } @required_params_for_register); | |||||||||||||||||||||||||||
| 65 | 0 | return $inner_self; | |||||||||||||||||||||||||||
| 66 | 0 | }; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | return $self; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 266 | my @has_defaults = qw(aspect note once priority subject_class_name subject_id); | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub has_defaults { @has_defaults } | ||||||||||||||||||||||||||
|
Subroutine
has_defaults: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | my %defaults = | ||||||||||||||||||||||||||||
| 77 | map { | ||||||||||||||||||||||||||||
| 78 | $_ => __PACKAGE__->__meta__->{has}->{$_}->{default_value} | ||||||||||||||||||||||||||||
| 79 | 1596 | } | |||||||||||||||||||||||||||
| 80 | grep { | ||||||||||||||||||||||||||||
| 81 | 266 | exists __PACKAGE__->__meta__->{has}->{$_} | |||||||||||||||||||||||||||
| 82 | && exists __PACKAGE__->__meta__->{has}->{$_}->{default_value} | ||||||||||||||||||||||||||||
| 83 | 1596 | ▶ | } @has_defaults; | ||||||||||||||||||||||||||
|
Condition33%1/3
exists __PACKAGE__->__meta__->{'has'}{$_} && exists __PACKAGE__->__meta__->{'has'}{$_}{'default_value'}
MC/DC0%0/2
exists __PACKAGE__->__meta__->{'has'}{$_} && exists __PACKAGE__->__meta__->{'has'}{$_}{'default_value'}
exists __PACKAGE__->__meta__->{'has'}{$_}
exists __PACKAGE__->__meta__->{'has'}{$_}{'default_value'}
Truth table33%1/3
A && B
A= exists __PACKAGE__->__meta__->{'has'}{$_}B= exists __PACKAGE__->__meta__->{'has'}{$_}{'default_value'}
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub defaults_for_register_callback { %defaults } | ||||||||||||||||||||||||||
|
Subroutine
defaults_for_register_callback: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 266 | ▶ | sub register_callback { | ||||||||||||||||||||||||||
|
Subroutine
register_callback: not called CC=10 SCAR=46.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 89 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | unless (defined $params{id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $params{id} = UR::Object::Type->autogenerate_new_object_id_uuid; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my %values = $class->defaults_for_register_callback(); | |||||||||||||||||||||||||||
| 96 | 0 | my @specified_params = grep { exists $params{$_} } @required_params_for_register; | |||||||||||||||||||||||||||
| 97 | 0 | @values{@specified_params} = map { delete $params{$_} } @specified_params; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my @bad_params = keys %params; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if (@bad_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | Carp::croak('invalid params: ' . join(', ', @bad_params)); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my @missing_params = grep { not exists $values{$_} } @required_params_for_register; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (@missing_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | Carp::croak('missing required params: ' . join(', ', @missing_params)); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my @undef_values = grep { not defined $values{$_} } keys %values; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (@undef_values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | Carp::croak('undefined values: ' . join(', ', @undef_values)); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $subject_class_name = $values{subject_class_name}; | |||||||||||||||||||||||||||
| 115 | 0 | my $subject_class_meta = eval { $subject_class_name->__meta__ }; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | Carp::croak("Can't create observer with subject_class_name '$subject_class_name': Can't get class metadata for class '$subject_class_name': $@"); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless ($subject_class_meta) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | Carp::croak("Class $subject_class_name cannot be the subject class for an observer because there is no class metadata"); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my $aspect = $values{aspect}; | |||||||||||||||||||||||||||
| 124 | 0 | my $subject_id = $values{subject_id}; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | unless ($subject_class_meta->_is_valid_signal($aspect)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my $croak = sub { Carp::croak("'$aspect' is not a valid aspect for class $subject_class_name") }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | unless ($subject_class_name->can('validate_subscription')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $croak->(); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | unless ($subject_class_name->validate_subscription($aspect, $subject_id, $values{callback})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $croak->(); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | $class->_insert_record_into_all_change_subscriptions( | ||||||||||||||||||||||||||||
| 136 | @values{qw(subject_class_name aspect subject_id)}, | ||||||||||||||||||||||||||||
| 137 | 0 | [@values{qw(callback note priority id once)}], | |||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | return $values{id}; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 266 | ▶ | sub _insert_record_into_all_change_subscriptions { | ||||||||||||||||||||||||||
|
Subroutine
_insert_record_into_all_change_subscriptions: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 144 | 266 | my($class,$subject_class_name, $aspect,$subject_id, $new_record) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 266 | ▶ | if ($subject_class_name eq 'UR::Object') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | $subject_class_name = ''; | |||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 266 | ▶ | my $list = $UR::Context::all_change_subscriptions->{$subject_class_name}->{$aspect}->{$subject_id} ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$UR::Context::all_change_subscriptions->{$subject_class_name}{$aspect}{$subject_id} ||= []
MC/DC0%0/1
$UR::Context::all_change_subscriptions->{$subject_class_name}{$aspect}{$subject_id} ||= []
$UR::Context::all_change_subscriptions->{$subject_class_name}{$aspect}{$subject_id}
Truth table50%1/2
A
A= $UR::Context::all_change_subscriptions->{$subject_class_name}{$aspect}{$subject_id}
| |||||||||||||||||||||||||||||
| 151 | 266 | push @$list, $new_record; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub _modify_priority { | ||||||||||||||||||||||||||
|
Subroutine
_modify_priority: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 155 | 0 | my($self, $aspect, $old_val, $new_val) = @_; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | my $subject_class_name = $self->subject_class_name; | |||||||||||||||||||||||||||
| 158 | 0 | my $subject_aspect = $self->aspect; | |||||||||||||||||||||||||||
| 159 | 0 | my $subject_id = $self->subject_id; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | my $list = $UR::Context::all_change_subscriptions->{$subject_class_name}->{$subject_aspect}->{$subject_id}; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | return unless $list; # this is probably an error condition | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | my $data; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | for (my $i = 0; $i < @$list; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($list->[$i]->[3] eq $self->id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | ($data) = splice(@$list,$i, 1); | |||||||||||||||||||||||||||
| 168 | 0 | last; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return unless $data; # This is probably an error condition... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | $data->[2] = $new_val; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | $self->_insert_record_into_all_change_subscriptions($subject_class_name, $subject_aspect, $subject_id, $data); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub callback { | ||||||||||||||||||||||||||
|
Subroutine
callback: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 0 | shift->{callback}; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub subscription { | ||||||||||||||||||||||||||
|
Subroutine
subscription: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 183 | shift->{subscription} | ||||||||||||||||||||||||||||
| 184 | 0 | } | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub unregister_callback { | ||||||||||||||||||||||||||
|
Subroutine
unregister_callback: not called CC=14 SCAR=53.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 188 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | my $id = delete $params{id}; | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | unless (defined $id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | Carp::croak('missing required parameter: id'); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | my @undef_params = grep { not defined $params{$_} } keys %params; | |||||||||||||||||||||||||||
| 196 | 0 | ▶ | if (@undef_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 0 | Carp::croak('undefined params: ' . join(', ', @undef_params)); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | my $aspect = delete $params{aspect}; | |||||||||||||||||||||||||||
| 201 | 0 | my $subject_class_name = delete $params{subject_class_name}; | |||||||||||||||||||||||||||
| 202 | 0 | my $subject_id = delete $params{subject_id}; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | my @bad_params = keys %params; | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | if (@bad_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | Carp::croak('invalid params: ' . join(', ', @bad_params)); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | my @subject_class_names = $subject_class_name || keys %{$UR::Context::all_change_subscriptions}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$subject_class_name || keys %{$UR::Context::all_change_subscriptions;}
MC/DC0%0/2
$subject_class_name || keys %{$UR::Context::all_change_subscriptions;}
$subject_class_name
keys %{$UR::Context::all_change_subscriptions;}
Truth table0%0/3
A || B
A= $subject_class_nameB= keys %{$UR::Context::all_change_subscriptions;}
| |||||||||||||||||||||||||||||
| 210 | 0 | for my $subject_class_name (@subject_class_names) { | |||||||||||||||||||||||||||
| 211 | 0 | ▶ | my @aspects = $aspect || keys %{$UR::Context::all_change_subscriptions->{$subject_class_name}}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$aspect || keys %{$$UR::Context::all_change_subscriptions{$subject_class_name};}
MC/DC0%0/2
$aspect || keys %{$$UR::Context::all_change_subscriptions{$subject_class_name};}
$aspect
keys %{$$UR::Context::all_change_subscriptions{$subject_class_name};}
Truth table0%0/3
A || B
A= $aspectB= keys %{$$UR::Context::all_change_subscriptions{$subject_class_name};}
| |||||||||||||||||||||||||||||
| 212 | 0 | for my $aspect (@aspects) { | |||||||||||||||||||||||||||
| 213 | 0 | ▶ | my @subject_ids = $subject_id || keys %{$UR::Context::all_change_subscriptions->{$subject_class_name}->{$aspect}}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$subject_id || keys %{$$UR::Context::all_change_subscriptions{$subject_class_name}{$aspect};}
MC/DC0%0/2
$subject_id || keys %{$$UR::Context::all_change_subscriptions{$subject_class_name}{$aspect};}
$subject_id
keys %{$$UR::Context::all_change_subscriptions{$subject_class_name}{$aspect};}
Truth table0%0/3
A || B
A= $subject_idB= keys %{$$UR::Context::all_change_subscriptions{$subject_class_name}{$aspect};}
| |||||||||||||||||||||||||||||
| 214 | 0 | for my $subject_id (@subject_ids) { | |||||||||||||||||||||||||||
| 215 | 0 | my $arrayref = $UR::Context::all_change_subscriptions->{$subject_class_name}->{$aspect}->{$subject_id}; | |||||||||||||||||||||||||||
| 216 | 0 | ▶ | for (my $i = 0; $i < @$arrayref; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($arrayref->[$i]->[3] eq $id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | splice(@$arrayref, $i, 1); | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | if (@$arrayref == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | $arrayref = undef; | |||||||||||||||||||||||||||
| 221 | 0 | delete $UR::Context::all_change_subscriptions->{$subject_class_name}->{$aspect}->{$subject_id}; | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | if (not keys %{ $UR::Context::all_change_subscriptions->{$subject_class_name}->{$aspect} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | delete $UR::Context::all_change_subscriptions->{$subject_class_name}->{$aspect}; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | 0 | return 1; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | return; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 237 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 238 | #$DB::single = 1; | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | my $subject_class_name = $self->subject_class_name; | |||||||||||||||||||||||||||
| 241 | 0 | my $subject_id = $self->subject_id; | |||||||||||||||||||||||||||
| 242 | 0 | my $aspect = $self->aspect; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | $subject_class_name = '' if (! $subject_class_name or $subject_class_name eq 'UR::Object'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $subject_class_name or $subject_class_name eq "UR::Object"
MC/DC0%0/2
not $subject_class_name or $subject_class_name eq "UR::Object"
not $subject_class_name
$subject_class_name eq "UR::Object"
Truth table0%0/3
not(A) or B
A= not $subject_class_nameB= $subject_class_name eq "UR::Object"
| |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | $subject_id = '' unless (defined $subject_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 0 | ▶ | $aspect = '' unless (defined $aspect); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | my $unregistered = $self->unregister_callback( | |||||||||||||||||||||||||||
| 249 | aspect => $aspect, | ||||||||||||||||||||||||||||
| 250 | id => $self->id, | ||||||||||||||||||||||||||||
| 251 | subject_class_name => $subject_class_name, | ||||||||||||||||||||||||||||
| 252 | subject_id => $subject_id, | ||||||||||||||||||||||||||||
| 253 | ); | ||||||||||||||||||||||||||||
| 254 | 0 | ▶ | if ($unregistered) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 0 | ▶ | unless ($subject_class_name eq '' || $subject_class_name->inform_subscription_cancellation($aspect, $subject_id, $self->{'callback'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$subject_class_name eq "" or $subject_class_name->inform_subscription_cancellation($aspect, $subject_id, $self->{"callback"})
MC/DC0%0/2
$subject_class_name eq "" or $subject_class_name->inform_subscription_cancellation($aspect, $subject_id, $self->{"callback"})
$subject_class_name eq ""
$subject_class_name->inform_subscription_cancellation($aspect, $subject_id, $self->{"callback"})
Truth table0%0/3
A or B
A= $subject_class_name eq ""B= $subject_class_name->inform_subscription_cancellation($aspect, $subject_id, $self->{"callback"})
| |||||||||||||||||||||||||||||
| 256 | 0 | Carp::confess("Failed to validate requested subscription cancellation for aspect '$aspect' on class $subject_class_name"); | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | 0 | $self->SUPER::delete(); | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 0 | ▶ | sub __rollback__ { | ||||||||||||||||||||||||||
|
Subroutine
__rollback__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 263 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 264 | 0 | return UR::Observer::delete($self); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | ▶ | sub get_with_special_parameters { | ||||||||||||||||||||||||||
|
Subroutine
get_with_special_parameters: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 268 | 0 | my($class,$rule,%extra) = @_; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | my $callback = delete $extra{'callback'}; | |||||||||||||||||||||||||||
| 271 | 0 | ▶ | if (keys %extra) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | Carp::croak("Unrecognized parameters in get(): " . join(', ', keys(%extra))); | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | 0 | my @matches = $class->get($rule); | |||||||||||||||||||||||||||
| 275 | 0 | return grep { $_->callback eq $callback } @matches; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 266 | 1; | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =pod | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =head1 NAME | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | UR::Observer - bind callbacks to object changes | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | $rocket = Acme::Rocket->create( | ||||||||||||||||||||||||||||
| 290 | fuel_level => 100 | ||||||||||||||||||||||||||||
| 291 | ); | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | $observer = $rocket->add_observer( | ||||||||||||||||||||||||||||
| 294 | aspect => 'fuel_level', | ||||||||||||||||||||||||||||
| 295 | callback => | ||||||||||||||||||||||||||||
| 296 | sub { | ||||||||||||||||||||||||||||
| 297 | print "fuel level is: " . shift->fuel_level . "\n" | ||||||||||||||||||||||||||||
| 298 | }, | ||||||||||||||||||||||||||||
| 299 | priority => 2, | ||||||||||||||||||||||||||||
| 300 | ); | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | $observer2 = UR::Observer->create( | ||||||||||||||||||||||||||||
| 303 | subject_class_name => 'Acme::Rocket', | ||||||||||||||||||||||||||||
| 304 | subject_id => $rocket->id, | ||||||||||||||||||||||||||||
| 305 | aspect => 'fuel_level', | ||||||||||||||||||||||||||||
| 306 | callback => | ||||||||||||||||||||||||||||
| 307 | sub { | ||||||||||||||||||||||||||||
| 308 | my($self,$changed_aspect,$old_value,$new_value) = @_; | ||||||||||||||||||||||||||||
| 309 | if ($new_value == 0) { | ||||||||||||||||||||||||||||
| 310 | print "Bail out!\n"; | ||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | }, | ||||||||||||||||||||||||||||
| 313 | priority => 0 | ||||||||||||||||||||||||||||
| 314 | ); | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | for (3 .. 0) { | ||||||||||||||||||||||||||||
| 318 | $rocket->fuel_level($_); | ||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | # fuel level is: 3 | ||||||||||||||||||||||||||||
| 321 | # fuel level is: 2 | ||||||||||||||||||||||||||||
| 322 | # fuel level is: 1 | ||||||||||||||||||||||||||||
| 323 | # Bail out! | ||||||||||||||||||||||||||||
| 324 | # fuel level is: 0 | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | $observer->delete; | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | UR::Observer implements the observer pattern for UR objects. These observers | ||||||||||||||||||||||||||||
| 331 | can be attached to individual object instances, or to whole classes. They | ||||||||||||||||||||||||||||
| 332 | can send notifications for changes to object attributes, or to other state | ||||||||||||||||||||||||||||
| 333 | changes such as when an object is loaded from its datasource or deleted. | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | Observers can be created either by using the method C<add_observer()> on | ||||||||||||||||||||||||||||
| 338 | another class, or by calling C<create()> on the UR::Observer class. | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | my $o1 = Some::Other::Class->add_observer(...); | ||||||||||||||||||||||||||||
| 341 | my $o2 = $object_instance->add_observer(...); | ||||||||||||||||||||||||||||
| 342 | my $o3 = UR::Observer->create(...); | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | The constructor accepts these parameters: | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =over 2 | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =item subject_class_name | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | The name of the class the observer is watching. If this observer is being | ||||||||||||||||||||||||||||
| 351 | created via C<add_observer()>, then it figures out the subject_class_name | ||||||||||||||||||||||||||||
| 352 | from the class or object it is being called on. | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =item subject_id | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | The ID of the object the observer is watching. If this observer is being | ||||||||||||||||||||||||||||
| 357 | created via C<add_observer()>, then it figures out the subject_id from the | ||||||||||||||||||||||||||||
| 358 | object it was called on. If C<add_observer()> was called as a class method, | ||||||||||||||||||||||||||||
| 359 | then subject_id is omitted, and means that the observer should fire for | ||||||||||||||||||||||||||||
| 360 | changes on any instance of the class or sub-class. | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =item priority | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | A numeric value used to determine the order the callbacks are fired. Lower | ||||||||||||||||||||||||||||
| 365 | numbers are higher priority, and are run before callbacks with a numerically | ||||||||||||||||||||||||||||
| 366 | higher priority. The default priority is 1. Negative numbers are ok. | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =item aspect | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | The attribute the observer is watching for changes on. The aspect is commonly | ||||||||||||||||||||||||||||
| 371 | one of the properties of the class. In this case, the callback is fired after | ||||||||||||||||||||||||||||
| 372 | the property's value changes. aspect can be omitted, which means the observer | ||||||||||||||||||||||||||||
| 373 | should fire for any change in the object state. If both subject_id and aspect | ||||||||||||||||||||||||||||
| 374 | are omitted, then the observer will fire for any change to any instance of the | ||||||||||||||||||||||||||||
| 375 | class. | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | There are other, system-level aspects that can be watched for that correspond to other types | ||||||||||||||||||||||||||||
| 378 | of state change: | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =over 2 | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =item create | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | After a new object instance is created | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =item delete | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | After an n object instance is deleted | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =item load | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | After an object instance is loaded from its data source | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =item commit | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | After an object instance has changes saved to its data source | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =back | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =item callback | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | A coderef that is called after the observer's event happens. The coderef is | ||||||||||||||||||||||||||||
| 403 | passed four parameters: $self, $aspect, $old_value, $new_value. In this case, | ||||||||||||||||||||||||||||
| 404 | $self is the object that is changing, not the UR::Observer instance (unless, | ||||||||||||||||||||||||||||
| 405 | of course, you have created an observer on UR::Observer). The return value of | ||||||||||||||||||||||||||||
| 406 | the callback is ignored. | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =item once | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | If the 'once' attribute is true, the observer is deleted immediately after | ||||||||||||||||||||||||||||
| 411 | the callback is run. This has the effect of running the callback only once, | ||||||||||||||||||||||||||||
| 412 | no matter how many times the observer condition is triggered. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =item note | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | A text string that is ignored by the system | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =back | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =head2 Custom aspects | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | You can create an observer for an aspect that is neither a property nor one | ||||||||||||||||||||||||||||
| 423 | of the system aspects by listing the aspect names in the metadata for the | ||||||||||||||||||||||||||||
| 424 | class. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | class My::Class { | ||||||||||||||||||||||||||||
| 427 | has => [ 'prop_a', 'another_prop' ], | ||||||||||||||||||||||||||||
| 428 | valid_signals => ['custom', 'pow' ], | ||||||||||||||||||||||||||||
| 429 | }; | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | my $o = My::Class->add_observer( | ||||||||||||||||||||||||||||
| 432 | aspect => 'pow', | ||||||||||||||||||||||||||||
| 433 | callback => sub { print "POW!\n" }, | ||||||||||||||||||||||||||||
| 434 | ); | ||||||||||||||||||||||||||||
| 435 | My::Class->__signal_observers__('pow'); # POW! | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | my $obj = My::Class->create(prop_a => 1); | ||||||||||||||||||||||||||||
| 438 | $obj->__signal_observers__('custom'); # not an error | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | To help catch typos, creating an observer for a non-standard aspect throws an | ||||||||||||||||||||||||||||
| 441 | exception unless the named aspect is in the list of 'valid_signals' in the | ||||||||||||||||||||||||||||
| 442 | class metadata. Nothing in the system will trigger these observers, but they | ||||||||||||||||||||||||||||
| 443 | can be triggered in your own code using the C<__signal_observers()__> class or | ||||||||||||||||||||||||||||
| 444 | object method. Sending a signal for an aspect that no observers are watching | ||||||||||||||||||||||||||||
| 445 | for is not an error. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =cut | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||