| 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
}
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
}
| |||||||||||||||||||||
| 58 | 2 | ▶ | my $pkg = ref $self || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 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"} ||= []
MC/DC0%0/1
$self->{"facets"} ||= []
$self->{"facets"}
Truth table50%1/2
A
A= $self->{"facets"}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 77 | 5 | $name = shift(@args); | |||||||||||||||||||
| 78 | 5 | $self->debug("preparing facet: $name") if DEBUG; | |||||||||||||||||||
| 79 | 5 | ▶ | push( | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 118 | @_ | ||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 4 | 1; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||