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   1658442 use strict;
  27         44  
  27         815  
3 27     27   99 use warnings;
  27         46  
  27         1148  
4 27     27   7393 use Gnuplot::Builder::PrototypedData;
  27         107  
  27         851  
5 27     27   136 use Scalar::Util qw(weaken blessed);
  27         48  
  27         1371  
6 27     27   139 use Carp;
  27         40  
  27         1232  
7 27     27   10179 use overload '""' => 'to_string';
  27         27475  
  27         162  
8              
9             sub new {
10 124     124 1 2133122 my ($class, $source, @set_option_args) = @_;
11 124         412 my $self = bless {
12             pdata => undef,
13             pdata_join => undef, ## deprecated. should be removed in the future.
14             parent => undef,
15             }, $class;
16 124         381 $self->_init_pdata();
17 124 100       245 if(defined $source) {
18 33         74 $self->set_source($source);
19             }
20 124 100       225 if(@set_option_args) {
21 7         16 $self->set_option(@set_option_args);
22             }
23 124         274 return $self;
24             }
25              
26             sub new_file {
27 18     18 1 401086 my ($class, $filename, @set_option_args) = @_;
28 18         56 return $class->new->set_file($filename)->set_option(@set_option_args);
29             }
30              
31             sub new_data {
32 10     10 1 2156 my ($class, $data_provider, @set_option_args) = @_;
33 10         27 return $class->new->set_file('-')->set_data($data_provider)->set_option(@set_option_args);
34             }
35              
36             sub _init_pdata {
37 124     124   179 my ($self) = @_;
38 124         188 weaken $self;
39             $self->{pdata} = Gnuplot::Builder::PrototypedData->new(
40             entry_evaluator => sub {
41 49     49   82 my ($key, $coderef) = @_;
42 49         104 return $coderef->($self, $key);
43             },
44             attribute_evaluator => { source => sub {
45 11     11   39 my ($key, $coderef) = @_;
46 11         19 return $coderef->($self);
47             } },
48 124         946 );
49 124         259 $self->{pdata_join} = Gnuplot::Builder::PrototypedData->new();
50             }
51              
52             sub to_string {
53 119     119 1 4269 my ($self) = @_;
54 119         159 my @words = ();
55 119         232 push @words, $self->get_source;
56             $self->{pdata}->each_resolved_entry(sub {
57 198     198   1957 my ($name, $values_arrayref) = @_;
58 198         277 my @values = grep { defined($_) } @$values_arrayref;
  259         554  
59 198 100       387 return if !@values;
60 170         326 my $join = $self->{pdata_join}->get_resolved_attribute($name);
61 170 100       281 if(defined $join) {
62 22         115 push @words, $name, join($join, @values);
63             }else {
64 148         597 push @words, $name, @values;
65             }
66 119         599 });
67 119 100       736 return join " ", grep { defined($_) && "$_" ne "" } @words;
  492         1560  
68             }
69              
70             *params_string = *to_string;
71              
72             sub set_source {
73 42     42 1 88 my ($self, $source) = @_;
74             $self->{pdata}->set_attribute(
75 42         121 key => "source", value => $source
76             );
77 42         84 return $self;
78             }
79              
80             sub setq_source {
81 36     36 1 104 my ($self, $source) = @_;
82             $self->{pdata}->set_attribute(
83 36         121 key => "source", value => $source, quote => 1,
84             );
85 36         100 return $self;
86             }
87              
88             *set_file = *setq_source;
89              
90             sub get_source {
91 138     138 1 1342 my ($self) = @_;
92 138         363 return $self->{pdata}->get_resolved_attribute("source");
93             }
94              
95             sub delete_source {
96 5     5 1 9 my ($self) = @_;
97 5         17 $self->{pdata}->delete_attribute("source");
98 5         18 return $self;
99             }
100              
101             sub set_option {
102 93     93 1 290 my ($self, @options) = @_;
103             $self->{pdata}->set_entry(
104 93         302 entries => \@options,
105             quote => 0,
106             );
107 93         502 return $self;
108             }
109              
110             *set = *set_option;
111              
112             sub setq_option {
113 25     25 1 120 my ($self, @options) = @_;
114             $self->{pdata}->set_entry(
115 25         86 entries => \@options,
116             quote => 1,
117             );
118 25         140 return $self;
119             }
120              
121             *setq = *setq_option;
122              
123             sub unset {
124 2     2 1 10 my ($self, @opt_names) = @_;
125 2         4 return $self->set_option(map {$_ => undef} @opt_names);
  3         12  
126             }
127              
128             sub get_option {
129 92     92 1 5827 my ($self, $name) = @_;
130 92         255 return $self->{pdata}->get_resolved_entry($name);
131             }
132              
133             sub delete_option {
134 2     2 1 6 my ($self, @names) = @_;
135 2         4 foreach my $name (@names) {
136 2         7 $self->{pdata}->delete_entry($name);
137             }
138 2         9 return $self;
139             }
140              
141             sub set_parent {
142 13     13 1 1630 my ($self, $parent) = @_;
143 13 50       37 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     120 if(!blessed($parent) || !$parent->isa("Gnuplot::Builder::Dataset")) {
150 0         0 croak "parent must be a Gnuplot::Builder::Dataset";
151             }
152 13         25 $self->{parent} = $parent;
153 13         72 $self->{pdata}->set_parent($parent->{pdata});
154 13         41 $self->{pdata_join}->set_parent($parent->{pdata_join});
155 13         27 return $self;
156             }
157              
158 5     5 1 33 sub get_parent { return $_[0]->{parent} }
159              
160             *parent = *get_parent;
161              
162             sub new_child {
163 11     11 1 41 my ($self) = @_;
164 11         27 return Gnuplot::Builder::Dataset->new->set_parent($self);
165             }
166              
167             sub set_data {
168 24     24 1 2603 my ($self, $data_provider) = @_;
169             $self->{pdata}->set_attribute(
170 24         81 key => "data", value => $data_provider
171             );
172 24         55 return $self;
173             }
174              
175             sub write_data_to {
176 41     41 1 1715 my ($self, $writer) = @_;
177 41         101 my $data_provider = $self->{pdata}->get_resolved_attribute("data");
178 41 100       85 return $self if not defined $data_provider;
179 24 100       54 if(ref($data_provider) eq "CODE") {
180 10         26 $data_provider->($self, $writer);
181             }else {
182 14         29 $writer->($data_provider);
183             }
184 24         710 return $self;
185             }
186              
187             sub delete_data {
188 6     6 1 4595 my ($self) = @_;
189 6         22 $self->{pdata}->delete_attribute("data");
190 6         22 return $self;
191             }
192              
193             sub _deprecated {
194 26     26   36 my ($msg) = @_;
195 26         124 my $method = (caller(1))[3];
196 26         3296 carp "$method is deprecated. $msg";
197             }
198              
199             sub set_join {
200 24     24 0 106 my ($self, %joins) = @_;
201 24         48 _deprecated("Use Gnuplot::Builder::JoinDict.");
202 24         205 foreach my $name (keys %joins) {
203             $self->{pdata_join}->set_attribute(
204 28         86 key => $name, value => $joins{$name}, quote => 0
205             );
206             }
207 24         100 return $self;
208             }
209              
210             sub delete_join {
211 2     2 0 6 my ($self, @names) = @_;
212 2         5 _deprecated("Use Gnuplot::Builder::JoinDict.");
213 2         13 foreach my $name (@names) {
214 2         8 $self->{pdata_join}->delete_attribute($name);
215             }
216 2         8 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__