| 1 | package AutoCode::Object; | ||
| 2 | use strict; | ||
| 3 | use AutoCode::Root; | ||
| 4 | our @ISA=qw(AutoCode::Root); | ||
| 5 | |||
| 6 | # this method have to be overridden here, since the scalar_getst and | ||
| 7 | # array_getset must be assigned before _initialize. | ||
| 8 | # | ||
| 9 | sub new { | ||
| 10 | my ($invocant, @args)=@_; | ||
| 11 | my $class = ref($invocant) || $invocant; | ||
| 12 | my $self={}; | ||
| 13 | bless $self, $class; | ||
| 14 | $self->_initialize(@args); | ||
| 15 | return $self; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub _initialize { | ||
| 19 | my ($self, @args)=@_; | ||
| 20 | $self->SUPER::_initialize(@args); | ||
| 21 | |||
| 22 | $self->_add_scalar_accessor('type', 'factory'); | ||
| 23 | |||
| 24 | my ($type, $factory, $scalar_accessors, $array_accessors)= | ||
| 25 | $self->_rearrange( | ||
| 26 | [qw(TYPE FACTORY SCALAR_ACCESSORS ARRAY_ACCESSORS)], @args); | ||
| 27 | (ref($scalar_accessors)eq'ARRAY') | ||
| 28 | or $self->throw("SCALAR_ACCESSORS should be a reference to an array"); | ||
| 29 | $self->_add_scalar_accessor(@$scalar_accessors); | ||
| 30 | (ref($array_accessors)eq'ARRAY') | ||
| 31 | or $self->throw("ARRAY_ACCESSORS should be a reference to an array"); | ||
| 32 | $self->_add_array_accessor(@$array_accessors); | ||
| 33 | |||
| 34 | |||
| 35 | defined $type and $self->type($type); | ||
| 36 | defined $factory and $self->factory($factory); | ||
| 37 | |||
| 38 | # Now it is magic time to map the @args into accessors | ||
| 39 | # First, grep all args' keys that are not intent to be used in this Object, | ||
| 40 | # but the specific business objects | ||
| 41 | my @reserved = qw(TYPE FACTORY SCALAR_ACCESSORS ARRAY_ACCESSORS); | ||
| 42 | my %args = @args; | ||
| 43 | my @args_keys = map {s/^\-//; $_} keys %args; | ||
| 44 | %args=(); # recycle it. | ||
| 45 | while(@args){ | ||
| 46 | (my $key=shift @args) =~ tr/a-z\055/A-Z/d; | ||
| 47 | $args{$key}=shift @args; | ||
| 48 | } | ||
| 49 | # @args_keys = grep {my $z=$_; (map{$z eq $_}@reserved?():$z)}@args_keys; | ||
| 50 | foreach(@args_keys){ | ||
| 51 | my $v=$args{uc $_}; | ||
| 52 | print "$_\t\t$v\t|\n"; | ||
| 53 | $_ = lc $_; | ||
| 54 | $self->$_($v) if $self->can($_); | ||
| 55 | } | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | 1; |