| 1 | # $Id$ |
|---|
| 2 | |
|---|
| 3 | package Google::Chart::Types; |
|---|
| 4 | use Moose; |
|---|
| 5 | use Moose::Util::TypeConstraints; |
|---|
| 6 | use Sub::Exporter -setup => { |
|---|
| 7 | exports => [ qw(hash_coersion) ] |
|---|
| 8 | }; |
|---|
| 9 | |
|---|
| 10 | sub 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 | |
|---|
| 76 | no Moose; |
|---|
| 77 | |
|---|
| 78 | 1; |
|---|
| 79 | |
|---|
| 80 | __END__ |
|---|
| 81 | |
|---|
| 82 | =head1 NAME |
|---|
| 83 | |
|---|
| 84 | Google::Chart::Types - Google::Chart Miscellaneous Types |
|---|
| 85 | |
|---|
| 86 | =cut |
|---|