| 1 | package Google::Chart::WithData; | ||
| 2 | use Moose::Role; | ||
| 3 | use Google::Chart::Data; | ||
| 4 | use namespace::clean -except => qw(meta); | ||
| 5 | |||
| 6 | # guess what, not all Chart types have datasets associated with it. | ||
| 7 | # so we make this a role | ||
| 8 | |||
| 9 | has data => ( | ||
| 10 | is => 'ro', | ||
| 11 | isa => 'Google::Chart::Data', | ||
| 12 | required => 1, | ||
| 13 | lazy_build => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | has data_class => ( | ||
| 17 | is => 'ro', | ||
| 18 | isa => 'ClassName', | ||
| 19 | required => 1, | ||
| 20 | lazy_build => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | has data_traits => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'ArrayRef', | ||
| 26 | required => 1, | ||
| 27 | lazy_build => 1, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has dataset_class => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Str', | ||
| 33 | required => 1, | ||
| 34 | lazy_build => 1, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has dataset_traits => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'ArrayRef', | ||
| 40 | required => 1, | ||
| 41 | lazy_build => 1, | ||
| 42 | ); | ||
| 43 | |||
| 44 | has encoding_class => ( | ||
| 45 | is => 'ro', | ||
| 46 | isa => 'Str', | ||
| 47 | default => 'Text', | ||
| 48 | ); | ||
| 49 | |||
| 50 | around BUILDARGS => sub { | ||
| 51 | my ($next, $self, @args) = @_; | ||
| 52 | my $args = $next->($self, @args); | ||
| 53 | |||
| 54 | if (my $encoding_class = delete $args->{encoding}) { | ||
| 55 | $args->{encoding_class} = $encoding_class; | ||
| 56 | } | ||
| 57 | return $args; | ||
| 58 | }; | ||
| 59 | |||
| 60 | around prepare_query => sub { | ||
| 61 | my ($next, $self, @args) = @_; | ||
| 62 | my @query = $next->( $self, @args ); | ||
| 63 | |||
| 64 | push @query, $self->data->prepare_query( $self ); | ||
| 65 | return @query; | ||
| 66 | }; | ||
| 67 | |||
| 68 | sub BUILD { | ||
| 69 | my ($self, $params) = @_; | ||
| 70 | |||
| 71 | my %encoding_params; | ||
| 72 | foreach my $key ( grep { /^encoding_(?!class)/ } keys %$params) { | ||
| 73 | my $new_key = $key; | ||
| 74 | $new_key =~ s/^encoding_//; | ||
| 75 | $encoding_params{ $new_key } = $params->{$key}; | ||
| 76 | } | ||
| 77 | $self->data_encoding( $self->encoding_class, %encoding_params ); | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _build_data { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | my $traits = $self->data_traits; | ||
| 85 | my $class = $self->data_class; | ||
| 86 | if (@$traits > 0) { | ||
| 87 | my $meta = Moose::Meta::Class->create_anon_class( | ||
| 88 | superclasses => [ $class ], | ||
| 89 | roles => $traits, | ||
| 90 | cache => 1, | ||
| 91 | ); | ||
| 92 | $class = $meta->name; | ||
| 93 | } | ||
| 94 | |||
| 95 | return $class->new(); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _build_data_class { 'Google::Chart::Data' } | ||
| 99 | sub _build_data_traits { [] } | ||
| 100 | sub _build_dataset_class{ 'Google::Chart::DataSet' } | ||
| 101 | sub _build_dataset_traits{ [] } | ||
| 102 | |||
| 103 | sub add_dataset { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | my $class = $self->dataset_class; | ||
| 107 | if (! Class::MOP::is_class_loaded($class) ) { | ||
| 108 | Class::MOP::load_class($class); | ||
| 109 | } | ||
| 110 | |||
| 111 | my $traits = $self->dataset_traits; | ||
| 112 | if (@$traits > 0) { | ||
| 113 | my $meta = Moose::Meta::Class->create_anon_class( | ||
| 114 | superclasses => [ $class ], | ||
| 115 | roles => $traits, | ||
| 116 | cache => 1, | ||
| 117 | ); | ||
| 118 | $class = $meta->name; | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->data->add_dataset( $class->new(@_) ); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub data_encoding { | ||
| 125 | my $self = shift; | ||
| 126 | my $encoding = shift; | ||
| 127 | if (! blessed $encoding) { | ||
| 128 | if ($encoding !~ s/^\+//) { | ||
| 129 | $encoding = "Google::Chart::Encoding::$encoding"; | ||
| 130 | } | ||
| 131 | if (! Class::MOP::is_class_loaded($encoding)) { | ||
| 132 | Class::MOP::load_class($encoding); | ||
| 133 | } | ||
| 134 | $encoding = $encoding->new(@_); | ||
| 135 | } | ||
| 136 | $self->data->set_encoding($encoding); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub get_datasets { | ||
| 140 | my $self = shift; | ||
| 141 | return $self->data->datasets; | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; | ||
| 145 | |||
| 146 | __END__ |