root/lang/perl/Google-Chart/branches/moose/lib/Google/Chart/Types.pm @ 16111

Revision 16111, 1.8 kB (checked in by daisuke, 5 years ago)

coerce sizes

  • Property svn:keywords set to Id
Line 
1# $Id$
2
3package Google::Chart::Types;
4use Moose;
5use Moose::Util::TypeConstraints;
6use Sub::Exporter -setup => {
7    exports => [ qw(hash_coersion) ]
8};
9
10sub hash_coersion {
11    my (%args) = @_;
12
13    my $default = $args{default};
14    my $prefix  = $args{prefix};
15
16    return sub {
17        my $h = $_;
18        my $module = $h->{module} || $default ||
19            confess "No module name provided for coersion";
20        if ($module !~ s/^\+//) {
21            $module = join('::', $prefix, $module);
22        }
23        Class::MOP::load_class( $module );
24        return $module->new(%{ $h->{args} });
25    }
26}
27
28{
29    subtype 'Google::Chart::Color'
30        => as 'Str'
31        => where { /^[a-f0-9]{6}/i }
32    ;
33}
34
35{
36    role_type 'Google::Chart::Type';
37    coerce 'Google::Chart::Type'
38        => from 'Str'
39        => via {
40            my $class = sprintf( 'Google::Chart::Type::%s', ucfirst $_ );
41            Class::MOP::load_class($class);
42
43            return $class->new();
44        }
45    ;
46    coerce 'Google::Chart::Type'
47        => from 'HashRef'
48        => hash_coersion(prefix => "Google::Chart::Type")
49    ;
50}
51
52{
53    role_type 'Google::Chart::Data';
54    coerce 'Google::Chart::Data'
55        => from 'ArrayRef'
56        => via {
57            my $class = 'Google::Chart::Data::Text';
58            Class::MOP::load_class($class);
59            $class->new(datasets => $_);
60        }
61    ;
62    coerce 'Google::Chart::Data'
63        => from 'HashRef'
64        => via {
65            my $class = $_->{module};
66            if ($class !~ s/^\+//) {
67                $class = "Google::Chart::Data::$class";
68            }
69            Class::MOP::load_class($class);
70
71            $class->new(datasets => $_->{args});
72        }
73    ;
74}
75
76no Moose;
77
781;
79
80__END__
81
82=head1 NAME
83
84Google::Chart::Types - Google::Chart Miscellaneous Types
85
86=cut
Note: See TracBrowser for help on using the browser.