| 1 | package Evo::Class::Attrs::PP; | ||
| 2 | use Evo '-Export; Carp croak confess'; | ||
| 3 | use constant {ECA_OPTIONAL => 0, ECA_DEFAULT => 1, ECA_DEFAULT_CODE => 2, ECA_REQUIRED => 3, | ||
| 4 | ECA_LAZY => 4,}; | ||
| 5 | |||
| 6 | export qw( | ||
| 7 | ECA_OPTIONAL ECA_DEFAULT ECA_DEFAULT_CODE ECA_REQUIRED ECA_LAZY | ||
| 8 | ); | ||
| 9 | |||
| 10 | my sub _croak_bad_value ($val, $name, $msg) { | ||
| 11 | $msg //= ''; | ||
| 12 | croak qq{Bad value "$val" for attribute "$name": $msg}; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub new { bless [], shift } | ||
| 16 | |||
| 17 | sub exists ($self, $name) { | ||
| 18 | do { return 1 if $_->{name} eq $name } | ||
| 19 | for @$self; | ||
| 20 | return; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub slots ($self) { | ||
| 24 | @$self; | ||
| 25 | } | ||
| 26 | |||
| 27 | my sub _find_index ($self, $name) { | ||
| 28 | my $index = 0; | ||
| 29 | do { last if $_->{name} eq $name; $index++ } | ||
| 30 | for @$self; | ||
| 31 | $index; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _reg_attr ($self, %opts) { | ||
| 35 | $self->[_find_index($self, $opts{name})] = my $attr = \%opts; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _gen_attr ($self, %opts) { | ||
| 39 | my ($name, $check, $ro) = @opts{qw(name check ro)}; | ||
| 40 | my $lazy = $opts{type} == ECA_LAZY ? $opts{value} : undef; | ||
| 41 | |||
| 42 | # simplest and popular | ||
| 43 | if (!$ro && !$lazy && !$check) { | ||
| 44 | return sub { | ||
| 45 | return $_[0]{$name} if @_ == 1; | ||
| 46 | $_[0]{$name} = $_[1]; | ||
| 47 | $_[0]; | ||
| 48 | }; | ||
| 49 | } | ||
| 50 | |||
| 51 | # more complex. we can optimize it by splitting to 6 other. but better use XS | ||
| 52 | return sub { | ||
| 53 | if (@_ == 1) { | ||
| 54 | return $_[0]{$name} if exists $_[0]{$name}; | ||
| 55 | return unless $lazy; | ||
| 56 | return $_[0]{$name} = $lazy->($_[0]); | ||
| 57 | } | ||
| 58 | croak qq{Attribute "$name" is readonly} if $ro; | ||
| 59 | if ($check) { | ||
| 60 | my ($ok, $msg) = $check->(my $val = $_[1]); | ||
| 61 | _croak_bad_value($val, $name, $msg) if !$ok; | ||
| 62 | } | ||
| 63 | $_[0]{$name} = $_[1]; | ||
| 64 | $_[0]; | ||
| 65 | }; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub gen_attr ($self, %opts) { | ||
| 69 | $self->_reg_attr(%opts); | ||
| 70 | $self->_gen_attr(%opts); | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub gen_new($self) { | ||
| 75 | |||
| 76 | sub ($class, %opts) { | ||
| 77 | no strict 'refs'; ## no critic | ||
| 78 | $class = ref $class || $class; | ||
| 79 | my $attrs = ${"${class}::EVO_CLASS_ATTRS"} || croak "Not an Evo class, no ATTRS"; | ||
| 80 | my $obj = {}; | ||
| 81 | |||
| 82 | # iterate known attrs | ||
| 83 | foreach my $slot (@$attrs) { | ||
| 84 | my ($name, $type, $value, $check) = @$slot{qw(name type value check)}; | ||
| 85 | |||
| 86 | if (exists $opts{$name}) { | ||
| 87 | if ($check) { | ||
| 88 | my ($ok, $err) = $check->(my $val = $opts{$name}); | ||
| 89 | _croak_bad_value($opts{$name}, $name, $err) if !$ok; | ||
| 90 | } | ||
| 91 | $obj->{$name} = delete $opts{$name}; | ||
| 92 | next; | ||
| 93 | } | ||
| 94 | |||
| 95 | # required and default are mutually exclusive | ||
| 96 | if ($type == ECA_REQUIRED) { | ||
| 97 | croak qq#Attribute "$name" is required#; | ||
| 98 | } | ||
| 99 | elsif ($type == ECA_DEFAULT) { | ||
| 100 | $obj->{$name} = $value; | ||
| 101 | } | ||
| 102 | elsif ($type == ECA_DEFAULT_CODE) { | ||
| 103 | $obj->{$name} = $value->($class); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | croak "Unknown attributes: " . join(',', keys %opts) if (keys %opts); | ||
| 108 | |||
| 109 | bless $obj, $class; | ||
| 110 | }; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __END__ |