| 1 |
#----------------------------------------------------------------------- |
| 2 |
# any - text must match one of a pre-defined set of values |
| 3 |
# TODO: change name to 'any'. Should we accept facets and make a |
| 4 |
# literal match a facet too? |
| 5 |
#----------------------------------------------------------------------- |
| 6 |
|
| 7 |
package Badger::Data::Facet::Any; |
| 8 |
|
| 9 |
use Badger::Class |
| 10 |
base => 'Badger::Data::Facet', |
| 11 |
constants => 'ARRAY'; |
| 12 |
|
| 13 |
|
| 14 |
sub init { |
| 15 |
my ($self, $config) = @_; |
| 16 |
|
| 17 |
$self->SUPER::init($config); |
| 18 |
|
| 19 |
# TODO: should we updgrade these to facets? (prolly) |
| 20 |
|
| 21 |
# ensure value is folded to a list reference |
| 22 |
my $value = $self->{ value }; |
| 23 |
$self->{ values } = |
| 24 |
ref $value eq ARRAY |
| 25 |
? $value |
| 26 |
: [$value]; |
| 27 |
|
| 28 |
return $self |
| 29 |
} |
| 30 |
|
| 31 |
sub validate { |
| 32 |
my ($self, $value, $type) = @_; |
| 33 |
|
| 34 |
# TODO: should we worry about numerical comparisons? In the original |
| 35 |
# context of XML::Schema there was no need because all data originates |
| 36 |
# from text documents and a text comparison is what defined equality. |
| 37 |
# (e.g. 1.0 is not the same as 1) |
| 38 |
foreach my $expect (@{ $self->{ values } }) { |
| 39 |
return $value |
| 40 |
if $value eq $expect; |
| 41 |
} |
| 42 |
|
| 43 |
return $self->invalid_msg( not_any => $type || 'Text', $self->{ values }, $value ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#----------------------------------------------------------------------- |
| 49 |
# whitespace |
| 50 |
#----------------------------------------------------------------------- |
| 51 |
|