File Coverage

blib/lib/Khonsu/Ra.pm
Criterion Covered Total %
statement 230 244 94.2
branch 17 24 70.8
condition 5 6 83.3
subroutine 62 66 93.9
pod 0 6 0.0
total 314 346 90.7


line stmt bran cond sub pod time code
1             package Khonsu::Ra;
2              
3 5     5   2196 use strict;
  5         15  
  5         158  
4 5     5   30 use warnings;
  5         10  
  5         137  
5              
6 5     5   2262 use Clone;
  5         12934  
  5         239  
7              
8 5     5   2004 use Khonsu::Shape::Line;
  5         14  
  5         177  
9 5     5   2046 use Khonsu::Shape::Box;
  5         12  
  5         169  
10 5     5   2014 use Khonsu::Shape::Circle;
  5         15  
  5         190  
11 5     5   1955 use Khonsu::Shape::Pie;
  5         12  
  5         187  
12 5     5   1999 use Khonsu::Shape::Ellipse;
  5         19  
  5         202  
13              
14 5     5   1918 use Khonsu::Font;
  5         14  
  5         175  
15              
16 5     5   1992 use Khonsu::Text;
  5         14  
  5         182  
17 5     5   2056 use Khonsu::Text::H1;
  5         14  
  5         210  
18 5     5   1956 use Khonsu::Text::H2;
  5         22  
  5         189  
19 5     5   2034 use Khonsu::Text::H3;
  5         14  
  5         188  
20 5     5   1968 use Khonsu::Text::H4;
  5         12  
  5         245  
21 5     5   2347 use Khonsu::Text::H5;
  5         18  
  5         217  
22 5     5   1960 use Khonsu::Text::H6;
  5         13  
  5         198  
23              
24 5     5   1918 use Khonsu::Image;
  5         16  
  5         213  
25              
26 5     5   2027 use Khonsu::Form;
  5         12  
  5         241  
27 5     5   2081 use Khonsu::Form::Field::Input;
  5         19  
  5         250  
28 5     5   2097 use Khonsu::Form::Field::Select;
  5         11  
  5         257  
29 5     5   2083 use Khonsu::Form::Field::Checkbox;
  5         18  
  5         300  
30              
31 5     5   2058 use Khonsu::TOC;
  5         16  
  5         295  
32              
33 5     5   3196 use Types::Standard qw/Str Object ArrayRef HashRef Num Bool CodeRef/;
  5         388117  
  5         56  
34              
35 5     5   8301 use constant RW => (is => 'rw');
  5         14  
  5         502  
36 5     5   33 use constant REQ => (required => 1);
  5         10  
  5         335  
37 5     5   36 use constant STR => (isa => Str);
  5         15  
  5         30  
38 5     5   325 use constant OBJ => (isa => Object);
  5         15  
  5         48  
39 5     5   393 use constant BOOL => (isa => Bool);
  5         19  
  5         54  
40 5     5   385 use constant AR => (isa => ArrayRef);
  5         10  
  5         27  
41 5     5   419 use constant HR => (isa => HashRef);
  5         14  
  5         26  
42 5     5   479 use constant DAR => (isa => ArrayRef, default => sub { [ ] });
  5         31  
  5         23  
  354         1350  
43 5     5   430 use constant DHR => (isa => HashRef, default => sub { { } });
  5         9  
  5         14  
  748         1676  
44 5     5   412 use constant NUM => (isa => Num);
  5         22  
  5         30  
45 5     5   357 use constant CODE => (isa => CodeRef);
  5         17  
  5         46  
46 5         42 use constant POINTS => (
47             x => {is => 'rw', isa => Num},
48             y => {is => 'rw', isa => Num},
49             w => {is => 'rw', isa => Num},
50             h => {is => 'rw', isa => Num},
51 5     5   521 );
  5         14  
52             use constant LINE => (
53 4         65 line => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Line->new() }}
54 5     5   559 );
  5         8  
  5         21  
55             use constant BOX => (
56 94         493 box => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Box->new() }}
57 5     5   647 );
  5         9  
  5         40  
58             use constant CIRCLE => (
59 4         39 circle => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Circle->new() }}
60 5     5   573 );
  5         11  
  5         50  
61             use constant PIE => (
62 4         48 pie => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Pie->new() }}
63 5     5   634 );
  5         36  
  5         27  
64             use constant ELLIPSE => (
65 4         28 ellipse => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Ellipse->new() }}
66 5     5   605 );
  5         21  
  5         23  
67             use constant FONT => (
68 402         1076 font => {is => 'rw', isa => Object, default => sub { Khonsu::Font->new() }}
69 5     5   617 );
  5         12  
  5         17  
70             use constant TEXT => (
71 4         40 text => {is => 'rw', isa => Object, default => sub { Khonsu::Text->new() }}
72 5     5   640 );
  5         15  
  5         19  
73             use constant H1 => (
74 4         56 h1 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H1->new() }}
75 5     5   576 );
  5         12  
  5         20  
76             use constant H2 => (
77 4         41 h2 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H2->new() }}
78 5     5   568 );
  5         20  
  5         16  
79             use constant H3 => (
80 4         38 h3 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H3->new() }}
81 5     5   667 );
  5         15  
  5         17  
82             use constant H4 => (
83 4         39 h4 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H4->new() }}
84 5     5   634 );
  5         22  
  5         23  
85             use constant H5 => (
86 4         49 h5 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H5->new() }}
87 5     5   643 );
  5         18  
  5         34  
