« AttributeShortcuts/Trait/Attribute.pm
↑ Summary
AttributeShortcuts/Trait/Attribute/HasAnonBuilder.pm »
| 1 | # | ||||||||||||||||||||||||||||||||
| 2 | # This file is part of MooseX-AttributeShortcuts | ||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2017, 2015, 2014, 2013, 2012, 2011 by Chris Weyl. | ||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||
| 10 | package MooseX::AttributeShortcuts::Trait::Role::Attribute; | ||||||||||||||||||||||||||||||||
| 11 | 30 | our $AUTHORITY = 'cpan:RSRCHBOY'; | |||||||||||||||||||||||||||||||
| 12 | 30 | $MooseX::AttributeShortcuts::Trait::Role::Attribute::VERSION = '0.037'; | |||||||||||||||||||||||||||||||
| 13 | # ABSTRACT: Role attribute trait to create builder method | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 30 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 30 | ▶ | use namespace::autoclean 0.24; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 30 | ▶ | use MooseX::Types::Common::String ':all'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 30 | ▶ | use MooseX::Util; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 30 | ▶ | use aliased 'MooseX::AttributeShortcuts::Trait::Role::Method::Builder' | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | 30 | => 'RoleBuilderTrait'; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 30 | with 'MooseX::AttributeShortcuts::Trait::Attribute::HasAnonBuilder'; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 30 | parameter builder_prefix => (isa => NonEmptySimpleStr, default => '_build_'); | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 6 | ▶ | sub builder_method_metaclass { | ||||||||||||||||||||||||||||||
|
Subroutine
builder_method_metaclass: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 32 | 6 | my $self = shift @_; | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | # this mirrors the approach taken by | ||||||||||||||||||||||||||||||||
| 35 | # Moose::Meta::Role::Attribute->attribute_for_class() | ||||||||||||||||||||||||||||||||
| 36 | 6 | return with_traits( | |||||||||||||||||||||||||||||||
| 37 | $self->original_role->method_metaclass, | ||||||||||||||||||||||||||||||||
| 38 | RoleBuilderTrait, | ||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # no POD, as this is "private". If a role is composed into another role, the | ||||||||||||||||||||||||||||||||
| 43 | # role attributes are cloned into the new role using original_options. In | ||||||||||||||||||||||||||||||||
| 44 | # order to prevent us from installing the same build method twice, we poke at | ||||||||||||||||||||||||||||||||
| 45 | # original_options to ensure the information is propagated correctly. | ||||||||||||||||||||||||||||||||
| 46 | 6 | ▶ | after _set_anon_builder_installed => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 47 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 6 | $self->original_options->{anon_builder_installed} = 1; | |||||||||||||||||||||||||||||||
| 50 | 6 | return; | |||||||||||||||||||||||||||||||
| 51 | 30 | }; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 13 | ▶ | after attach_to_role => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 54 | 13 | my ($self, $role) = @_; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | ### has anon builder?: $self->has_anon_builder | ||||||||||||||||||||||||||||||||
| 57 | 13 | ▶ | return unless $self->has_anon_builder && !$self->anon_builder_installed; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_anon_builder and not $self->anon_builder_installed
MC/DC100%2/2
$self->has_anon_builder and not $self->anon_builder_installed
$self->has_anon_builder
not $self->anon_builder_installed
Truth table100%3/3
A and not(B)
A= $self->has_anon_builderB= not $self->anon_builder_installed
| |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | ### install our anon builder as a method: $role->name | ||||||||||||||||||||||||||||||||
| 60 | 6 | $role->add_method($self->builder => $self->_builder_method_meta($role)); | |||||||||||||||||||||||||||||||
| 61 | 6 | $self->_set_anon_builder_installed; | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 6 | return; | |||||||||||||||||||||||||||||||
| 64 | 30 | }; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 8 | ▶ | role { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 67 | 8 | my $p = shift @_; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | method canonical_builder_prefix => sub { $p->builder_prefix }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
canonical_builder_prefix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 13 | ▶ | around new => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 72 | # my ($orig, $class) = (shift, shift); | ||||||||||||||||||||||||||||||||
| 73 | 13 | my ($orig, $class, $name, %options) = @_; | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | # just pass to the original new() if we don't have an anon builder | ||||||||||||||||||||||||||||||||
| 76 | return $class->$orig($name => %options) | ||||||||||||||||||||||||||||||||
| 77 | 13 | ▶ | unless exists $options{builder} && (ref $options{builder} || q{}) eq 'CODE'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
exists $options{"builder"} and (ref $options{"builder"} || "") eq "CODE"
ref $options{'builder'} || ''
MC/DC100%2/2
exists $options{"builder"} and (ref $options{"builder"} || "") eq "CODE"
exists $options{"builder"}
ref $options{'builder'}
Truth table100%3/3
A and B
A= exists $options{"builder"}B= ref $options{'builder'}
| |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | # stash anon_builder, set builder => 1 | ||||||||||||||||||||||||||||||||
| 80 | 6 | $options{anon_builder} = $options{builder}; | |||||||||||||||||||||||||||||||
| 81 | 6 | $options{builder} = $class->_mxas_builder_name($name); | |||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | ### %options | ||||||||||||||||||||||||||||||||
| 84 | ### anon builder: $options{builder} | ||||||||||||||||||||||||||||||||
| 85 | 6 | return $class->$orig($name => %options); | |||||||||||||||||||||||||||||||
| 86 | 8 | }; | |||||||||||||||||||||||||||||||
| 87 | 30 | }; | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 30 | !!42; | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||||||