lib/Badger/Data/Type.pm

filter: Statementstmt 88.6% 39 / 44 Branchbran 58.3% 7 / 12 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 75.3% 61 / 81 CC 11 SCAR 13.7
CC
11
Cov
78%
constrain
6.1
_JUST_TESTING_clause
6.0
name
3.1
CRAP 3.9
« blib/lib/Badger/Data/Facet/Class.pm ↑ Summary lib/Badger/Factory/Class.pm »
1 #========================================================================
2 #
3 # Badger::Data::Type
4 #
5 # DESCRIPTION
6 # Base class data type.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Data::Type;
14  
15 4 ▶use Badger::Data::Facets;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 use Badger::Class
17 4 version => 0.01,
18 debug => 0,
19 base => 'Badger::Base',
20 import => 'CLASS',
21 accessors => 'base namespace facets',
22 constants => 'CODE DOT',
23 as_text => 'name',
24 is_true => 1,
25 constant => {
26 type => '',
27 simple => 0,
28 complex => 0,
29 # CLAUSES => 'Badger::Data::Clauses',
30 FACETS => 'Badger::Data::Facets',
31 },
32 alias => {
33 init => \&init_type,
34 4 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 our @PARAMS = qw( base name namespace );
39  
40  
41 sub init_type {
42 3 ▶ my ($self, $config) = @_;
Subroutine init_type: called CC=1 SCAR=0.0
Pod documented
43  
44 # copy in basic parameters
45 3 @$self{ @PARAMS } = @$config{ @PARAMS };
46  
47 # constraint the type with any validation facets defined
48 $self->constrain(
49 $self->class->list_vars( FACETS => $config->{ facets } )
50 3 );
51 3 return $self;
52 }
53  
54  
55 sub name {
56 3 ▶ my $self = shift;
Subroutine name: called CC=3 SCAR=11.3
Pod documented
57 3 ▶ return $self->{ name } ||= do {
Condition66%2/3
$self->{"name"} ||= do { my $pkg = ref $self || $self; my $base = CLASS(); $pkg =~ s/$base:://g; $pkg =~ s/::/./g; $pkg }
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"name"} ||= do { my $pkg = ref $self || $self; my $base = CLASS(); $pkg =~ s/$base:://g; $pkg =~ s/::/./g; $pkg }
$self->{"name"} do { my $pkg = ref $self || $self; my $base = CLASS(); $pkg =~ s/$base:://g; $pkg =~ s/::/./g; $pkg }
Truth table66%2/3
A ||= B
A= $self->{"name"}B= do { my $pkg = ref $self || $self; my $base = CLASS(); $pkg =~ s/$base:://g; $pkg =~ s/::/./g; $pkg }
ABresult
1X1
011
000
58 2 ▶ my $pkg = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
59 2 my $base = CLASS;
60 2 $pkg =~ s/${base}:://g;
61 2 $pkg =~ s/::/./g;
62 2 $pkg;
63 };
64 }
65  
66  
67 sub constrain {
68 3 ▶ my ($self, @args) = @_;
Subroutine constrain: called CC=6 SCAR=18.1
Pod documented
69 3 my $FACETS = $self->FACETS;
70 3 ▶ my $facets = $self->{ facets } ||= [ ];
Condition50%1/2
$self->{"facets"} ||= []
l!l
03
MC/DC0%0/1
$self->{"facets"} ||= []
$self->{"facets"}
Truth table50%1/2
A
A= $self->{"facets"}
Aresult
11
00
71 3 my $type = $self->type;
72 3 my ($name, $value);
73  
74 3 $self->debug("preparing facets: ", $self->dump_data($facets)) if DEBUG;
75  
76 3 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 5 3 8
77 5 $name = shift(@args);
78 5 $self->debug("preparing facet: $name") if DEBUG;
79 5 ▶ push(
Branch: 3 branches
BranchTrueFalseTotal
ref $name eq 'CODE' ? : 0 5 5
$type eq $name ? : 3 2 5
$type ? : 2 0 2
80 @$facets,
81 ref $name eq CODE
82 ? $name
83 : $FACETS->facet(
84 # prepend the basic type (e.g. length => text.length)
85 # unless type and facet are the same (e.g. text => text)
86 ($type eq $name) ? $type : ($type ? $type.DOT.$name : $name),
87 shift(@args)
88 )
89 );
90 }
91  
92 3 $self->debug("constrained type with facets: ", $self->dump_data($facets), "\n")
93 if DEBUG;
94 }
95  
96  
97 sub validate {
98 5 ▶ my ($self, $value) = @_;
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
99  
100 5 foreach my $facet (@{ $self->{ facets } }) {
101 8 $self->debug("validating facet: $facet with value: $value") if DEBUG;
102 8 ▶ ref $facet eq CODE
Branch: 1 branch
BranchTrueFalseTotal
ref $facet eq 'CODE' ? : 0 8 8
103 ? $facet->($value, $self) # TODO: this should be passed as refs...
104 : $facet->validate($value, $self);
105 }
106 2 return $value;
107 }
108  
109  
110 sub _JUST_TESTING_clause {
111 0 ▶ my $self = shift;
Subroutine _JUST_TESTING_clause: not called CC=2 SCAR=17.9
112 0 my $type = shift;
113 0 my $clauses = $self->CLAUSES;
114 $clauses->clause(
115 $type,
116 $self,
117 0 ▶ map { ref $_ ? $_ : $clauses->clause( literal => $_ ) }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
118 @_
119 );
120 }
121  
122  
123 4 1;
124  
125 __END__
« blib/lib/Badger/Data/Facet/Class.pm ↑ Summary lib/Badger/Factory/Class.pm »