KOBAYASHI, Hiroaki

NAME

MOP4Import::Base::Configure - Base class with configure() interface for fields

SYNOPSIS

  package MyMetaCPAN {
     
     use MOP4Import::Base::Configure -as_base
       , [fields =>
          [baseurl => default => 'https://fastapi.metacpan.org/v1'],
        ]
       ;
     
     sub get {
       (my MY $self, my $entry) = @_;
       $self->furl_get("$self->{baseurl}$entry");
     }
   }
  
  #
  
  my $obj = MyMetaCPAN->new;
  print $obj->baseurl; # => https://fastapi.metacpan.org/v1
  $obj->get("/author/someone");

  $obj = MyMetaCPAN->new(baseurl => 'http://localhost:8000');
  # $obj = MyMetaCPAN->new({baseurl => 'http://localhost:8000'});
  # $obj->configure(baseurl => 'http://localhost:8000');

  print $obj->baseurl; # => 'http://localhost:8000'
  $obj->get("/author/someone");

DESCRIPTION

MOP4Import::Base::Configure is a minimalistic base class for fields based OO with support of Tk-like new/configure interface, automatic getter generation and default value initialization.

This class also inherits MOP4Import::Declare, so you can define your own declare_.. pragmas too.

METHODS

new (%opts | \%opts)

Usual constructor. This passes given %opts to "configure". Actual implementation is following:

  sub new :method {
    my MY $self = fields::new(shift);
    $self->configure(@_);
    $self->before_configure_default;
    $self->after_new; # Note: deprecated.
    $self->configure_default;
    $self->after_configure_default;
    $self;
  }

configure (%opts | \%opts)

General setter interface for public fields. See also Tk style configure method.

configure_default ()

This fills undefined public fields with their default values. Default values are obtained via default_FIELD hook. They are normally defined by default field spec.

HOOK METHODS

before_configure_default

This hook is called after configure and just before configure_default. This is useful to change behavior whether specific option is given or not.

after_configure_default

This hook is called after configure_default. This is useful to compute all fields are filled with default values.

after_new (deprecated)

This method is called just before configure_default.

FIELD SPECs

For field spec, you can also have hook for field spec.

default => VALUE

This defines default_FIELDNAME method with given VALUE.

weakref => 1

This generates set hook (onconfigure_FIELDNAME) wrapped with "weaken" in Scalar::Util.

json_type => STRING | Cpanel::JSON::XS::Type

To be documented...

SEE ALSO

MOP4Import::Declare

AUTHOR

Kobayashi, Hiroaki <hkoba@cpan.org>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.