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