## Methods called prior to instantiation
-sub process_options_for_handles {
- my ( $self, $options ) = @_;
+before '_process_options' => sub {
+ my ( $self, $name, $options ) = @_;
- if ( my $type = $self->helper_type ) {
- ( exists $options->{isa} )
- || confess "You must define a type with the $type metaclass";
+ $self->_check_helper_type( $options, $name );
- my $isa = $options->{isa};
+ $options->{is} = $self->_default_is
+ if ! exists $options->{is} && $self->can('_default_is');
- unless ( blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint') ) {
- $isa
- = Moose::Util::TypeConstraints::find_or_create_type_constraint(
- $isa);
- }
+ $options->{default} = $self->_default_default
+ if ! exists $options->{default} && $self->can('_default_default');
+};
- ( $isa->is_a_type_of($type) )
- || confess
- "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
- }
-}
+sub _check_helper_type {
+ my ( $self, $options, $name ) = @_;
-before '_process_options' => sub {
- my ( $self, $name, $options ) = @_;
- $self->process_options_for_handles( $options, $name );
-};
+ my $type = $self->helper_type;
+
+ $options->{isa} = $self->helper_type
+ unless exists $options->{isa};
+
+ my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
+ $options->{isa} );
+
+ ( $isa->is_a_type_of($type) )
+ || confess
+ "The type constraint for $name must be a subtype of $type but it's a $isa";
+}
around '_canonicalize_handles' => sub {
my $next = shift;
## methods called after instantiation
-before 'install_accessors' => sub { (shift)->check_handles_values };
+before 'install_accessors' => sub { (shift)->_check_handles_values };
-sub check_handles_values {
+sub _check_handles_values {
my $self = shift;
my $method_constructors = $self->method_constructors;
Moose::AttributeHelpers::Trait::Base - base role for helpers
-=head1 METHODS
-
-=head2 check_handles_values
-
-Confirms that handles has all valid possibilities in it.
-
-=head2 process_options_for_handles
-
-Ensures that the type constraint (C<isa>) matches the helper type.
-
=head1 BUGS
All complex software has bugs lurking in it, and this module is no
sub helper_type { 'Bool' }
+sub _default_is { 'rw' }
+
# NOTE:
# we don't use the method provider for this
# module since many of the names of the provied
default => 'Moose::AttributeHelpers::MethodProvider::Bool'
);
-before 'process_options_for_handles' => sub {
- my ( $self, $options, $name ) = @_;
-
- # Set some default attribute options here unless already defined
- if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) {
- $options->{isa} = $type;
- }
-};
-
no Moose::Role;
# register the alias ...
metaclass => 'Bool',
is => 'rw',
isa => 'Bool',
- default => sub { 0 },
+ default => 0,
handles => {
illuminate => 'set',
darken => 'unset',
sub helper_type { 'Bag' }
-before 'process_options_for_handles' => sub {
- my ( $self, $options, $name ) = @_;
-
- # Set some default attribute options here unless already defined
- if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) {
- $options->{isa} = $type;
- }
-
- $options->{default} = sub { +{} }
- unless exists $options->{default};
-};
+sub _default_default { sub { {} } }
no Moose::Role;
no Moose::Util::TypeConstraints;
default => 'Moose::AttributeHelpers::MethodProvider::Counter',
);
-sub helper_type { 'Num' }
-
-before 'process_options_for_handles' => sub {
- my ( $self, $options, $name ) = @_;
+sub _default_default { 0 }
+sub _default_is { 'ro' }
- # Set some default attribute options here unless already defined
- if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) {
- $options->{isa} = $type;
- }
-
- $options->{is} = 'ro' unless exists $options->{is};
- $options->{default} = 0 unless exists $options->{default};
-};
+sub helper_type { 'Num' }
-after 'check_handles_values' => sub {
+after '_check_handles_values' => sub {
my $self = shift;
my $handles = $self->handles;
metaclass => 'Counter',
is => 'ro',
isa => 'Num',
- default => sub { 0 },
+ default => 0,
handles => {
inc_counter => 'inc',
dec_counter => 'dec',
);
my $page = MyHomePage->new();
- $page->inc_counter; # same as $page->counter($page->counter + 1);
- $page->dec_counter; # same as $page->counter($page->counter - 1);
+ $page->inc_counter; # same as $page->counter( $page->counter + 1 );
+ $page->dec_counter; # same as $page->counter( $page->counter - 1 );
=head1 DESCRIPTION
=item B<helper_type>
-=item B<process_options_for_handles>
-
-Run before its superclass method.
-
-=item B<check_handles_values>
-
-Run after its superclass method.
-
=back
=head1 PROVIDED METHODS
metaclass => 'Number',
is => 'ro',
isa => 'Int',
- default => sub { 5 },
+ default => 5,
handles => {
set => 'set',
add => 'add',
sub helper_type { 'Str' }
-before 'process_options_for_handles' => sub {
- my ( $self, $options, $name ) = @_;
+sub _default_default { q{} }
+sub _default_is { 'rw' }
- # Set some default attribute options here unless already defined
- if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) {
- $options->{isa} = $type;
- }
-
- $options->{is} = 'rw' unless exists $options->{is};
- $options->{default} = '' unless exists $options->{default};
-};
-
-after 'check_handles_values' => sub {
+after '_check_handles_values' => sub {
my $self = shift;
my $handles = $self->handles;
metaclass => 'String',
is => 'rw',
isa => 'Str',
- default => sub { '' },
+ default => q{},
handles => {
add_text => 'append',
replace_text => 'replace',