1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#
# This file is part of MooseX-AttributeShortcuts
#
# This software is Copyright (c) 2017, 2015, 2014, 2013, 2012, 2011 by Chris Weyl.
#
# This is free software, licensed under:
#
# The GNU Lesser General Public License, Version 2.1, February 1999
#
package MooseX::AttributeShortcuts::Trait::Role::Attribute;
our $AUTHORITY = 'cpan:RSRCHBOY';
$MooseX::AttributeShortcuts::Trait::Role::Attribute::VERSION = '0.037';
# ABSTRACT: Role attribute trait to create builder method
use MooseX::Role::Parameterized;
use namespace::autoclean 0.24;
use MooseX::Types::Common::String ':all';
use MooseX::Util;
use aliased 'MooseX::AttributeShortcuts::Trait::Role::Method::Builder'
=> 'RoleBuilderTrait';
with 'MooseX::AttributeShortcuts::Trait::Attribute::HasAnonBuilder';
parameter builder_prefix => (isa => NonEmptySimpleStr, default => '_build_');
sub builder_method_metaclass {
my $self = shift @_;
# this mirrors the approach taken by
# Moose::Meta::Role::Attribute->attribute_for_class()
return with_traits(
$self->original_role->method_metaclass,
RoleBuilderTrait,
);
}
# no POD, as this is "private". If a role is composed into another role, the
# role attributes are cloned into the new role using original_options. In
# order to prevent us from installing the same build method twice, we poke at
# original_options to ensure the information is propagated correctly.
after _set_anon_builder_installed => sub {
my $self = shift;
$self->original_options->{anon_builder_installed} = 1;
return;
};
after attach_to_role => sub {
my ($self, $role) = @_;
### has anon builder?: $self->has_anon_builder
return unless $self->has_anon_builder && !$self->anon_builder_installed;
### install our anon builder as a method: $role->name
$role->add_method($self->builder => $self->_builder_method_meta($role));
$self->_set_anon_builder_installed;
return;
};
role {
my $p = shift @_;
method canonical_builder_prefix => sub { $p->builder_prefix };
around new => sub {
# my ($orig, $class) = (shift, shift);
my ($orig, $class, $name, %options) = @_;
# just pass to the original new() if we don't have an anon builder
return $class->$orig($name => %options)
unless exists $options{builder} && (ref $options{builder} || q{}) eq 'CODE';
# stash anon_builder, set builder => 1
$options{anon_builder} = $options{builder};
$options{builder} = $class->_mxas_builder_name($name);
### %options
### anon builder: $options{builder}
return $class->$orig($name => %options);
};
};
!!42;
__END__
=pod
=encoding UTF-8
=for :stopwords Chris Weyl Alders David Etheridge Graham Karen Knop Olaf Steinbrunner
=head1 NAME
MooseX::AttributeShortcuts::Trait::Role::Attribute - Role attribute trait to create builder method
=head1 VERSION
This document describes version 0.037 of MooseX::AttributeShortcuts::Trait::Role::Attribute - released November 20, 2017 as part of MooseX-AttributeShortcuts.
=head1 DESCRIPTION
Normally, attribute options processing takes place at the time an attribute is
created and attached to a class, either by virtue of a C<has> statement in a
class definition or when a role is applied to a class.
This is not an optimal approach for inline builder methods.
This is a role attribute trait, to create builder methods when role attributes
are created, so that they can be aliased, excluded, etc, like any other role
method.
=head1 ROLE PARAMETERS
Parameterized roles accept parameters that influence their construction. This role accepts the following parameters.
=head2 builder_prefix
=head1 AROUND METHOD MODIFIERS
=head2 new
If we have an inline builder defined in our role options, swizzle our options
such that C<builder> becomes the builder method name, and C<anon_builder> is
the anonymous sub.
=head1 AFTER METHOD MODIFIERS
=head2 attach_to_role
If we have an inline builder defined in our role options, install it as a
method.
=head1 METHODS
=head2 builder_method_metaclass()
Returns the metaclass we'll use to install a inline builder.
=head1 SEE ALSO
Please see those modules/websites for more information related to this module.
=over 4
=item *
L<MooseX::AttributeShortcuts|MooseX::AttributeShortcuts>
=back
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website
L<https://github.com/RsrchBoy/moosex-attributeshortcuts/issues>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 AUTHOR
Chris Weyl <cweyl@alumni.drew.edu>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2017, 2015, 2014, 2013, 2012, 2011 by Chris Weyl.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999
=cut
|