blib/lib/Marlin/XAttribute/Lvalue.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Marlin::XAttribute::Lvalue;
6  
7 our $AUTHORITY = 'cpan:TOBYINK';
8 our $VERSION = '0.020000';
9  
10 1 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Eval::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use LV ();
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Marlin::Attribute ();
14 use Scalar::Util ();
15  
16 # This is just a Marlin::Role which will be applied to Marlin::Attribute
17 use Marlin::Role;
18  
19 after canonicalize_is => sub {
20 my $me = shift;
21
22 if ( not ref $me->{':Lvalue'} ) {
23 my $method_name = $me->{':Lvalue'};
24 $me->{':Lvalue'} = { method_name => $method_name, try => !!0 };
25 }
26  
27 if ( $me->{':Lvalue'}{method_name} eq 1 ) {
28 $me->{':Lvalue'}{method_name} = $me->{slot};
29 }
30
31 # If user has requested an lvalue method that has the same name
32 # as a reader or accessor, assume they don't want the reader or
33 # accessor!
34 my $method_name = $me->{':Lvalue'}{method_name};
35 for my $thing ( qw/ reader accessor / ) {
36 delete $me->{$thing} if defined $me->{$thing} && $me->{$thing} eq $method_name;
37 }
38
39 # We might have removed the reader and/or accessor, leaving this attribute
40 # without any reader, writer, accessor, predicate, or clearer, making it
41 # technically bare. That doesn't really matter to us, but if inflating to
42 # Moose, it will whine about a non-bare attribute which has no methods
43 # associated with it.
44 unless ( grep { defined $me->{$_} } qw/ reader writer accessor predicate clearer / ) {
45 $me->{is} = 'bare';
46 }
47 };
48  
49 after install_accessors => sub {
50 my $me = shift;
51
52 my $pkg = $me->{package};
53 my $method_name = $me->{':Lvalue'}{method_name};
54
55 if ( Marlin::Attribute::HAS_CXSA and $me->has_simple_accessor ) {
56 Class::XSAccessor->import(
57 class => $pkg,
58 lvalue_accessors => { $method_name => $me->{slot} },
59 );
60 return;
61 }
62
63 $me->install_coderef( $method_name, $me->Lvalue );
64 };
65  
66 around provides_accessors => sub {
67 my $next = shift;
68 my $me = shift;
69
70 my @list = $me->$next( @_ );
71 push @list, [ $me->{':Lvalue'}{method_name}, 'lvalue accessor', $me ];
72
73 return @list;
74 };
75  
76 sub Lvalue {
77 my $me = shift;
78
79 # inline_to_coderef doesn't actually expect any "sub{" and "}" to wrap
80 # the code and will add that itself, but we need to include that to add
81 # the ":lvalue" attribute. So we need to do this little trick afterwards
82 # to remove the "sub {...}" wrapper that inline_to_coderef adds.
83 my $coderef = $me->inline_to_coderef( 'lvalue accessor' => qq{
84 +sub :lvalue {
85 my \$self = shift;
86 LV::lvalue
87 LV::get { @{[ $me->inline_reader('$self') ]} }
88 LV::set { @{[ $me->inline_writer('$self', '$_[0]') ]} }
89 }
90 } );
91 $coderef = $coderef->(); # Little trick
92 }
93  
94 my $missing_mxlva_warning;
95 after injected_metadata => sub {
96 my ( $me, $framework, $meta_attr ) = @_;
97
98 return unless my $method = $me->{':Lvalue'}{method_name};
99
100 if ( $framework eq 'Moose' ) {
101
102 if ( not eval { require MooseX::LvalueAttribute; 1 } ) {
103 if ( not $missing_mxlva_warning++ ) {
104 Marlin::Util::_carp 'MooseX::LvalueAttribute is not installed';
105 }
106 return;
107 }
108
109 require Moose::Util;
110 require MooseX::LvalueAttribute::Trait::Accessor;
111 require MooseX::LvalueAttribute::Trait::Attribute;
112
113 Moose::Util::ensure_all_roles(
114 $meta_attr,
115 'MooseX::LvalueAttribute::Trait::Attribute',
116 );
117
118 my $accessor = Moose::Meta::Method::Accessor->_new(
119 accessor_type => 'accessor',
120 attribute => $meta_attr,
121 name => $me->{slot},
122 body => defined( &{ $me->{package} . "::$method" } ) ? \&{ $me->{package} . "::$method" } : $me->Lvalue,
123 package_name => $me->{package},
124 definition_context => +{ %{ $meta_attr->{definition_context} } },
125 );
126 Moose::Util::ensure_all_roles(
127 $accessor,
128 'MooseX::LvalueAttribute::Trait::Accessor',
129 );
130 Scalar::Util::weaken( $accessor->{attribute} );
131 $meta_attr->associate_method( $accessor );
132
133 my $meta_class = Moose::Util::find_meta($meta_attr->associated_class);
134 $meta_class->make_mutable;
135 $meta_class->add_method( $accessor->name, $accessor );
136 $meta_class->make_immutable;
137
138 $me->injected_accessor_metadata( Moose => $accessor );
139 }
140 };
141  
142 __PACKAGE__
143 __END__
↑ Summary