| 1 | package Oryx::Attribute; | ||
| 2 | |||
| 3 | use base qw(Oryx::MetaClass); | ||
| 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 | my ($class, $meta, $owner) = @_; | ||
| 29 | my $self = bless { | ||
| 30 | owner => $owner, | ||
| 31 | meta => $meta, | ||
| 32 | }, $class; | ||
| 33 | |||
| 34 | eval 'use '.$self->type_class; | ||
| 35 | $self->_croak($@) if $@; | ||
| 36 | |||
| 37 | no strict 'refs'; | ||
| 38 | *{$owner.'::'.$self->name} = $self->_mk_accessor; | ||
| 39 | |||
| 40 | 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 | sub create { } | ||
| 67 | sub retrieve { } | ||
| 68 | sub update { } | ||
| 69 | sub delete { } | ||
| 70 | sub search { } | ||
| 71 | |||
| 72 | =item construct( $self, $obj ) | ||
| 73 | |||
| 74 | Inflate the attribute value and C<tie> it to the implementing | ||
| 75 | Value class, eg: L<Oryx::Value::String>, L<Oryx::Value::Complex> | ||
| 76 | etc. (see L<perltie>) | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub construct { | ||
| 81 | my ($self, $obj) = @_; | ||
| 82 | |||
| 83 | my $attr_name = $self->name; | ||
| 84 | $obj->{$attr_name} = $self->inflate($obj->{$attr_name}); | ||
| 85 | |||
| 86 | my @args = ($self, $obj); | ||
| 87 | tie $obj->{$attr_name}, $self->type_class, @args; | ||
| 88 | |||
| 89 | return $obj; | ||
| 90 | } | ||
| 91 | |||
| 92 | =item name | ||
| 93 | |||
| 94 | returns the C<name> 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 | my $self = shift; | ||
| 102 | return $self->getMetaAttribute("name"); | ||
| 103 | } | ||
| 104 | |||
| 105 | =item type | ||
| 106 | |||
| 107 | returns the C<type> meta-attribute for this attribute. Defaults | ||
| 108 | to 'String'. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub type { | ||
| 113 | my $self = shift; | ||
| 114 | $self->getMetaAttribute("type") || 'String'; | ||
| 115 | } | ||
| 116 | |||
| 117 | =item size | ||
| 118 | |||
| 119 | returns the C<size> 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 | my $self = shift; | ||
| 127 | return $self->getMetaAttribute("size"); | ||
| 128 | } | ||
| 129 | |||
| 130 | =item required | ||
| 131 | |||
| 132 | returns the value of the C<required> 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 | my $self = shift; | ||
| 143 | 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<Oryx::DBI::Util> | ||
| 151 | classes. The second argument is an optional size constraint. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub primitive { | ||
| 156 | my $self = shift; | ||
| 157 | return $self->type_class->primitive; | ||
| 158 | } | ||
| 159 | |||
| 160 | =item type_class | ||
| 161 | |||
| 162 | returns the canonical package name of the implementing | ||
| 163 | L<Oryx::Value> meta-type for this attribute. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub type_class { | ||
| 168 | my $self = shift; | ||
| 169 | return 'Oryx::Value::'.$self->type; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub deflate { | ||
| 173 | my $self = shift; | ||
| 174 | my $value = shift; | ||
| 175 | if (ref $self->meta->{deflate} eq 'CODE') { | ||
| 176 | return $self->meta->{deflate}->($value); | ||
| 177 | } else { | ||
| 178 | return $self->type_class->deflate($value); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | sub inflate { | ||
| 183 | my $self = shift; | ||
| 184 | my $value = shift; | ||
| 185 | if (ref $self->meta->{inflate} eq 'CODE') { | ||
| 186 | return $self->meta->{inflate}->($value); | ||
| 187 | } else { | ||
| 188 | return $self->type_class->inflate($value); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub _mk_accessor { | ||
| 193 | my $attrib = shift; | ||
| 194 | my $attrib_name = $attrib->name; | ||
| 195 | return sub { | ||
| 196 | my $self = shift; | ||
| 197 | $self->{$attrib_name} = shift if @_; | ||
| 198 | $self->{$attrib_name}; | ||
| 199 | }; | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =head1 AUTHOR | ||
| 207 | |||
| 208 | Richard Hundt <richard NO SPAM AT protea-systems.com> | ||
| 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 |