line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package EntityModel::Class::Accessor; |
2
|
|
|
|
|
|
|
$EntityModel::Class::Accessor::VERSION = '0.016'; |
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
569
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
EntityModel::Class::Accessor - generic class accessor |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.016 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
See L. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head2 add_to_class |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Returns (method name, coderef) pairs for new methods to add. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub add_to_class { |
27
|
7
|
|
|
7
|
1
|
12
|
my ($class, $pkg, $k, $v) = @_; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
return $k => $class->method_list( |
30
|
|
|
|
|
|
|
pkg => $pkg, |
31
|
|
|
|
|
|
|
k => $k, |
32
|
|
|
|
|
|
|
pre => $v->{pre}, |
33
|
|
|
|
|
|
|
post => $v->{post}, |
34
|
|
|
|
|
|
|
allowed => $v->{valid}, |
35
|
|
|
|
|
|
|
validate => defined $v->{valid} |
36
|
|
|
|
|
|
|
? ref $v->{valid} eq 'CODE' |
37
|
0
|
|
|
0
|
|
0
|
? $v->{valid} : sub { $_[0] eq $v->{valid} } |
38
|
|
|
|
|
|
|
: undef |
39
|
7
|
0
|
|
|
|
54
|
); |
|
|
50
|
|
|
|
|
|
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head2 method_list |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Returns the coderef for the method that should be applied to the requesting class. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub method_list { |
49
|
4
|
|
|
4
|
1
|
27
|
my ($self, %opt) = @_; |
50
|
4
|
|
|
|
|
8
|
my $k = delete $opt{k}; |
51
|
4
|
50
|
33
|
|
|
67
|
if($opt{pre} || $opt{post}) { |
52
|
|
|
|
|
|
|
return sub { |
53
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
54
|
0
|
0
|
|
|
|
0
|
if($opt{pre}) { |
55
|
0
|
0
|
|
|
|
0
|
$opt{pre}->($self, @_) |
56
|
|
|
|
|
|
|
or return; |
57
|
|
|
|
|
|
|
} |
58
|
0
|
0
|
|
|
|
0
|
if(@_) { |
59
|
0
|
0
|
0
|
|
|
0
|
die $_[0] . ' is invalid' if $opt{validate} && !$opt{validate}->($_[0]); |
60
|
0
|
|
|
|
|
0
|
my $v = $_[0]; |
61
|
|
|
|
|
|
|
# Readonly values can be problematic, make a copy if we can - but don't trash refs. |
62
|
0
|
0
|
0
|
|
|
0
|
$v = "$v" if Scalar::Util::readonly($v) && !ref $v; |
63
|
0
|
|
|
|
|
0
|
$self->{$k} = $v; |
64
|
|
|
|
|
|
|
} |
65
|
0
|
0
|
|
|
|
0
|
$opt{post}->($self, @_) if $opt{post}; |
66
|
0
|
0
|
|
|
|
0
|
return $self if @_; |
67
|
0
|
|
|
|
|
0
|
$self->{$k}; |
68
|
0
|
|
|
|
|
0
|
}; |
69
|
|
|
|
|
|
|
} else { |
70
|
|
|
|
|
|
|
return sub { |
71
|
4
|
100
|
|
4
|
|
925
|
return $_[0]->{$k} unless @_ > 1; |
|
|
|
|
4
|
|
|
|
72
|
2
|
50
|
33
|
|
|
11
|
die $_[1] . ' is invalid' if $opt{validate} && !$opt{validate}->(@_); |
73
|
2
|
|
|
|
|
5
|
my $v = $_[1]; |
74
|
|
|
|
|
|
|
# Readonly values can be problematic, make a copy if we can - but don't trash refs. |
75
|
2
|
50
|
33
|
|
|
12
|
$v = "$v" if Scalar::Util::readonly($v) && !ref $v; |
76
|
2
|
|
|
|
|
11
|
$_[0]->{$k} = $v; |
77
|
2
|
|
|
|
|
11
|
return $_[0]; |
78
|
4
|
|
|
|
|
40
|
}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
__END__ |