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   2711 use strict;
  5         35  
  5         243  
4 5     5   28 use warnings;
  5         10  
  5         327  
5              
6 5     5   2715 use Clone;
  5         3265  
  5         371  
7              
8 5     5   2548 use Khonsu::Shape::Line;
  5         18  
  5         208  
9 5     5   2574 use Khonsu::Shape::Box;
  5         21  
  5         233  
10 5     5   2509 use Khonsu::Shape::Circle;
  5         21  
  5         204  
11 5     5   2512 use Khonsu::Shape::Pie;
  5         18  
  5         215  
12 5     5   2296 use Khonsu::Shape::Ellipse;
  5         17  
  5         247  
13              
14 5     5   2898 use Khonsu::Font;
  5         18  
  5         257  
15              
16 5     5   2460 use Khonsu::Text;
  5         19  
  5         263  
17 5     5   2416 use Khonsu::Text::H1;
  5         18  
  5         276  
18 5     5   2536 use Khonsu::Text::H2;
  5         39  
  5         274  
19 5     5   2431 use Khonsu::Text::H3;
  5         16  
  5         242  
20 5     5   2400 use Khonsu::Text::H4;
  5         16  
  5         276  
21 5     5   2534 use Khonsu::Text::H5;
  5         18  
  5         301  
22 5     5   2389 use Khonsu::Text::H6;
  5         19  
  5         269  
23              
24 5     5   2406 use Khonsu::Image;
  5         19  
  5         302  
25              
26 5     5   2616 use Khonsu::Form;
  5         17  
  5         378  
27 5     5   2559 use Khonsu::Form::Field::Input;
  5         16  
  5         302  
28 5     5   2575 use Khonsu::Form::Field::Select;
  5         84  
  5         377  
29 5     5   3083 use Khonsu::Form::Field::Checkbox;
  5         14  
  5         327  
30              
31 5     5   2507 use Khonsu::TOC;
  5         17  
  5         371  
32              
33 5     5   3492 use Types::Standard qw/Str Object ArrayRef HashRef Num Bool CodeRef/;
  5         730364  
  5         93  
34              
35 5     5   25362 use constant RW => (is => 'rw');
  5         15  
  5         631  
36 5     5   59 use constant REQ => (required => 1);
  5         11  
  5         451  
37 5     5   35 use constant STR => (isa => Str);
  5         12  
  5         30  
38 5     5   484 use constant OBJ => (isa => Object);
  5         12  
  5         31  
39 5     5   495 use constant BOOL => (isa => Bool);
  5         15  
  5         28  
40 5     5   504 use constant AR => (isa => ArrayRef);
  5         13  
  5         27  
41 5     5   494 use constant HR => (isa => HashRef);
  5         40  
  5         30  
42 5     5   576 use constant DAR => (isa => ArrayRef, default => sub { [ ] });
  5         11  
  5         49  
  564         1981  
43 5     5   566 use constant DHR => (isa => HashRef, default => sub { { } });
  5         12  
  5         25  
  1168         4565  
44 5     5   627 use constant NUM => (isa => Num);
  5         13  
  5         30  
45 5     5   529 use constant CODE => (isa => CodeRef);
  5         34  
  5         39  
46 5         32 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   762 );
  5         11  
52             use constant LINE => (
53 4         102 line => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Line->new() }}
54 5     5   850 );
  5         10  
  5         26  
55             use constant BOX => (
56 133         830 box => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Box->new() }}
57 5     5   858 );
  5         11  
  5         31  
58             use constant CIRCLE => (
59 4         41 circle => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Circle->new() }}
60 5     5   799 );
  5         11  
  5         21  
61             use constant PIE => (
62 4         55 pie => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Pie->new() }}
63 5     5   808 );
  5         12  
  5         20  
64             use constant ELLIPSE => (
65 4         53 ellipse => {is => 'rw', isa => Object, default => sub { Khonsu::Shape::Ellipse->new() }}
66 5     5   687 );
  5         12  
  5         28  
67             use constant FONT => (
68 612         3643 font => {is => 'rw', isa => Object, default => sub { Khonsu::Font->new() }}
69 5     5   817 );
  5         12  
  5         24  
70             use constant TEXT => (
71 4         95 text => {is => 'rw', isa => Object, default => sub { Khonsu::Text->new() }}
72 5     5   751 );
  5         21  
  5         21  
73             use constant H1 => (
74 4         55 h1 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H1->new() }}
75 5     5   778 );
  5         10  
  5         25  
76             use constant H2 => (
77 4         57 h2 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H2->new() }}
78 5     5   848 );
  5         10  
  5         41  
79             use constant H3 => (
80 4         48 h3 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H3->new() }}
81 5     5   737 );
  5         12  
  5         33  
82             use constant H4 => (
83 4         52 h4 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H4->new() }}
84 5     5   680 );
  5         25  
  5         19  
85             use constant H5 => (
86 4         53 h5 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H5->new() }}
87 5     5   737 );
  5         11  
  5         19  
