Minions
view release on metacpan or search on metacpan
lib/Minions/Implementation.pm view on Meta::CPAN
use Package::Stash;
use Readonly;
sub import {
my ($class, %arg) = @_;
strict->import();
$arg{-caller} = (caller)[0];
$class->define(%arg);
}
sub define {
my ($class, %arg) = @_;
my $caller_pkg = delete $arg{-caller} || (caller)[0];
my $stash = Package::Stash->new($caller_pkg);
$class->update_args(\%arg);
$class->add_attribute_syms(\%arg, $stash);
$stash->add_symbol('%__meta__', \%arg);
}
sub add_attribute_syms {
my ($class, $arg, $stash) = @_;
my @slots = (
keys %{ $arg->{has} },
@{ $arg->{requires}{attributes} || [] },
'', # semiprivate pkg
);
foreach my $slot ( @slots ) {
$class->add_obfu_name($arg, $stash, $slot);
}
}
sub add_obfu_name {
my ($class, $arg, $stash, $slot) = @_;
my $data_version = $stash->get_symbol('$DATA_VERSION');
Readonly my $sym_val => sprintf(
"%s-$slot",
Minions::_Guts::attribute_sym($data_version),
);
$Minions::_Guts::obfu_name{$slot} = $sym_val;
my $prefix = '';
if($slot eq '' || $arg->{attr_style} eq '_2') {
$prefix = '__';
}
elsif($arg->{attr_style} eq 'uc' || ! $arg->{attr_style}) {
$slot = uc $slot;
}
$stash->add_symbol(
sprintf('$%s%s', $prefix, $slot),
\ $sym_val
);
}
sub update_args {}
1;
__END__
=head1 NAME
Minions::Implementation
=head1 SYNOPSIS
package Example::Construction::Acme::Set_v1;
use Minions::Implementation
has => {
set => {
default => sub { {} },
init_arg => 'items',
map_init_arg => sub { return { map { $_ => 1 } @{ $_[0] } } },
}
},
;
sub has {
my ($self, $e) = @_;
exists $self->{$SET}{$e};
}
sub add {
my ($self, $e) = @_;
++$self->{$SET}{$e};
}
1;
=head1 DESCRIPTION
An implementation is a package containing attribute definitions as well as subroutines implementing the
behaviours described by the class interface.
=head1 CONFIGURATION
An implementation package can be configured either using Minions::Implementation or with a package variable C<%__meta__>. Both methods make use of the following keys:
=head2 has => HASHREF
This declares attributes of the implementation, mapping the name of an attribute to a hash with keys described in
the following sub sections.
An attribute called "foo" can be accessed via it's object in one of two ways:
# implementation defined using Minions::Implementation
$self->{$FOO}
# implementation defined using %__meta__
$self->{-foo}
The advantage of the first form is that the symbol C<$FOO> is not (easily) available to users of the object, so
there is greater incentive for using the provided interface when using the object.
( run in 0.840 second using v1.01-cache-2.11-cpan-ceb78f64989 )