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