File Coverage

blib/lib/Gnuplot/Builder/Dataset.pm
Criterion Covered Total %
statement 114 122 93.4
branch 16 18 88.8
condition 1 3 33.3
subroutine 32 33 96.9
pod 19 22 86.3
total 182 198 91.9


line stmt bran cond sub pod time code
1             package Gnuplot::Builder::Dataset;
2 27     27   2374261 use strict;
  27         85  
  27         1115  
3 27     27   141 use warnings;
  27         66  
  27         1615  
4 27     27   9920 use Gnuplot::Builder::PrototypedData;
  27         105  
  27         1145  
5 27     27   172 use Scalar::Util qw(weaken blessed);
  27         70  
  27         1820  
6 27     27   174 use Carp;
  27         53  
  27         1702  
7 27     27   12515 use overload '""' => 'to_string';
  27         36091  
  27         189  
8              
9             sub new {
10 124     124 1 3274900 my ($class, $source, @set_option_args) = @_;
11 124         524 my $self = bless {
12             pdata => undef,
13             pdata_join => undef, ## deprecated. should be removed in the future.
14             parent => undef,
15             }, $class;
16 124         421 $self->_init_pdata();
17 124 100       344 if(defined $source) {
18 33         119 $self->set_source($source);
19             }
20 124 100       343 if(@set_option_args) {
21 7         23 $self->set_option(@set_option_args);
22             }
23 124         394 return $self;
24             }
25              
26             sub new_file {
27 18     18 1 560919 my ($class, $filename, @set_option_args) = @_;
28 18         71 return $class->new->set_file($filename)->set_option(@set_option_args);
29             }
30              
31             sub new_data {
32 10     10 1 3444 my ($class, $data_provider, @set_option_args) = @_;
33 10         38 return $class->new->set_file('-')->set_data($data_provider)->set_option(@set_option_args);
34             }
35              
36             sub _init_pdata {
37 124     124   225 my ($self) = @_;
38 124         247 weaken $self;
39             $self->{pdata} = Gnuplot::Builder::PrototypedData->new(
40             entry_evaluator => sub {
41 49     49   94 my ($key, $coderef) = @_;
42 49         113 return $coderef->($self, $key);
43             },
44             attribute_evaluator => { source => sub {
45 11     11   52 my ($key, $coderef) = @_;
46 11         28 return $coderef->($self);
47             } },
48 124         1210 );
49 124         406 $self->{pdata_join} = Gnuplot::Builder::PrototypedData->new();
50             }
51              
52             sub to_string {
53 119     119 1 4482 my ($self) = @_;
54 119         213 my @words = ();
55 119         319 push @words, $self->get_source;
56             $self->{pdata}->each_resolved_entry(sub {
57 198     198   1613 my ($name, $values_arrayref) = @_;
58 198         369 my @values = grep { defined($_) } @$values_arrayref;
  259         663  
59 198 100       466 return if !@values;
60 170         526 my $join = $self->{pdata_join}->get_resolved_attribute($name);
61 170 100       390 if(defined $join) {
62 22         183 push @words, $name, join($join, @values);
63             }else {
64 148         801 push @words, $name, @values;
65             }
66 119         846 });
67 119 100       941 return join " ", grep { defined($_) && "$_" ne "" } @words;
  492         3163  
68             }
69              
70             *params_string = *to_string;
71              
72             sub set_source {
73 42     42 1 152 my ($self, $source) = @_;
74             $self->{pdata}->set_attribute(
75 42         162 key => "source", value => $source
76             );
77 42         77 return $self;
78             }
79              
80             sub setq_source {
81 36     36 1 133 my ($self, $source) = @_;
82             $self->{pdata}->set_attribute(
83 36         207 key => "source", value => $source, quote => 1,
84             );
85 36         164 return $self;
86             }
87              
88             *set_file = *setq_source;
89              
90             sub get_source {
91 138     138 1 1562 my ($self) = @_;
92 138         487 return $self->{pdata}->get_resolved_attribute("source");
93             }
94              
95             sub delete_source {
96 5     5 1 15 my ($self) = @_;
97 5         26 $self->{pdata}->delete_attribute("source");
98 5         25 return $self;
99             }
100              
101             sub set_option {
102 93     93 1 402 my ($self, @options) = @_;
103             $self->{pdata}->set_entry(
104 93         439 entries => \@options,
105             quote => 0,
106             );
107 93         699 return $self;
108             }
109              
110             *set = *set_option;
111              
112             sub setq_option {
113 25     25 1 149 my ($self, @options) = @_;
114             $self->{pdata}->set_entry(
115 25         138 entries => \@options,
116             quote => 1,
117             );
118 25         209 return $self;
119             }
120              
121             *setq = *setq_option;
122              
123             sub unset {
124 2     2 1 10 my ($self, @opt_names) = @_;
125 2         3 return $self->set_option(map {$_ => undef} @opt_names);
  3         8  
126             }
127              
128             sub get_option {
129 92     92 1 6487 my ($self, $name) = @_;
130 92         315 return $self->{pdata}->get_resolved_entry($name);
131             }
132              
133             sub delete_option {
134 2     2 1 9 my ($self, @names) = @_;
135 2         6 foreach my $name (@names) {
136 2         12 $self->{pdata}->delete_entry($name);
137             }
138 2         14 return $self;
139             }
140              
141             sub set_parent {
142 13     13 1 2718 my ($self, $parent) = @_;
143 13 50       45 if(!defined($parent)) {
144 0         0 $self->{parent} = undef;
145 0         0 $self->{pdata}->set_parent(undef);
146 0         0 $self->{pdata_join}->set_parent(undef);
147 0         0 return $self;
148             }
149 13 50 33     1374 if(!blessed($parent) || !$parent->isa("Gnuplot::Builder::Dataset")) {
150 0         0 croak "parent must be a Gnuplot::Builder::Dataset";
151             }
152 13         38 $self->{parent} = $parent;
153 13         112 $self->{pdata}->set_parent($parent->{pdata});
154 13         59 $self->{pdata_join}->set_parent($parent->{pdata_join});
155 13         52 return $self;
156             }
157              
158 5     5 1 41 sub get_parent { return $_[0]->{parent} }
159              
160             *parent = *get_parent;
161              
162             sub new_child {
163 11     11 1 56 my ($self) = @_;
164 11         39 return Gnuplot::Builder::Dataset->new->set_parent($self);
165             }
166              
167             sub set_data {
168 24     24 1 3683 my ($self, $data_provider) = @_;
169             $self->{pdata}->set_attribute(
170 24         96 key => "data", value => $data_provider
171             );
172 24         77 return $self;
173             }
174              
175             sub write_data_to {
176 41     41 1 2568 my ($self, $writer) = @_;
177 41         171 my $data_provider = $self->{pdata}->get_resolved_attribute("data");
178 41 100       158 return $self if not defined $data_provider;
179 24 100       76 if(ref($data_provider) eq "CODE") {
180 10         33 $data_provider->($self, $writer);
181             }else {
182 14         46 $writer->($data_provider);
183             }
184 24         1197 return $self;
185             }
186              
187             sub delete_data {
188 6     6 1 6049 my ($self) = @_;
189 6         25 $self->{pdata}->delete_attribute("data");
190 6         26 return $self;
191             }
192              
193             sub _deprecated {
194 26     26   42 my ($msg) = @_;
195 26         127 my $method = (caller(1))[3];
196 26         3911 carp "$method is deprecated. $msg";
197             }
198              
199             sub set_join {
200 24     24 0 125 my ($self, %joins) = @_;
201 24         58 _deprecated("Use Gnuplot::Builder::JoinDict.");
202 24         195 foreach my $name (keys %joins) {
203             $self->{pdata_join}->set_attribute(
204 28         101 key => $name, value => $joins{$name}, quote => 0
205             );
206             }
207 24         83 return $self;
208             }
209              
210             sub delete_join {
211 2     2 0 9 my ($self, @names) = @_;
212 2         7 _deprecated("Use Gnuplot::Builder::JoinDict.");
213 2         20 foreach my $name (@names) {
214 2         13 $self->{pdata_join}->delete_attribute($name);
215             }
216 2         12 return $self;
217             }
218              
219             sub Lens {
220 0     0 0   my ($self, $key) = @_;
221 0           require Gnuplot::Builder::Lens;
222 0           return Gnuplot::Builder::Lens->new(
223             "get_option", "set_option", $key
224             );
225             }
226              
227             1;
228              
229             __END__