| 1 | package MOP4Import::Base::Configure; | ||
| 2 | use MOP4Import::Declare -as_base, -fatal; | ||
| 3 | |||
| 4 | use Scalar::Util qw/weaken/; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use MOP4Import::Opts; | ||
| 8 | use MOP4Import::Types::Extend | ||
| 9 | FieldSpec => [[fields => qw/weakref/]]; | ||
| 10 | |||
| 11 | use constant DEBUG_WEAK => $ENV{DEBUG_MOP4IMPORT_WEAKREF}; | ||
| 12 | |||
| 13 | our %FIELDS; | ||
| 14 | |||
| 15 | #--------- | ||
| 16 | |||
| 17 | sub new :method { | ||
| 18 | my MY $self = fields::new(shift); | ||
| 19 | $self->configure(@_); | ||
| 20 | $self->before_configure_default; | ||
| 21 | $self->after_new; | ||
| 22 | $self->configure_default; | ||
| 23 | $self->after_configure_default; | ||
| 24 | $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub before_configure_default :method {} | ||
| 28 | sub after_new {}; # Should be deprecated near future. | ||
| 29 | sub after_configure_default :method {} | ||
| 30 | |||
| 31 | sub configure_default :method { | ||
| 32 | (my MY $self, my $target) = @_; | ||
| 33 | |||
| 34 | $target //= $self; | ||
| 35 | |||
| 36 | my $fields = MOP4Import::Declare::fields_hash($self); | ||
| 37 | |||
| 38 | while ((my $name, my FieldSpec $spec) = each %$fields) { | ||
| 39 | if (not defined $target->{$name} and defined $spec->{default}) { | ||
| 40 | $target->{$name} = $self->can("default_$name")->($self); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | $target; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub configure :method { | ||
| 48 | (my MY $self) = shift; | ||
| 49 | |||
| 50 | my @args = do { | ||
| 51 | if (@_ != 1) { | ||
| 52 | @_; | ||
| 53 | } elsif (ref $_[0] eq 'HASH') { | ||
| 54 | %{$_[0]} | ||
| 55 | } elsif (my $sub = UNIVERSAL::can($_[0], "cf_configs")) { | ||
| 56 | # Shallow copy via cf_configs() | ||
| 57 | $sub->($_[0]); | ||
| 58 | } else { | ||
| 59 | Carp::croak "Unknown argument! ".MOP4Import::Util::terse_dump($_[0]); | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | |||
| 63 | my $fields = MOP4Import::Declare::fields_hash($self); | ||
| 64 | |||
| 65 | my @setter; | ||
| 66 | while (my ($key, $value) = splice @args, 0, 2) { | ||
| 67 | unless (defined $key) { | ||
| 68 | croak "Undefined option name for class ".ref($self); | ||
| 69 | } | ||
| 70 | |||
| 71 | if ($key =~ m{^_}) { | ||
| 72 | croak "Private option is prohibited for class ".ref($self).": $key"; | ||
| 73 | } | ||
| 74 | |||
| 75 | if (my $sub = $self->can("onconfigure_$key")) { | ||
| 76 | push @setter, [$sub, $value]; | ||
| 77 | } elsif (exists $fields->{$key}) { | ||
| 78 | $self->{$key} = $value; | ||
| 79 | } else { | ||
| 80 | croak "Unknown option for class ".ref($self).": ".$key; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | $_->[0]->($self, $_->[-1]) for @setter; | ||
| 85 | |||
| 86 | $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub cget :method { | ||
| 90 | my ($self, $key, $default) = @_; | ||
| 91 | $key =~ s/^--//; | ||
| 92 | my $fields = MOP4Import::Declare::fields_hash($self); | ||
| 93 | if (not exists $fields->{$key}) { | ||
| 94 | confess "No such option: $key" | ||
| 95 | } | ||
| 96 | $self->{$key} // $default; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub declare___field_with_weakref :MetaOnly { | ||
| 100 | (my $myPack, my Opts $opts, my FieldSpec $fs, my ($k, $v)) = m4i_args(@_); | ||
| 101 | |||
| 102 | $fs->{$k} = $v; | ||
| 103 | |||
| 104 | if ($v) { | ||
| 105 | my $name = $fs->{name}; | ||
| 106 | my $setter = "onconfigure_$name"; | ||
| 107 | print STDERR "# Declaring weakref $setter for $opts->{objpkg}.$name\n" | ||
| 108 | if DEBUG_WEAK; | ||
| 109 | *{MOP4Import::Util::globref($opts->{objpkg}, $setter)} = sub { | ||
| 110 | print STDERR "# weaken $opts->{objpkg}.$name\n" if DEBUG_WEAK; | ||
| 111 | weaken($_[0]->{$name} = $_[1]); | ||
| 112 | }; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub cf_configs :method { | ||
| 117 | (my MY $self, my (%opts)) = @_; | ||
| 118 | my $all = delete $opts{all}; | ||
| 119 | if (keys %opts) { | ||
| 120 | Carp::croak "Unknown option for cf_configs: ".join(", ", sort keys %opts); | ||
| 121 | } | ||
| 122 | my $fields = MOP4Import::Util::fields_hash($self); | ||
| 123 | my @result; | ||
| 124 | foreach my $key ($self->cf_public_fields) { | ||
| 125 | defined (my $val = $self->{$key}) | ||
| 126 | or next; | ||
| 127 | my FieldSpec $spec = $fields->{$key}; | ||
| 128 | if (not $all | ||
| 129 | and defined $spec->{default} and $val eq $spec->{default}) { | ||
| 130 | next; | ||
| 131 | } | ||
| 132 | push @result, $key, MOP4Import::Util::shallow_copy($val); | ||
| 133 | } | ||
| 134 | @result; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub cf_public_fields :method { | ||
| 138 | my $obj_or_class = shift; | ||
| 139 | my $fields = MOP4Import::Util::fields_hash($obj_or_class); | ||
| 140 | sort grep {/^[a-z]/i} keys %$fields; | ||
| 141 | } | ||
| 142 | |||
| 143 | 1; | ||
| 144 | |||
| 145 | __END__ |