88             use constant H6 => (
89 4         52 h6 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H6->new() }}
90 5     5   558 );
  5         10  
  5         40  
91             use constant IMAGE => (
92 4         46 image => {is => 'rw', isa => Object, default => sub { Khonsu::Image->new() }}
93 5     5   589 );
  5         10  
  5         19  
94             use constant TOC => (
95 4         38 toc => {is => 'rw', isa => Object, default => sub { Khonsu::TOC->new() }}
96 5     5   648 );
  5         20  
  5         18  
97             use constant FORM => (
98 4         37 form => {is => 'rw', isa => Object, default => sub { Khonsu::Form->new() }}
99 5     5   605 );
  5         9  
  5         17  
100             use constant INPUT => (
101 4         80 input => {is => 'rw', isa => Object, default => sub { Khonsu::Form::Field::Input->new() }}
102 5     5   524 );
  5         40  
  5         26  
103             use constant SELECT => (
104 4         43 select => {is => 'rw', isa => Object, default => sub { Khonsu::Form::Field::Select->new() }}
105 5     5   626 );
  5         20  
  5         22  
106             use constant CHECKBOX => (
107 4         38 checkbox => {is => 'rw', isa => Object, default => sub { Khonsu::Form::Field::Checkbox->new() }}
108 5     5   592 );
  5         19  
  5         35  
109             sub new {
110 1012     1012 0 3311 my ($pkg, %params) = @_;
111              
112 1012         3779 my $self = bless {
113             attributes => {}
114             }, $pkg;
115 1012         3294 my @attributes = $self->attributes();
116 1012         3801 for (my $i = 0; $i < $#attributes; $i += 2) {
117 11054         22300 my ($key, $value) = ($attributes[$i], $attributes[$i + 1]);
118 0     0   0 $self->{attributes}->{$key} = $value->{is} eq 'ro' ? sub { $_[0]->{$key}; } : sub {
119 72484     72484   117459 my ($self, $val) = @_;
120 72484 100       124434 if (defined $val) {
121 12767 100       47698 if ($value->{isa}) {
122 12759         79601 $val = $value->{isa}->($val);
123             }
124 12767         271168 $self->{$key} = $val;
125             }
126 72484         320949 return $self->{$key};
127 11054 50       57182 };
128              
129 11054 50 66     25417 if ($value->{required} && ! defined $params{$key}) {
130 0         0 die "$key is required";
131             }
132            
133 11054 100 100     25085 if ($value->{default} && !$params{$key}) {
134 4680         10907 $params{$key} = $value->{default}->($self);
135             }
136              
137 11054 100       22778 if (defined $params{$key}) {
138 7695 50       22437 if ($value->{isa}) {
139 7695         48484 $params{$key} = $value->{isa}->($params{$key});
140             }
141 7695         155746 $self->{$key} = $params{$key};
142             }
143             }
144              
145 1012         1735 for my $plugin (@{$params{PLUGINS}}) {
  1012         2732  
146 0         0 my ($key) = lc($plugin) =~ m/::([^:]*)$/;
147             $self->{attributes}->{$key} = sub {
148 0     0   0 my ($self, $val) = @_;
149 0 0       0 if (defined $val) {
150 0         0 $self->{$key} = $val;
151             }
152 0         0 return $self->{$key};
153 0         0 };
154 0         0 $self->{$key} = $plugin->new();
155             $self->{attributes}->{"add_$key"} = sub {
156 0     0   0 my ($self, %args) = @_;
157 0         0 $self->$key->add($self, %args);
158 0         0 return $self;
159 0         0 };
160             }
161              
162 1012 100       4315 $self->BUILD(%params) if $self->can('BUILD');
163            
164 1012         5189 return $self;
165             }
166              
167             sub set_points {
168 90     90 0 4666 my ($self, $mx, $my, $mw, $mh) = @_;
169 90         493 $self->x($mx);
170 90         452 $self->y($my);
171 90         413 $self->w($mw);
172 90         418 $self->h($mh);
173 90         218 return $self;
174             }
175              
176             sub get_points {
177 1892     1892 0 3434 my ($self) = shift;
178             return (
179 1892         7255 x => $self->x,
180             y => $self->y,
181             w => $self->w,
182             h => $self->h
183             );
184             }
185              
186 110     110 0 242 sub attributes { return (); }
187              
188             sub set_attributes {
189 2894     2894 0 8042 my ($self, %params) = @_;
190 2894         7343 for (keys %params) {
191 7330 100       16773 next unless $self->{attributes}->{$_};
192 5797         16970 $self->{attributes}->{$_}($self, $params{$_});
193             }
194 2894         6719 return $self;
195             }
196              
197             sub clone {
198 170     170 0 304 my ($self) = @_;
199 170         312 my $ref = ref $self;
200 170         12848 return bless Clone::clone($self), $ref;
201             }
202              
203       0     sub DESTROY {}
204              
205             sub AUTOLOAD {
206 66687     66687   288548 my $classname = ref $_[0];
207 66687         80281 my $validname = '[_a-zA-Z][\:a-zA-Z0-9_]*';
208 66687         785966 our $AUTOLOAD =~ /^${classname}::($validname)$/;
209 66687         163271 my $key = $1;
210 66687 50       120562 die "illegal key name, must be of $validname form\n$AUTOLOAD" unless $key;
211 66687 50       154130 if ( $_[0]->{attributes}->{$key} ) {
212 66687         148525 $_[0]->{attributes}->{$key}->(@_);
213             } else {
214 0           die "illegal use of AUTOLOAD $classname -> $key -";
215             }
216             }
217              
218             1;