| 1 | package Bolts::Meta::Attribute::Traits::Initializer; | ||||||||||||
| 2 | 11 | $Bolts::Meta::Attribute::Traits::Initializer::VERSION = '0.143171'; | |||||||||||
| 3 | # ABSTRACT: Build an attribute with an initializer | ||||||||||||
| 4 | |||||||||||||
| 5 | 11 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 11 | ▶ | use Safe::Isa; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 11 | Moose::Util::meta_attribute_alias('Bolts::Initializer'); | |||||||||||
| 9 | |||||||||||||
| 10 | |||||||||||||
| 11 | # TODO Make this into a helper class so that other kinds of init can be added | ||||||||||||
| 12 | # and customized later. | ||||||||||||
| 13 | 11 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 11 | has init_type => ( | |||||||||||
| 15 | is => 'ro', | ||||||||||||
| 16 | isa => enum([qw( Array Scalar )]), | ||||||||||||
| 17 | required => 1, | ||||||||||||
| 18 | default => 'Scalar', | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | 11 | ▶ | no Moose::Util::TypeConstraints; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | |||||||||||||
| 22 | |||||||||||||
| 23 | 11 | has special_initializer => ( | |||||||||||
| 24 | is => 'ro', | ||||||||||||
| 25 | isa => 'Str', | ||||||||||||
| 26 | required => 1, | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | |||||||||||||
| 29 | 11 | has _original_default => ( | |||||||||||
| 30 | is => 'rw', | ||||||||||||
| 31 | predicate => '_has_original_default', | ||||||||||||
| 32 | ); | ||||||||||||
| 33 | |||||||||||||
| 34 | 52 | ▶ | before install_accessors => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 35 | 52 | my $self = shift; | |||||||||||
| 36 | 52 | my $meta = $self->associated_class; | |||||||||||
| 37 | |||||||||||||
| 38 | 52 | ▶ | $meta->add_attribute($self->special_initializer => ( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | is => 'ro', | ||||||||||||
| 40 | required => $self->is_required, | ||||||||||||
| 41 | init_arg => $self->name, | ||||||||||||
| 42 | ($self->_has_original_default ? ( | ||||||||||||
| 43 | default => $self->_original_default | ||||||||||||
| 44 | ) : ()), | ||||||||||||
| 45 | )); | ||||||||||||
| 46 | 11 | }; | |||||||||||
| 47 | |||||||||||||
| 48 | 52 | ▶ | before _process_options => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=18.9
| |||||||||||||
| 49 | 52 | my ($self, $name, $options) = @_; | |||||||||||
| 50 | |||||||||||||
| 51 | # Having these here is probably a sign that we're doing this wrong. | ||||||||||||
| 52 | # Should probably just have the default call some predefined subroutine | ||||||||||||
| 53 | # instead and skip these bits here. | ||||||||||||
| 54 | 52 | ▶ | $options->{special_initializer} //= '_' . $name . '_initializer'; | ||||||||||
|
Condition50%1/2
$options->{"special_initializer"} //= '_' . $name . '_initializer'
MC/DC0%0/1
$options->{"special_initializer"} //= '_' . $name . '_initializer'
$options->{"special_initializer"}
Truth table50%1/2
A
A= $options->{"special_initializer"}
| |||||||||||||
| 55 | 52 | ▶ | $options->{init_type} //= 'Scalar'; | ||||||||||
|
Condition50%1/2
$options->{"init_type"} //= "Scalar"
MC/DC0%0/1
$options->{"init_type"} //= "Scalar"
$options->{"init_type"}
Truth table50%1/2
A
A= $options->{"init_type"}
| |||||||||||||
| 56 | |||||||||||||
| 57 | 52 | my $_initializer = $options->{special_initializer}; | |||||||||||
| 58 | |||||||||||||
| 59 | $options->{_original_default} = delete $options->{default} | ||||||||||||
| 60 | 52 | ▶ | if exists $options->{default}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | |||||||||||||
| 62 | 52 | $options->{init_arg} = undef; | |||||||||||
| 63 | 52 | $options->{lazy} = 1; | |||||||||||
| 64 | |||||||||||||
| 65 | 52 | ▶ | if ($options->{init_type} eq 'Scalar') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 388 | ▶ | $options->{default} = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 67 | 388 | my $self = shift; | |||||||||||
| 68 | |||||||||||||
| 69 | 388 | my $init = $self->$_initializer; | |||||||||||
| 70 | 388 | ▶ | if ($init->$_isa('Bolts::Meta::Initializer')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 2 | return $self->initialize_value($init->get); | |||||||||||
| 72 | } | ||||||||||||
| 73 | else { | ||||||||||||
| 74 | 386 | return $init; | |||||||||||
| 75 | } | ||||||||||||
| 76 | 52 | }; | |||||||||||
| 77 | } | ||||||||||||
| 78 | else { | ||||||||||||
| 79 | 0 | ▶ | $options->{default} = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 80 | 0 | my $self = shift; | |||||||||||
| 81 | |||||||||||||
| 82 | 0 | my @values; | |||||||||||
| 83 | 0 | my $init_array = $self->$_initializer; | |||||||||||
| 84 | 0 | for my $init (@$init_array) { | |||||||||||
| 85 | 0 | ▶ | if ($init->$_isa('Bolts::Meta::Initializer')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | push @values, $self->initialize_value($init->get); | |||||||||||
| 87 | } | ||||||||||||
| 88 | else { | ||||||||||||
| 89 | 0 | push @values, $init; | |||||||||||
| 90 | } | ||||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 0 | return \@values; | |||||||||||
| 94 | 0 | }; | |||||||||||
| 95 | } | ||||||||||||
| 96 | 11 | }; | |||||||||||
| 97 | |||||||||||||
| 98 | 11 | 1; | |||||||||||
| 99 | |||||||||||||
| 100 | __END__ | ||||||||||||