| 1 | |||
| 2 | package Google::Chart::Types; | ||
| 3 | 16 | ▶ | use Moose (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 16 | ▶ | use Moose::Util::TypeConstraints; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 16 | ▶ | use Carp qw(carp confess); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 16 | ▶ | use namespace::clean -except => qw(meta); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | { | ||
| 9 | enum 'Google::Chart::Axis::Position' => qw(x y r t); | ||
| 10 | |||
| 11 | class_type 'Google::Chart::Axis'; | ||
| 12 | coerce 'Google::Chart::Axis' | ||
| 13 | => from 'ArrayRef' | ||
| 14 | => via { | ||
| 15 | if (! Class::MOP::is_class_loaded('Google::Chart::Axis')) { | ||
| 16 | Class::MOP::load_class('Google::Chart::Axis'); | ||
| 17 | } | ||
| 18 | return Google::Chart::Axis->new(axes => $_) | ||
| 19 | } | ||
| 20 | ; | ||
| 21 | |||
| 22 | class_type 'Google::Chart::Axis::Label'; | ||
| 23 | coerce 'Google::Chart::Axis::Label' | ||
| 24 | => from 'ArrayRef' | ||
| 25 | => via { | ||
| 26 | my $arg = $_; | ||
| 27 | if (! Class::MOP::is_class_loaded('Google::Chart::Axis::Label')) { | ||
| 28 | Class::MOP::load_class('Google::Chart::Axis::Label'); | ||
| 29 | } | ||
| 30 | |||
| 31 | # check if all elements are non-ref | ||
| 32 | if (grep { ref $_ } @$arg) { | ||
| 33 | return Google::Chart::Axis::Label->new(labels => $arg); | ||
| 34 | } else { | ||
| 35 | return Google::Chart::Axis::Label->new(labels => [$arg]); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | ; | ||
| 39 | |||
| 40 | subtype 'Google::Chart::Data::Set' | ||
| 41 | => as 'ArrayRef[ArrayRef]' | ||
| 42 | => message { "Data set must be an arrayref of arrayrefs" } | ||
| 43 | ; | ||
| 44 | |||
| 45 | coerce 'Google::Chart::Data::Set', | ||
| 46 | => from 'ArrayRef', | ||
| 47 | => via { | ||
| 48 | return [ $_ ] | ||
| 49 | } | ||
| 50 | ; | ||
| 51 | |||
| 52 | subtype 'Google::Chart::Color::Data' | ||
| 53 | => as 'Str' | ||
| 54 | => where { /^[a-f0-9]{6}$/i } | ||
| 55 | => message { "value '$_' is not a valid hexadecimal value" } | ||
| 56 | ; | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | { # These are the most simplistic coercions | ||
| 61 | class_type 'Google::Chart::Size'; | ||
| 62 | coerce 'Google::Chart::Size' | ||
| 63 | => from 'Str' | ||
| 64 | => via { | ||
| 65 | if (! /^(\d+)x(\d+)$/) { | ||
| 66 | confess("Could not parse $_ as size"); | ||
| 67 | } | ||
| 68 | |||
| 69 | return Google::Chart::Size->new(width => $1, height => $2); | ||
| 70 | } | ||
| 71 | ; | ||
| 72 | |||
| 73 | coerce 'Google::Chart::Size' | ||
| 74 | => from 'HashRef' | ||
| 75 | => via { | ||
| 76 | my $h = $_; | ||
| 77 | |||
| 78 | my ($width, $height) = ($h->{args}) ? | ||
| 79 | ($h->{args}->{width}, $h->{args}->{height}) : | ||
| 80 | ($h->{width}, $h->{height}) | ||
| 81 | ; | ||
| 82 | |||
| 83 | return Google::Chart::Size->new( width => $width, height => $height ); | ||
| 84 | } | ||
| 85 | ; | ||
| 86 | |||
| 87 | class_type 'Google::Chart::Data'; | ||
| 88 | coerce 'Google::Chart::Data' | ||
| 89 | => from 'ArrayRef' | ||
| 90 | => via { | ||
| 91 | return Google::Chart::Data->new(data => $_); | ||
| 92 | } | ||
| 93 | ; | ||
| 94 | |||
| 95 | class_type 'Google::Chart::Size'; | ||
| 96 | coerce 'Google::Chart::Size' | ||
| 97 | => from 'Str' | ||
| 98 | => via { | ||
| 99 | my %args; | ||
| 100 | @args{ qw(width height) } = split /x/, $_; | ||
| 101 | return Google::Chart::Size->new(%args); | ||
| 102 | } | ||
| 103 | ; | ||
| 104 | |||
| 105 | class_type 'Google::Chart::Title'; | ||
| 106 | coerce 'Google::Chart::Title' | ||
| 107 | => from 'Str' | ||
| 108 | => via { | ||
| 109 | return Google::Chart::Title->new(text => $_); | ||
| 110 | } | ||
| 111 | ; | ||
| 112 | |||
| 113 | role_type 'Google::Chart::Type'; | ||
| 114 | coerce 'Google::Chart::Type' | ||
| 115 | => from 'Str' | ||
| 116 | => via { | ||
| 117 | my $class = $_; | ||
| 118 | if ($class !~ s/^\+//) { | ||
| 119 | $class = "Google::Chart::Type::$class"; | ||
| 120 | } | ||
| 121 | |||
| 122 | if (! Class::MOP::is_class_loaded($class)) { | ||
| 123 | Class::MOP::load_class($class); | ||
| 124 | } | ||
| 125 | return $class->new(); | ||
| 126 | } | ||
| 127 | ; | ||
| 128 | coerce 'Google::Chart::Type' | ||
| 129 | => from 'HashRef' | ||
| 130 | => via { | ||
| 131 | my $h = $_; | ||
| 132 | |||
| 133 | carp("This form of coercion (from Hash to Object) is now deprecated."); | ||
| 134 | my $module = $h->{module} || | ||
| 135 | confess("No module name provided for coercion"); | ||
| 136 | if ($module !~ s/^\+//) { | ||
| 137 | $module = "Google::Chart::Type::$module"; | ||
| 138 | } | ||
| 139 | Class::MOP::load_class( $module ); | ||
| 140 | return $module->new(%{ $h->{args} }); | ||
| 141 | } | ||
| 142 | ; | ||
| 143 | |||
| 144 | coerce 'Google::Chart::Type' | ||
| 145 | => from 'ArrayRef' | ||
| 146 | => via { | ||
| 147 | my ($class, %args) = @$_; | ||
| 148 | if ($class !~ s/^\+//) { | ||
| 149 | $class = "Google::Chart::Type::$class"; | ||
| 150 | } | ||
| 151 | |||
| 152 | if (! Class::MOP::is_class_loaded($class)) { | ||
| 153 | Class::MOP::load_class($class); | ||
| 154 | } | ||
| 155 | return $class->new(%args); | ||
| 156 | } | ||
| 157 | ; | ||
| 158 | |||
| 159 | role_type 'Google::Chart::Encoding'; | ||
| 160 | coerce 'Google::Chart::Encoding' | ||
| 161 | => from 'Str' | ||
| 162 | => via { | ||
| 163 | my $class = $_; | ||
| 164 | if ($class !~ s/^\+//) { | ||
| 165 | $class = "Google::Chart::Encoding::$class"; | ||
| 166 | } | ||
| 167 | |||
| 168 | if (! Class::MOP::is_class_loaded($class)) { | ||
| 169 | Class::MOP::load_class($class); | ||
| 170 | } | ||
| 171 | return $class->new(); | ||
| 172 | } | ||
| 173 | ; | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; | ||
| 177 | |||
| 178 | __END__ | ||