File: Facets.pm

package info (click to toggle)
libbadger-perl 0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,400 kB
  • sloc: perl: 11,004; makefile: 9
file content (112 lines) | stat: -rw-r--r-- 2,505 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#========================================================================
#
# Badger::Data::Facets
#
# DESCRIPTION
#   Factory for Badger::Data::Facets validation objects.
#
# AUTHOR
#   Andy Wardley   <abw@wardley.org>
#
#========================================================================

package Badger::Data::Facets;

use Badger::Factory::Class
    version   => 0.01,
    debug     => 0,
    item      => 'facet',
    path      => 'Badger::Data::Facet BadgerX::Data::Facet',
    constants => 'HASH DOT';


our $PREFIXES = {
    text    => 'text',
    number  => 'number',
    list    => 'list',
};


sub type_args {
    my $self = shift;
    my $type = shift;       # my $save = $type;   # tmp debug
    my $name = $type;
    my $base;

    # See if we recognise the initial XXX_ prefix as a short-cut.  
    # If so then the part afterwords is the name we're interested in, 
    # e.g. text_max_length becomes 'text.max_length' which the factory
    # module will map to '<path>::Text::MaxLength' for loading, while 
    # 'max_length' is the name that we'll use as a default argument name.
    
    if ($type =~ s/^([^\W_]+)(\.|_)(.*)/$3/) {
        if ($2 eq DOT) {
            $type = $1.DOT.$type;
            $name = $3;
        }
        elsif ($base = $PREFIXES->{ $1 }) {
            $type = $base.DOT.$type;
            $name = $3;
        }
        else {
            $type = $1.$2.$type;
        }
    }

    my $args = @_ && ref $_[0] eq HASH ? shift : { $name => shift };

#    $self->debug("save:$save / type:$type / base:$base / name:$name");

    return ($type, $args);
}


1;

__END__

=head1 NAME

Badger::Data::Facets - factory module for data validation facets.

=head1 DESCRIPTION

This module implements a subclass of L<Badger::Factory> for loading and
instantiating data validation facets.

=head1 METHODS

The following methods are defined in addition to those inherited from the 
L<Badger::Factory> and L<Badger::Base> base classes.

=head2 type_args($type, @args)

This performs some custom handling of the arguments used to create data
facets. 

=head1 AUTHOR

Andy Wardley L<http://wardley.org/>

=head1 COPYRIGHT

Copyright (C) 2008-2012 Andy Wardley.  All Rights Reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 SEE ALSO

L<Badger::Factory>,
L<Badger::Base>.

=cut

# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: