| 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 |  | 1579 | use Badger::Data::Facets; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 33 |  | 
| 16 |  |  |  |  |  |  | use Badger::Class | 
| 17 | 4 |  |  |  |  | 38 | 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 |  | 26 | }; | 
|  | 4 |  |  |  |  | 8 |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 4 |  |  | 4 |  | 21 | use Badger::Debug ':dump'; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 16 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | our @PARAMS = qw( base name namespace ); | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub init_type { | 
| 42 | 3 |  |  | 3 | 1 | 6 | my ($self, $config) = @_; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # copy in basic parameters | 
| 45 | 3 |  |  |  |  | 100 | @$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 |  |  |  |  | 13 | ); | 
| 51 | 3 |  |  |  |  | 5 | return $self; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub name { | 
| 56 | 3 |  |  | 3 | 1 | 5 | my $self = shift; | 
| 57 | 3 |  | 66 |  |  | 15 | return $self->{ name } ||= do { | 
| 58 | 2 |  | 33 |  |  | 6 | my $pkg  = ref $self || $self; | 
| 59 | 2 |  |  |  |  | 6 | my $base = CLASS; | 
| 60 | 2 |  |  |  |  | 24 | $pkg =~ s/${base}:://g; | 
| 61 | 2 |  |  |  |  | 5 | $pkg =~ s/::/./g; | 
| 62 | 2 |  |  |  |  | 18 | $pkg; | 
| 63 |  |  |  |  |  |  | }; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub constrain { | 
| 68 | 3 |  |  | 3 | 1 | 8 | my ($self, @args) = @_; | 
| 69 | 3 |  |  |  |  | 8 | my $FACETS = $self->FACETS; | 
| 70 | 3 |  | 50 |  |  | 13 | my $facets = $self->{ facets } ||= [ ]; | 
| 71 | 3 |  |  |  |  | 8 | my $type   = $self->type; | 
| 72 | 3 |  |  |  |  | 5 | my ($name, $value); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 3 |  |  |  |  | 3 | $self->debug("preparing facets: ", $self->dump_data($facets)) if DEBUG; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 3 |  |  |  |  | 7 | while (@args) { | 
| 77 | 5 |  |  |  |  | 8 | $name = shift(@args); | 
| 78 | 5 |  |  |  |  | 5 | $self->debug("preparing facet: $name") if DEBUG; | 
| 79 | 5 | 50 |  |  |  | 42 | 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 |  |  |  |  | 6 | foreach my $facet (@{ $self->{ facets } }) { | 
|  | 5 |  |  |  |  | 10 |  | 
| 101 | 8 |  |  |  |  | 9 | $self->debug("validating facet: $facet with value: $value") if DEBUG; | 
| 102 | 8 | 50 |  |  |  | 22 | ref $facet eq CODE | 
| 103 |  |  |  |  |  |  | ? $facet->($value, $self)               # TODO: this should be passed as refs... | 
| 104 |  |  |  |  |  |  | : $facet->validate($value, $self); | 
| 105 |  |  |  |  |  |  | } | 
| 106 | 2 |  |  |  |  | 8 | 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__ |