line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#======================================================================== |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Badger::Data::Type |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# Base class data type. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# AUTHOR |
9
|
|
|
|
|
|
|
# Andy Wardley |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
#======================================================================== |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Badger::Data::Type; |
14
|
|
|
|
|
|
|
|
15
|
4
|
|
|
4
|
|
1537
|
use Badger::Data::Facets; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
32
|
|
16
|
|
|
|
|
|
|
use Badger::Class |
17
|
4
|
|
|
|
|
35
|
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
|
|
|
4
|
|
25
|
}; |
|
4
|
|
|
|
|
6
|
|
35
|
|
|
|
|
|
|
|
36
|
4
|
|
|
4
|
|
23
|
use Badger::Debug ':dump'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
15
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
our @PARAMS = qw( base name namespace ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub init_type { |
42
|
3
|
|
|
3
|
1
|
8
|
my ($self, $config) = @_; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# copy in basic parameters |
45
|
3
|
|
|
|
|
106
|
@$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
|
|
|
|
|
16
|
); |
51
|
3
|
|
|
|
|
7
|
return $self; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub name { |
56
|
3
|
|
|
3
|
1
|
5
|
my $self = shift; |
57
|
3
|
|
66
|
|
|
18
|
return $self->{ name } ||= do { |
58
|
2
|
|
33
|
|
|
7
|
my $pkg = ref $self || $self; |
59
|
2
|
|
|
|
|
6
|
my $base = CLASS; |
60
|
2
|
|
|
|
|
29
|
$pkg =~ s/${base}:://g; |
61
|
2
|
|
|
|
|
5
|
$pkg =~ s/::/./g; |
62
|
2
|
|
|
|
|
21
|
$pkg; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub constrain { |
68
|
3
|
|
|
3
|
1
|
8
|
my ($self, @args) = @_; |
69
|
3
|
|
|
|
|
10
|
my $FACETS = $self->FACETS; |
70
|
3
|
|
50
|
|
|
14
|
my $facets = $self->{ facets } ||= [ ]; |
71
|
3
|
|
|
|
|
10
|
my $type = $self->type; |
72
|
3
|
|
|
|
|
5
|
my ($name, $value); |
73
|
|
|
|
|
|
|
|
74
|
3
|
|
|
|
|
4
|
$self->debug("preparing facets: ", $self->dump_data($facets)) if DEBUG; |
75
|
|
|
|
|
|
|
|
76
|
3
|
|
|
|
|
8
|
while (@args) { |
77
|
5
|
|
|
|
|
7
|
$name = shift(@args); |
78
|
5
|
|
|
|
|
7
|
$self->debug("preparing facet: $name") if DEBUG; |
79
|
5
|
50
|
|
|
|
49
|
push( |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
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
|
|
|
|
|
6
|
$self->debug("constrained type with facets: ", $self->dump_data($facets), "\n") |
93
|
|
|
|
|
|
|
if DEBUG; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub validate { |
98
|
5
|
|
|
5
|
1
|
28
|
my ($self, $value) = @_; |
99
|
|
|
|
|
|
|
|
100
|
5
|
|
|
|
|
7
|
foreach my $facet (@{ $self->{ facets } }) { |
|
5
|
|
|
|
|
12
|
|
101
|
8
|
|
|
|
|
10
|
$self->debug("validating facet: $facet with value: $value") if DEBUG; |
102
|
8
|
50
|
|
|
|
25
|
ref $facet eq CODE |
103
|
|
|
|
|
|
|
? $facet->($value, $self) # TODO: this should be passed as refs... |
104
|
|
|
|
|
|
|
: $facet->validate($value, $self); |
105
|
|
|
|
|
|
|
} |
106
|
2
|
|
|
|
|
10
|
return $value; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _JUST_TESTING_clause { |
111
|
0
|
|
|
0
|
|
|
my $self = shift; |
112
|
0
|
|
|
|
|
|
my $type = shift; |
113
|
0
|
|
|
|
|
|
my $clauses = $self->CLAUSES; |
114
|
|
|
|
|
|
|
$clauses->clause( |
115
|
|
|
|
|
|
|
$type, |
116
|
|
|
|
|
|
|
$self, |
117
|
0
|
0
|
|
|
|
|
map { ref $_ ? $_ : $clauses->clause( literal => $_ ) } |
|
0
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
@_ |
119
|
|
|
|
|
|
|
); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |