Maker/Type.pm

filter: Statementstmt 98.1% 105 / 107 Branchbran 69.4% 25 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 96.6% 29 / 30 Podpod 9.0% 1 / 11 totaltotal 86.9% 160 / 184 CC 19 SCAR 8.4
CC
19
Cov
91%
_findtimes
5.2
add
4.0
_ref
3.1
CRAP 2.3
« Maker/Option.pm ↑ Summary Maker/Path/Meta.pm »
1 package Config::Maker::Type;
2  
3 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Config::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Config::Maker::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 require Config::Maker::Option;
12 9 our @CARP_NOT = qw(Config::Maker::Option);
13  
14 use overload
15 9 ▶ 'cmp' => \&Config::Maker::truecmp,
Subroutine BEGIN: called CC=1 SCAR=0.0
16 '<=>' => \&Config::Maker::truecmp,
17 '""' => 'name',
18 9 fallback => 1;
19  
20 9 our $root;
21 9 our $meta;
22 9 our $repository;
23 9 our %checks;
24  
25 # Get top-level context ('/' special type):
26 94 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod undocumented
27 94 $root;
28 }
29  
30 # Get special meta content ('//' special type)
31 229 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
32 229 $meta;
33 }
34  
35 # Get repository context ('*' special type). The repository is a special type
36 # where types for reuse are stored:
37 128 ▶sub repository {
Subroutine repository: called CC=1 SCAR=0.0
Pod undocumented
38 128 $repository;
39 }
40  
41 # Get a type from a context. Context is just a type currently being
42 # built:
43 952 ▶sub get {
Subroutine get: called CC=2 SCAR=7.1
Pod undocumented
44 952 my ($ctx, $name) = @_;
45
46 952 ▶ croak "No type $name in $ctx" unless $ctx->{children}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx->{"children"}{$name} 0 952 952
47 952 return $ctx->{children}{$name};
48 }
49  
50 # Build an Option using given arguments:
51 783 ▶sub instantiate {
Subroutine instantiate: called CC=2 SCAR=7.1
Pod undocumented
52 783 my ($self, $args) = @_;
53  
54 783 ▶ ref $args eq 'HASH' or confess "Not a hash reference!";
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq "HASH" or confess("Not a hash reference!") 783 0 783
55 783 Config::Maker::Option->new(-type => $self, %$args);
56 }
57  
58 # Get arguments for syntactic rule for arguments.
59 689 ▶sub body {
Subroutine body: called CC=1 SCAR=0.0
Pod undocumented
60 689 my ($self) = @_;
61 689 @{$self->{format}};
62 }
63  
64 # Get the name of the option type.
65 13807 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
66 13807 my ($self) = @_;
67 13807 $self->{name};
68 }
69  
70 # Build a new type...
71 1494 ▶sub _ref(\%$;$) {
Subroutine _ref: called CC=3 SCAR=11.2
72 1494 my ($hash, $key, $default) = @_;
73 1494 ▶ if(exists $hash->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $hash->{$key}) { } 804 690 1494
elsif (@_ == 3) { } 690 0 690
74 804 my $rv = $hash->{$key};
75 804 delete $hash->{$key};
76 804 return $rv;
77 } elsif(@_ == 3) {
78 690 return $default;
79 } else {
80 0 croak "Mandatory argument $key not specified";
81 }
82 }
83  
84 640 ▶sub _type {
Subroutine _type: called CC=3 SCAR=11.0
85 640 my ($type) = @_;
86 640 ▶ confess "_type($type)" unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 640 640
87 640 ▶ return $type if UNIVERSAL::isa($type, __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($type, __PACKAGE__) 512 128 640
88 128 return __PACKAGE__->repository->get($type);
89 }
90  
91 364 ▶sub _path {
Subroutine _path: called CC=1 SCAR=0.0
92 364 return Config::Maker::Path->make(@_);
93 }
94  
95 364 ▶sub _check {
Subroutine _check: called CC=2 SCAR=7.0
96 364 my ($check, $path) = @_;
97 364 ▶ croak "No check $check" unless $checks{$check};
Branch: 1 branch
BranchTrueFalseTotal
unless $Config::Maker::Type::checks{$check} 0 364 364
98 364 $check = $checks{$check};
99 364 $path = _path($path);
100 1601 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 1601 $check->($_[0], $path);
102 }
103 364 }
104  
105 249 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
106 249 my $class = shift;
107 249 ▶ my %args = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 24 225 249
108 249 my %type = (
109 name => _ref(%args, 'name'),
110 format => _ref(%args, 'format'),
111 children => {},
112 checks => [],
113 actions => _ref(%args, 'actions', []),
114 );
115 249 my $self = bless \%type, $class;
116  
117 # Children...
118 249 $self->add(@{_ref %args, 'children', []});
119  
120 # Aditional checks...
121 249 $self->addchecks(@{_ref %args, 'checks', []});
122  
123 249 $self->addto(@{_ref %args, 'contexts', [any => '*']});
124  
125 249 ▶ croak "Unknown arguments: " . join(', ', keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 249 249
126 if %args;
127 249 return $self;
128 }
129  
130 575 ▶sub add {
Subroutine add: called CC=4 SCAR=14.0
Pod undocumented
131 575 my $self = shift;
132 575 my @children = @_;
133 575 ▶ while(my ($spec, $type) = splice(@children, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@children, 0, 2)) 337 575 912
134 337 $type = _type($type);
135 337 ▶ next if $self->{children}->{$type} eq $type;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"children"}{$type} eq $type 0 337 337
136 croak "Different type named $type already added to $self"
137 337 ▶ if exists $self->{children}->{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"children"}{$type} 0 337 337
138 337 $self->{children}->{$type->name} = $type;
139 337 push @{$self->{checks}}, _check($spec, "$type");
140 }
141 }
142  
143 249 ▶sub addto {
Subroutine addto: called CC=2 SCAR=6.9
Pod undocumented
144 249 my $self = shift;
145 249 my @contexts = @_;
146 249 ▶ while(my ($spec, $ctx) = splice(@contexts, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@contexts, 0, 2)) 303 249 552
147 303 _type($ctx)->add($spec, $self);
148 }
149 }
150  
151 276 ▶sub addchecks {
Subroutine addchecks: called CC=2 SCAR=6.9
Pod undocumented
152 276 my $self = shift;
153 276 my @checks = @_;
154 276 ▶ while(my ($spec, $path) = splice(@checks, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@checks, 0, 2)) 27 276 303
155 27 push @{$self->{checks}}, _check($spec, $path);
156 }
157 }
158  
159 # Initialize:
160  
161 # format not used for the magic items, but is mandatory...
162 9 $repository = __PACKAGE__->new(
163 name => '*',
164 format => [],
165 contexts => [], # self-reference otherwise...
166 );
167  
168 9 $repository->add(any => $repository);
169 # Phew, and now _type should work...
170  
171 9 $root = __PACKAGE__->new(
172 name => '/',
173 format => [],
174 ); # Default context (in repository only) should work here...
175  
176 # And last but not least the meta type...
177 9 $meta = __PACKAGE__->new(
178 name => '//',
179 format => [],
180 ); # Default context (in repository only) should work here too...
181  
182 # Checking functions:
183  
184 972 ▶sub _findtimes {
Subroutine _findtimes: called CC=5 SCAR=16.6
185 972 ▶ confess "$_[1] can't ->find" unless UNIVERSAL::can($_[1], 'find');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($_[1], "find") 0 972 972
186 972 my $r = $_[1]->find($_[0]);
187 972 ▶ croak "Too few $_[1] in " . $_[0]->id if @$r < $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if @$r < $_[2] 0 972 972
188 972 ▶ return 1 if @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 3 45 927 972
189 927 ▶ croak "Too many $_[1] in " . $_[0]->id if @$r > $_[3];
Branch: 1 branch
BranchTrueFalseTotal
if @$r > $_[3] 0 927 927
190 927 return 1;
191 }
192  
193 9 ▶BEGIN { # Constants must be done early enough...
Subroutine BEGIN: called CC=1 SCAR=0.0
194 %checks = (
195 0 ▶ none => sub { _findtimes(@_, 0,0); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 850 ▶ opt => sub { _findtimes(@_,0,1); },
Subroutine __ANON__: called CC=1 SCAR=0.0
197 77 ▶ one => sub { _findtimes(@_,1,1); },
Subroutine __ANON__: called CC=1 SCAR=0.0
198 45 ▶ mand => sub { _findtimes(@_,1); },
Subroutine __ANON__: called CC=1 SCAR=0.0
199 629 ▶ any => sub { 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
200 9 );
201 }
202  
203 9 1;
204  
205 __END__
« Maker/Option.pm ↑ Summary Maker/Path/Meta.pm »