88             use constant H6 => (
89 4         53 h6 => {is => 'rw', isa => Object, default => sub { Khonsu::Text::H6->new() }}
90 5     5   876 );
  5         24  
  5         46  
91             use constant IMAGE => (
92 4         54 image => {is => 'rw', isa => Object, default => sub { Khonsu::Image->new() }}
93 5     5   903 );
  5         20  
  5         29  
94             use constant TOC => (
95 4         49 toc => {is => 'rw', isa => Object, default => sub { Khonsu::TOC->new() }}
96 5     5   660 );
  5         21  
  5         28  
97             use constant FORM => (
98 4         47 form => {is => 'rw', isa => Object, default => sub { Khonsu::Form->new() }}
99 5     5   696 );
  5         12  
  5         20  
100             use constant INPUT => (
101 4         64 input => {is => 'rw', isa => Object, default => sub { Khonsu::Form::Field::Input->new() }}
102 5     5   711 );
  5         11  
  5         26  
103             use constant SELECT => (
104 4         57 select => {is => 'rw', isa => Object, default => sub { Khonsu::Form::Field::Select->new() }}
105 5     5   800 );
  5         10  
  5         20  
106             use constant CHECKBOX => (
107 4         58 checkbox => {is => 'rw', isa => Object, default => sub { Khonsu::Form::Field::Checkbox->new() }}
108 5     5   763 );
  5         13  
  5         26  
109             sub new {
110 1510     1510 0 7673 my ($pkg, %params) = @_;
111              
112 1510         6932 my $self = bless {
113             attributes => {}
114             }, $pkg;
115 1510         7324 my @attributes = $self->attributes();
116 1510         6960 for (my $i = 0; $i < $#attributes; $i += 2) {
117 16757         42401 my ($key, $value) = ($attributes[$i], $attributes[$i + 1]);
118 0     0   0 $self->{attributes}->{$key} = $value->{is} eq 'ro' ? sub { $_[0]->{$key}; } : sub {
119 109711     109711   213101 my ($self, $val) = @_;
120 109711 100       227552 if (defined $val) {
121 18929 100       104128 if ($value->{isa}) {
122 18921         144479 $val = $value->{isa}->($val);
123             }
124 18929         545485 $self->{$key} = $val;
125             }
126 109711         675709 return $self->{$key};
127 16757 50       139865 };
128              
129 16757 50 66     45871 if ($value->{required} && ! defined $params{$key}) {
130 0         0 die "$key is required";
131             }
132            
133 16757 100 100     56548 if ($value->{default} && !$params{$key}) {
134 7107         21075 $params{$key} = $value->{default}->($self);
135             }
136              
137 16757 100       45320 if (defined $params{$key}) {
138 11830 50       41985 if ($value->{isa}) {
139 11830         92992 $params{$key} = $value->{isa}->($params{$key});
140             }
141 11830         311262 $self->{$key} = $params{$key};
142             }
143             }
144              
145 1510         2798 for my $plugin (@{$params{PLUGINS}}) {
  1510         5512  
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 1510 100       8249 $self->BUILD(%params) if $self->can('BUILD');
163            
164 1510         12825 return $self;
165             }
166              
167             sub set_points {
168 129     129 0 10594 my ($self, $mx, $my, $mw, $mh) = @_;
169 129         1024 $self->x($mx);
170 129         877 $self->y($my);
171 129         734 $self->w($mw);
172 129         652 $self->h($mh);
173 129         413 return $self;
174             }
175              
176             sub get_points {
177 2927     2927 0 7526 my ($self) = shift;
178             return (
179 2927         14404 x => $self->x,
180             y => $self->y,
181             w => $self->w,
182             h => $self->h
183             );
184             }
185              
186 149     149 0 374 sub attributes { return (); }
187              
188             sub set_attributes {
189 4466     4466 0 18654 my ($self, %params) = @_;
190 4466         16657 for (keys %params) {
191 12115 100       36150 next unless $self->{attributes}->{$_};
192 8902         31862 $self->{attributes}->{$_}($self, $params{$_});
193             }
194 4466         14630 return $self;
195             }
196              
197             sub clone {
198 248     248 0 497 my ($self) = @_;
199 248         589 my $ref = ref $self;
200 248         25162 return bless Clone::clone($self), $ref;
201             }
202              
203       0     sub DESTROY {}
204              
205             sub AUTOLOAD {
206 100809     100809   533884 my $classname = ref $_[0];
207 100809         150450 my $validname = '[_a-zA-Z][\:a-zA-Z0-9_]*';
208 100809         3058483 our $AUTOLOAD =~ /^${classname}::($validname)$/;
209 100809         289231 my $key = $1;
210 100809 50       244721 die "illegal key name, must be of $validname form\n$AUTOLOAD" unless $key;
211 100809 50       310076 if ( $_[0]->{attributes}->{$key} ) {
212 100809         269366 $_[0]->{attributes}->{$key}->(@_);
213             } else {
214 0           die "illegal use of AUTOLOAD $classname -> $key -";
215             }
216             }
217              
218             1;