blib/lib/MooseX/SetOnce.pm

filter: Statementstmt 81.2% 26 / 32 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod n/a totaltotal 79.1% 38 / 48 CC 2 SCAR 3.1
CC
2
Cov
79%
_ensure_unset
2.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.4
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package MooseX::SetOnce 0.203;
4 # ABSTRACT: write-once, read-many attributes for Moose
5  
6 #pod =head1 SYNOPSIS
7 #pod
8 #pod Add the "SetOnce" trait to attributes:
9 #pod
10 #pod package Class;
11 #pod use Moose;
12 #pod use MooseX::SetOnce;
13 #pod
14 #pod has some_attr => (
15 #pod is => 'rw',
16 #pod traits => [ qw(SetOnce) ],
17 #pod );
18 #pod
19 #pod ...and then you can only set them once:
20 #pod
21 #pod my $object = Class->new;
22 #pod
23 #pod $object->some_attr(10); # works fine
24 #pod $object->some_attr(20); # throws an exception: it's already set!
25 #pod
26 #pod =head1 DESCRIPTION
27 #pod
28 #pod The 'SetOnce' attribute lets your class have attributes that are not lazy and
29 #pod not set, but that cannot be altered once set.
30 #pod
31 #pod The logic is very simple: if you try to alter the value of an attribute with
32 #pod the SetOnce trait, either by accessor or writer, and the attribute has a value,
33 #pod it will throw an exception.
34 #pod
35 #pod If the attribute has a clearer, you may clear the attribute and set it again.
36 #pod
37 #pod =cut
38  
39 package MooseX::SetOnce::Attribute 0.203;
40 2 ▶use Moose::Role 0.90;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 12 ▶before set_value => sub { $_[0]->_ensure_unset($_[1]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
43  
44 6 ▶around _inline_set_value => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 6 my $orig = shift;
46 6 my $self = shift;
47 6 my ($instance) = @_;
48  
49 6 my @source = $self->$orig(@_);
50  
51 return (
52 6 'Class::MOP::class_of(' . $instance . ')->find_attribute_by_name(',
53 '\'' . quotemeta($self->name) . '\'',
54 ')->_ensure_unset(' . $instance . ');',
55 @source,
56 );
57 2 ▶} if $Moose::VERSION >= 1.9900;
Branch: 1 branch
BranchTrueFalseTotal
if $Moose::VERSION >= "1.99" 2 0 2
58  
59 34 ▶sub _ensure_unset {
Subroutine _ensure_unset: called CC=2 SCAR=6.9
60 34 my ($self, $instance) = @_;
61 34 ▶ Carp::confess("cannot change value of SetOnce attribute " . $self->name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_value($instance) 24 10 34
62 if $self->has_value($instance);
63 }
64  
65 0 ▶around accessor_metaclass => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my ($orig, $self, @rest) = @_;
67  
68 0 return Moose::Meta::Class->create_anon_class(
69 superclasses => [ $self->$orig(@_) ],
70 roles => [ 'MooseX::SetOnce::Accessor' ],
71 cache => 1
72 )->name
73 2 ▶} if $Moose::VERSION < 1.9900;
Branch: 1 branch
BranchTrueFalseTotal
if $Moose::VERSION < "1.99" 0 2 2
74  
75 package MooseX::SetOnce::Accessor 0.203;
76 2 ▶use Moose::Role 0.90;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 0 ▶around _inline_store => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 my ($orig, $self, $instance, $value) = @_;
80  
81 0 my $code = $self->$orig($instance, $value);
82 0 $code = sprintf qq[%s->meta->find_attribute_by_name("%s")->_ensure_unset(%s);\n%s],
83 $instance,
84 quotemeta($self->associated_attribute->name),
85 $instance,
86 $code;
87  
88 0 return $code;
89 2 };
90  
91 package Moose::Meta::Attribute::Custom::Trait::SetOnce 0.203;
92 2 ▶sub register_implementation { 'MooseX::SetOnce::Attribute' }
Subroutine register_implementation: called CC=1 SCAR=0.0
93  
94 2 1;
95  
96 __END__
↑ Summary