line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Oryx::Attribute; |
2
|
|
|
|
|
|
|
|
3
|
15
|
|
|
15
|
|
93
|
use base qw(Oryx::MetaClass); |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
2404
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Oryx::Attribute - Attribute meta-type for Oryx persistent classes |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $attrib = Oryx::Attribute->new( \%meta, $owner ); |
12
|
|
|
|
|
|
|
$attrib->name; # name used for accessor generation |
13
|
|
|
|
|
|
|
$attrib->size; # size constraint for the value |
14
|
|
|
|
|
|
|
$attrib->type; # value type |
15
|
|
|
|
|
|
|
$attrib->required; # NOT NULL |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 DESCRIPTION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 METHODS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=over |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=item new( $meta, $owner ) |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new { |
28
|
0
|
|
|
0
|
1
|
|
my ($class, $meta, $owner) = @_; |
29
|
0
|
|
|
|
|
|
my $self = bless { |
30
|
|
|
|
|
|
|
owner => $owner, |
31
|
|
|
|
|
|
|
meta => $meta, |
32
|
|
|
|
|
|
|
}, $class; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
eval 'use '.$self->type_class; |
35
|
0
|
0
|
|
|
|
|
$self->_croak($@) if $@; |
36
|
|
|
|
|
|
|
|
37
|
15
|
|
|
15
|
|
83
|
no strict 'refs'; |
|
15
|
|
|
|
|
35
|
|
|
15
|
|
|
|
|
8853
|
|
38
|
0
|
|
|
|
|
|
*{$owner.'::'.$self->name} = $self->_mk_accessor; |
|
0
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
return $self; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item create |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Abstract (see implementing subclasses) |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item retrieve |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Abstract (see implementing subclasses) |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item update |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Abstract (see implementing subclasses) |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item delete |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Abstract (see implementing subclasses) |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item search |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Abstract (see implementing subclasses) |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
0
|
1
|
|
sub create { } |
67
|
0
|
|
|
0
|
1
|
|
sub retrieve { } |
68
|
0
|
|
|
0
|
1
|
|
sub update { } |
69
|
0
|
|
|
0
|
1
|
|
sub delete { } |
70
|
0
|
|
|
0
|
1
|
|
sub search { } |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item construct( $self, $obj ) |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Inflate the attribute value and C it to the implementing |
75
|
|
|
|
|
|
|
Value class, eg: L, L |
76
|
|
|
|
|
|
|
etc. (see L) |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub construct { |
81
|
0
|
|
|
0
|
1
|
|
my ($self, $obj) = @_; |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
my $attr_name = $self->name; |
84
|
0
|
|
|
|
|
|
$obj->{$attr_name} = $self->inflate($obj->{$attr_name}); |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my @args = ($self, $obj); |
87
|
0
|
|
|
|
|
|
tie $obj->{$attr_name}, $self->type_class, @args; |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return $obj; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item name |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
returns the C meta-attribute for this attribute. This |
95
|
|
|
|
|
|
|
is the same as the accessor and the field in the table in which |
96
|
|
|
|
|
|
|
the value for this attribute is stored. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub name { |
101
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
102
|
0
|
|
|
|
|
|
return $self->getMetaAttribute("name"); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item type |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
returns the C meta-attribute for this attribute. Defaults |
108
|
|
|
|
|
|
|
to 'String'. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub type { |
113
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
114
|
0
|
0
|
|
|
|
|
$self->getMetaAttribute("type") || 'String'; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item size |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
returns the C meta-attribute for this attribute. This is |
120
|
|
|
|
|
|
|
the allowed length for the 'String' or size of 'Number' etc. and |
121
|
|
|
|
|
|
|
is used for input checking by the Value type. No default. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub size { |
126
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
127
|
0
|
|
|
|
|
|
return $self->getMetaAttribute("size"); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item required |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
returns the value of the C meta-attribute. This has |
133
|
|
|
|
|
|
|
the effect of raising an error if an instance of the owning |
134
|
|
|
|
|
|
|
class is constructed without a value for this field defined |
135
|
|
|
|
|
|
|
in the prototype hash reference which is passed to |
136
|
|
|
|
|
|
|
C<< Oryx::Class->create( \%proto ) >>. Equivalent to a NOT NULL |
137
|
|
|
|
|
|
|
constraint. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub required { |
142
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
143
|
0
|
|
|
|
|
|
return $self->getMetaAttribute('required'); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item primitive |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
returns a list the first argument of which is one of: 'Integer', |
149
|
|
|
|
|
|
|
'String', 'Boolean', 'Float', 'Text', 'Binary' or 'DateTime' |
150
|
|
|
|
|
|
|
which are mapped to SQL column types by the L |
151
|
|
|
|
|
|
|
classes. The second argument is an optional size constraint. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub primitive { |
156
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
157
|
0
|
|
|
|
|
|
return $self->type_class->primitive; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item type_class |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
returns the canonical package name of the implementing |
163
|
|
|
|
|
|
|
L meta-type for this attribute. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub type_class { |
168
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
169
|
0
|
|
|
|
|
|
return 'Oryx::Value::'.$self->type; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub deflate { |
173
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
174
|
0
|
|
|
|
|
|
my $value = shift; |
175
|
0
|
0
|
|
|
|
|
if (ref $self->meta->{deflate} eq 'CODE') { |
176
|
0
|
|
|
|
|
|
return $self->meta->{deflate}->($value); |
177
|
|
|
|
|
|
|
} else { |
178
|
0
|
|
|
|
|
|
return $self->type_class->deflate($value); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub inflate { |
183
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
184
|
0
|
|
|
|
|
|
my $value = shift; |
185
|
0
|
0
|
|
|
|
|
if (ref $self->meta->{inflate} eq 'CODE') { |
186
|
0
|
|
|
|
|
|
return $self->meta->{inflate}->($value); |
187
|
|
|
|
|
|
|
} else { |
188
|
0
|
|
|
|
|
|
return $self->type_class->inflate($value); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub _mk_accessor { |
193
|
0
|
|
|
0
|
|
|
my $attrib = shift; |
194
|
0
|
|
|
|
|
|
my $attrib_name = $attrib->name; |
195
|
|
|
|
|
|
|
return sub { |
196
|
0
|
|
|
0
|
|
|
my $self = shift; |
197
|
0
|
0
|
|
|
|
|
$self->{$attrib_name} = shift if @_; |
198
|
0
|
|
|
|
|
|
$self->{$attrib_name}; |
199
|
0
|
|
|
|
|
|
}; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
1; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=back |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 AUTHOR |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Richard Hundt |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 THANKS TO |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Andrew Sterling Hanenkamp |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 LICENCE |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
This module is free software and may be used under the same terms as |
217
|
|
|
|
|
|
|
Perl itself. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |
220
|
|
|
|
|
|
|
|