| 1 | package Crop::Object::Attr; | ||
| 2 | use base qw/ Crop /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Attr | ||
| 6 | Certain attribute of an Object. | ||
| 7 | |||
| 8 | This module not inherit <Crop::Object>, so automatic generation of getters/setters missed. | ||
| 9 | |||
| 10 | <my %Attibutes> are default values each item has. | ||
| 11 | =cut | ||
| 12 | |||
| 13 | use v5.14; | ||
| 14 | use warnings; | ||
| 15 | no warnings 'experimental::smartmatch'; | ||
| 16 | |||
| 17 | use Clone qw/ clone /; | ||
| 18 | |||
| 19 | use Crop::Debug; | ||
| 20 | use Crop::Error; | ||
| 21 | use Crop::Object::Constants; | ||
| 22 | use Crop::Object::Key; | ||
| 23 | |||
| 24 | =begin nd | ||
| 25 | Constants: Groupping attribute types by store semantics. | ||
| 26 | |||
| 27 | Constant: ATTR_STORED | ||
| 28 | Attribute has to be stored in warehouse. | ||
| 29 | |||
| 30 | Constant: ATTR_CACHED | ||
| 31 | Attribute is not storable in warehouse. | ||
| 32 | |||
| 33 | Constant: ATTR_KEY | ||
| 34 | Attribute is a Key in a warehouse terms. | ||
| 35 | =cut | ||
| 36 | use constant { | ||
| 37 | ATTR_STORED => [qw/ store key /], | ||
| 38 | ATTR_CACHED => [qw/ cache /], | ||
| 39 | ATTR_KEY => [qw/ key /], | ||
| 40 | }; | ||
| 41 | |||
| 42 | =begin nd | ||
| 43 | Constant: DEFAULT_TYPE | ||
| 44 | Any attribute has type; this is default | ||
| 45 | =cut | ||
| 46 | use constant { | ||
| 47 | DEFAULT_TYPE => 'store', | ||
| 48 | }; | ||
| 49 | |||
| 50 | =begin nd | ||
| 51 | Variable: my %Attributes | ||
| 52 | Attributes: | ||
| 53 | |||
| 54 | default - default value of the object | ||
| 55 | extern - declaration of relation to other object | ||
| 56 | key - <Crop::Object::Key> object if attribute is a key | ||
| 57 | mode - access mode (read, write, read/write) | ||
| 58 | name - attribute name | ||
| 59 | stable - each class defines their own semantics of this attribute | ||
| 60 | source - raw source of declaration | ||
| 61 | type - defines either attribute is storable in warehouse ('store', 'cache', 'key') | ||
| 62 | =cut | ||
| 63 | my %Attributes = ( | ||
| 64 | default => undef, | ||
| 65 | extern => undef, | ||
| 66 | key => undef, | ||
| 67 | mode => undef, | ||
| 68 | name => {mode => 'read'}, | ||
| 69 | stable => undef, | ||
| 70 | source => undef, | ||
| 71 | type => DEFAULT_TYPE, | ||
| 72 | ); | ||
| 73 | |||
| 74 | =begin nd | ||
| 75 | Variable: my @Required | ||
| 76 | All madatory constructor arguments. | ||
| 77 | |||
| 78 | Variable: my @Passthrough | ||
| 79 | All optional constructor arguments. | ||
| 80 | =cut | ||
| 81 | my @Required = qw/ name source /; | ||
| 82 | my @Passthrough = qw/ default mode stable type /; | ||
| 83 | |||
| 84 | =begin nd | ||
| 85 | Constructor: new (%in) | ||
| 86 | Set the name and parse the declaration. | ||
| 87 | |||
| 88 | Check mandatory arguments, return erros if missed. | ||
| 89 | |||
| 90 | Parameters: | ||
| 91 | %in - hash of attributes and their values | ||
| 92 | |||
| 93 | Returns: | ||
| 94 | $self - if all right | ||
| 95 | undef - otherwise | ||
| 96 | =cut | ||
| 97 | sub new { | ||
| 98 | my ($class, %in) = @_; | ||
| 99 | |||
| 100 | my $self = bless {%Attributes}, $class; | ||
| 101 | |||
| 102 | # set mandatory attributes | ||
| 103 | exists $in{$_} ? $self->{$_} = $in{$_} : return warn "Attr haven't required field: $_" for @Required; | ||
| 104 | |||
| 105 | # set optional attributes from %in{source} | ||
| 106 | exists $in{source}->{$_} and $self->{$_} = $in{source}->{$_} for @Passthrough; | ||
| 107 | |||
| 108 | # define either type is 'key' | ||
| 109 | if (exists $in{source}->{key}) { | ||
| 110 | $self->{type} = 'key'; | ||
| 111 | $self->{key} = Crop::Object::Key->new(type => $in{source}->{key}); | ||
| 112 | } | ||
| 113 | |||
| 114 | if (exists $in{source}->{extern}) { | ||
| 115 | debug __PACKAGE__ . '::new()_EXTERN=', $in{source}->{extern}; | ||
| 116 | return warn 'Crop::Object::Attr->new NOT IMPLEMENTED'; | ||
| 117 | # $self->{extern} = Crop::Object::Attr::Extern->new( | ||
| 118 | # attr => $self, | ||
| 119 | # source => $in{source}->{extern}, | ||
| 120 | # ); | ||
| 121 | } | ||
| 122 | |||
| 123 | $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | =begin nd | ||
| 127 | Method: accessible ($mode) | ||
| 128 | Check either attribute has the accessor. | ||
| 129 | |||
| 130 | Returns: | ||
| 131 | true - if attr has accessible | ||
| 132 | false - oterwise | ||
| 133 | =cut | ||
| 134 | sub accessible { | ||
| 135 | my ($self, $mode) = @_; | ||
| 136 | |||
| 137 | defined $self->{mode} and $self->{mode} =~ $mode; | ||
| 138 | } | ||
| 139 | |||
| 140 | =begin nd | ||
| 141 | Method: default ( ) | ||
| 142 | Get the declared default value. | ||
| 143 | |||
| 144 | Returns: | ||
| 145 | Copy of default value. This is what you need. | ||
| 146 | =cut | ||
| 147 | sub default { | ||
| 148 | my $self = shift; | ||
| 149 | |||
| 150 | ref $self->{default} ? clone $self->{default} : $self->{default}; | ||
| 151 | } | ||
| 152 | |||
| 153 | =begin nd | ||
| 154 | Method: has ($key, $val) | ||
| 155 | Is attribute has $key with corresponding $val? | ||
| 156 | |||
| 157 | Method of a class. | ||
| 158 | |||
| 159 | Parameters: | ||
| 160 | $key - name of the key | ||
| 161 | $val - value of the $key | ||
| 162 | |||
| 163 | Returns: | ||
| 164 | true - if has | ||
| 165 | false - if not | ||
| 166 | =cut | ||
| 167 | sub has { | ||
| 168 | my ($class, $key, $val) = @_; | ||
| 169 | |||
| 170 | exists $class->{source} and exists $class->{source}{$key} and $class->{source}{$key} eq $val; | ||
| 171 | } | ||
| 172 | |||
| 173 | =begin nd | ||
| 174 | Method: has_default ( ) | ||
| 175 | Is attribute has default value? | ||
| 176 | |||
| 177 | Returns: | ||
| 178 | true - if it has | ||
| 179 | false - otherwise | ||
| 180 | =cut | ||
| 181 | sub has_default { defined shift->{default} } | ||
| 182 | |||
| 183 | =begin nd | ||
| 184 | Method: is_stable ( ) | ||
| 185 | Is attribute stable? | ||
| 186 | |||
| 187 | Each class defines semantics of this flag separately. | ||
| 188 | |||
| 189 | Returns: | ||
| 190 | true - if stable | ||
| 191 | false - if not stable | ||
| 192 | =cut | ||
| 193 | sub is_stable { shift->{stable} } | ||
| 194 | |||
| 195 | =begin nd | ||
| 196 | Method: key ( ) | ||
| 197 | Getter. | ||
| 198 | } | ||
| 199 | =cut | ||
| 200 | sub key { shift->{key} } | ||
| 201 | |||
| 202 | =begin nd | ||
| 203 | Method: name ( ) | ||
| 204 | Get the attribute name. | ||
| 205 | |||
| 206 | Returns: | ||
| 207 | name as string | ||
| 208 | =cut | ||
| 209 | sub name { shift->{name} } | ||
| 210 | |||
| 211 | =begin nd | ||
| 212 | Method: of_type ($type) | ||
| 213 | Is attribute type equal to the $type specified? | ||
| 214 | |||
| 215 | $type has not mapping to the 'type' attribute, but abstract discipline specified by <Crop::Object::Constants>: | ||
| 216 | |||
| 217 | - STORED | ||
| 218 | - CACHED | ||
| 219 | - ANY | ||
| 220 | |||
| 221 | Parameters: | ||
| 222 | $type - type to check; STORED, CACHED, ANY, specified by <Crop::Object::Constants> | ||
| 223 | |||
| 224 | Returns: | ||
| 225 | true - if correspondes | ||
| 226 | false - otherwise | ||
| 227 | =cut | ||
| 228 | sub of_type { | ||
| 229 | my ($self, $type) = @_; | ||
| 230 | |||
| 231 | given ($type) { | ||
| 232 | when (STORED) { $self->{type} ~~ ATTR_STORED } | ||
| 233 | when (CACHED) { $self->{type} ~~ ATTR_CACHED } | ||
| 234 | when (KEY) { $self->{type} ~~ ATTR_KEY } | ||
| 235 | when (ANY) { defined $self->{type} } | ||
| 236 | |||
| 237 | default { warn "OBJECT|ALERT: Unknown Attribute discipline '$type'" } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | =begin nd | ||
| 242 | Method: Set_state ( ) | ||
| 243 | Do nothing. | ||
| 244 | |||
| 245 | Attr is not a <Crop::Object> subclass, so has to redefine this method. | ||
| 246 | =cut | ||
| 247 | sub Set_state { } | ||
| 248 | |||
| 249 | 1; |