| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | package Class::Tiny::Immutable; | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Class::Tiny (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | our @ISA = 'Class::Tiny'; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | my %REQUIRED_ATTRIBUTES; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub prepare_class { | ||||||||||||||||||||||||||||||
|
Subroutine
prepare_class: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 15 | 1 | my ( $class, $pkg ) = @_; | |||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | @{"${pkg}::ISA"} = "Class::Tiny::Immutable::Object" unless @{"${pkg}::ISA"}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub create_attributes { | ||||||||||||||||||||||||||||||
|
Subroutine
create_attributes: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 21 | 1 | my ( $class, $pkg, @spec ) = @_; | |||||||||||||||||||||||||||||||
| 22 | 1 | $class->SUPER::create_attributes( $pkg, @spec ); | |||||||||||||||||||||||||||||||
| 23 | 1 | $REQUIRED_ATTRIBUTES{$pkg}{$_} = 1 for grep { ref $_ ne 'HASH' } @spec; | |||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | sub __gen_sub_body { | ||||||||||||||||||||||||||||||
|
Subroutine
__gen_sub_body: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||
| 27 | 4 | my ($self, $name, $has_default, $default_type) = @_; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 4 | ▶ | if ($has_default && $default_type eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$has_default and $default_type eq "CODE"
MC/DC0%0/2
$has_default and $default_type eq "CODE"
$has_default
$default_type eq "CODE"
Truth table66%2/3
A and B
A= $has_defaultB= $default_type eq "CODE"
| |||||||||||||||||||||||||||||||||
| 30 | 0 | return <<"HERE"; | |||||||||||||||||||||||||||||||
| 31 | sub $name { | ||||||||||||||||||||||||||||||||
| 32 | return ( | ||||||||||||||||||||||||||||||||
| 33 | ( \@_ == 1 ) | ||||||||||||||||||||||||||||||||
| 34 | ? ( exists \$_[0]{$name} ? \$_[0]{$name} : ( \$_[0]{$name} = \$default->( \$_[0] ) ) ) | ||||||||||||||||||||||||||||||||
| 35 | : Carp::croak( "$name is a read-only accessor" ) | ||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | HERE | ||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | elsif ($has_default) { | ||||||||||||||||||||||||||||||||
| 41 | 1 | return <<"HERE"; | |||||||||||||||||||||||||||||||
| 42 | sub $name { | ||||||||||||||||||||||||||||||||
| 43 | return ( | ||||||||||||||||||||||||||||||||
| 44 | ( \@_ == 1 ) | ||||||||||||||||||||||||||||||||
| 45 | ? ( exists \$_[0]{$name} ? \$_[0]{$name} : ( \$_[0]{$name} = \$default ) ) | ||||||||||||||||||||||||||||||||
| 46 | : Carp::croak( "$name is a read-only accessor" ) | ||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | HERE | ||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||||||
| 52 | 3 | return <<"HERE"; | |||||||||||||||||||||||||||||||
| 53 | sub $name { | ||||||||||||||||||||||||||||||||
| 54 | return \@_ == 1 ? \$_[0]{$name} : Carp::croak( "$name is a read-only accessor" ); | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | HERE | ||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 5 | ▶ | sub get_all_required_attributes_for { | ||||||||||||||||||||||||||||||
|
Subroutine
get_all_required_attributes_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 61 | 5 | my ( $class, $pkg ) = @_; | |||||||||||||||||||||||||||||||
| 62 | # attributes are stored per package, so we need to walk the mro ourselves | ||||||||||||||||||||||||||||||||
| 63 | # rely on Class::Tiny to have loaded the appropriate mro | ||||||||||||||||||||||||||||||||
| 64 | my %attr = | ||||||||||||||||||||||||||||||||
| 65 | 10 | map { $_ => undef } | |||||||||||||||||||||||||||||||
| 66 | 5 | ▶ | map { keys %{ $REQUIRED_ATTRIBUTES{$_} || {} } } @{ mro::get_linear_isa($pkg) }; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$REQUIRED_ATTRIBUTES{$_} || {}
MC/DC100%1/1
$REQUIRED_ATTRIBUTES{$_} || {}
$REQUIRED_ATTRIBUTES{$_}
Truth table100%2/2
A
A= $REQUIRED_ATTRIBUTES{$_}
| |||||||||||||||||||||||||||||||||
| 67 | 5 | return keys %attr; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | package Class::Tiny::Immutable::Object; | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 1 | our @ISA = 'Class::Tiny::Object'; | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 1 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 77 | 3 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||||||
| 78 | 3 | my @missing = grep { !exists $args->{$_} } | |||||||||||||||||||||||||||||||
| 79 | Class::Tiny::Immutable->get_all_required_attributes_for( ref $self ); | ||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | Carp::croak( 'Missing required attributes: ' . join( ', ', sort @missing ) ) if @missing; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 1 | 1; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | Class::Tiny::Immutable - Minimalist class construction, with read-only | ||||||||||||||||||||||||||||||||
| 88 | attributes | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | In I<Person.pm>: | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | package Person; | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | use Class::Tiny::Immutable qw( name ); | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 1; | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | In I<Employee.pm>: | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | package Employee; | ||||||||||||||||||||||||||||||||
| 103 | use parent 'Person'; | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | use Class::Tiny::Immutable qw( ssn ), { | ||||||||||||||||||||||||||||||||
| 106 | timestamp => sub { time } # lazy attribute with default | ||||||||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 1; | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | In I<example.pl>: | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | use Employee; | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | my $obj = Employee->new; # dies, name and ssn attributes are required | ||||||||||||||||||||||||||||||||
| 116 | my $obj = Employee->new( name => "Larry", ssn => "111-22-3333" ); | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | my $name = $obj->name; | ||||||||||||||||||||||||||||||||
| 119 | my $timestamp = $obj->timestamp; | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | # no attributes can be set | ||||||||||||||||||||||||||||||||
| 122 | $obj->ssn("222-33-4444"); # dies | ||||||||||||||||||||||||||||||||
| 123 | $obj->timestamp(time); # dies | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | L<Class::Tiny::Immutable> is a wrapper around L<Class::Tiny> which makes the | ||||||||||||||||||||||||||||||||
| 128 | generated attributes read-only, and required to be set in the object | ||||||||||||||||||||||||||||||||
| 129 | constructor if they do not have a lazy default defined. In other words, | ||||||||||||||||||||||||||||||||
| 130 | attributes are either "lazy" or "required". | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | In addition to methods inherited from L<Class::Tiny>, Class::Tiny::Immutable | ||||||||||||||||||||||||||||||||
| 135 | defines the following additional introspection method: | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | =head2 get_all_required_attributes_for | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | my @required = Class::Tiny::Immutable->get_all_required_attributes_for($class); | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | Returns an unsorted list of required attributes known to Class::Tiny::Immutable | ||||||||||||||||||||||||||||||||
| 142 | for a class and its superclasses. | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | =head1 BUGS | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | Report any issues on the public bugtracker. | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | Dan Book <dbook@cpan.org> | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | This software is Copyright (c) 2017 by Dan Book. | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | L<Moo>, L<MooseX::AttributeShortcuts> | ||||||||||||||||||||||||||||||||