| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Data::Facet | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Base class validation facet for simple data types. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | #======================================================================== | ||
| 12 | |||
| 13 | package Badger::Data::Facet; | ||
| 14 | |||
| 15 | use Badger::Debug ':dump'; | ||
| 16 | use Badger::Class | ||
| 17 | version => 0.01, | ||
| 18 | debug => 0, | ||
| 19 | base => 'Badger::Base', | ||
| 20 | import => 'class', | ||
| 21 | words => 'ARGS OPTS', | ||
| 22 | accessors => 'value', | ||
| 23 | messages => { | ||
| 24 | # invalid => 'Invalid %s. %s', | ||
| 25 | # list_length => '%s should be %d elements long (got %d)', | ||
| 26 | # list_too_short => '%s should be at least %d elements long (got %d)', | ||
| 27 | # list_too_long => '%s should be at most %d elements long (got %d)', | ||
| 28 | # text_length => '%s should be %d characters long (got %d)', | ||
| 29 | # text_too_short => '%s should be at least %d characters long (got %d)', | ||
| 30 | # text_too_long => '%s should be at most %d characters long (got %d)', | ||
| 31 | # too_small => '%s should be no less than %d (got %d)', | ||
| 32 | # too_large => '%s should be no more than %d (got %d)', | ||
| 33 | # pattern => '%s does not match pattern: %s', | ||
| 34 | not_any => '%s does not match any of the permitted values: <3>', | ||
| 35 | # whitespace => 'Invalid whitespace option: %s (expected one of: %s)', | ||
| 36 | # not_number => '%s is not a number: <3>', | ||
| 37 | }; | ||
| 38 | |||
| 39 | |||
| 40 | sub init { | ||
| 41 | my ($self, $config) = @_; | ||
| 42 | my $class = $self->class; | ||
| 43 | my ($option, @optional); | ||
| 44 | |||
| 45 | $self->debug("init() config is ", $self->dump_data($config)) if DEBUG; | ||
| 46 | |||
| 47 | foreach $option ($class->list_vars(ARGS)) { | ||
| 48 | $self->{ $option } = defined $config->{ $option } | ||
| 49 | ? $config->{ $option } | ||
| 50 | : $self->error_msg( missing => $option ); | ||
| 51 | } | ||
| 52 | |||
| 53 | @optional = $class->list_vars(OPTS); | ||
| 54 | @$self{ @optional } = @$config{ @optional }; | ||
| 55 | |||
| 56 | $self->{ name } ||= do { | ||
| 57 | my $pkg = ref $self; | ||
| 58 | $pkg =~ /.*::(\w+)$/; | ||
| 59 | $1; | ||
| 60 | }; | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub validate { | ||
| 67 | shift->not_implemented; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | sub invalid { | ||
| 72 | shift->error(@_); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | sub invalid_msg { | ||
| 77 | my $self = shift; | ||
| 78 | $self->invalid( $self->message( @_ ) ); | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | 1; | ||
| 83 | |||
| 84 | __END__ |