File Coverage

blib/lib/HTML/Widget/Plugin.pm
Criterion Covered Total %
statement 54 55 98.1
branch 12 16 75.0
condition 1 3 33.3
subroutine 13 14 92.8
pod 5 5 100.0
total 85 93 91.4


line stmt bran cond sub pod time code
1 15     15   8431 use strict;
  15         28  
  15         426  
2 15     15   74 use warnings;
  15         27  
  15         581  
3             package HTML::Widget::Plugin;
4             # ABSTRACT: base class for HTML widgets
5             $HTML::Widget::Plugin::VERSION = '0.204';
6 15     15   71 use Carp ();
  15         27  
  15         276  
7 15     15   11482 use List::MoreUtils qw(uniq);
  15         176447  
  15         114  
8 15     15   9582 use MRO::Compat;
  15         31  
  15         379  
9 15     15   77 use Scalar::Util qw(reftype);
  15         33  
  15         1259  
10 15     15   10600 use Sub::Install;
  15         24248  
  15         64  
11              
12             #pod =head1 DESCRIPTION
13             #pod
14             #pod This class provides a simple way to write plugins for HTML::Widget::Factory.
15             #pod
16             #pod =head1 METHODS
17             #pod
18             #pod =head2 new
19             #pod
20             #pod my $plugin = Plugin->new( \%arg );
21             #pod
22             #pod The default plugin constructor is really simple. It requires that the argument
23             #pod is either a hashref or not given.
24             #pod
25             #pod =cut
26              
27             sub new {
28 200     200 1 10935 my ($class, $arg) = @_;
29 200 100       482 $arg = {} unless defined $arg;
30              
31 200 50 33     1206 Carp::confess("illegal argument to $class->new: $arg")
32             unless ref $arg and reftype $arg eq 'HASH';
33              
34 200         229 my @attribute_args;
35 200         231 for (@{ mro::get_linear_isa($class) }) {
  200         759  
36 449 50       2554 next unless $_->can('_attribute_args');
37 449         1273 push @attribute_args, $_->_attribute_args(@_);
38             }
39 200         1617 @attribute_args = uniq @attribute_args;
40              
41 200         494 my @boolean_args;
42 200         295 for ($class, @{ mro::get_linear_isa($class) }) {
  200         622  
43 649 50       2993 next unless $_->can('_boolean_args');
44 649         1667 push @boolean_args, $_->_boolean_args(@_);
45             }
46 200         743 @boolean_args = uniq @boolean_args;
47              
48 200         2748 bless {
49             %$arg,
50             _attribute_args => \@attribute_args,
51             _boolean_args => \@boolean_args,
52             }, $class;
53             }
54              
55             #pod =head2 rewrite_arg
56             #pod
57             #pod $arg = $plugin->rewrite_arg($arg);
58             #pod
59             #pod This method returns a reference to a hash of arguments, rewriting the given
60             #pod hash reference to place arguments that are intended to become element
61             #pod attributes into the C parameter.
62             #pod
63             #pod It moves attributes listed in the results of the C method.
64             #pod
65             #pod =cut
66              
67             sub rewrite_arg {
68 57     57 1 94 my ($class, $given_arg) = @_;
69 57 100       280 my $arg = { $given_arg ? %$given_arg : () };
70              
71 57         208 my %bool = map { $_ => 1 } $class->boolean_args;
  49         179  
72              
73 57         187 for ($class->attribute_args) {
74 353 100       906 if (exists $arg->{$_}) {
75 82         239 $arg->{attr}{$_} = delete $arg->{$_};
76 82 50       265 $arg->{attr}{$_} = $arg->{attr}{$_} ? $_ : undef if $bool{$_};
    100          
77             }
78             }
79              
80 57         199 return $arg;
81             }
82              
83             #pod =head2 C< attribute_args >
84             #pod
85             #pod This method returns a list of argument names, the values of which should be
86             #pod used as HTML element attributes.
87             #pod
88             #pod The default implementation climbs the plugin's inheritance tree, calling
89             #pod C<_attribute_args> and pushing all the results onto a list from which unique
90             #pod results are then returned.
91             #pod
92             #pod =cut
93              
94             sub attribute_args {
95 57     57 1 90 my ($self) = shift;
96 57         75 return @{ $self->{_attribute_args} };
  57         243  
97             }
98              
99 219     219   784 sub _attribute_args { qw(id name class tabindex) }
100              
101             #pod =head2 C< boolean_args >
102             #pod
103             #pod This method returns a list of argument names, the values of which should be
104             #pod treated as booleans.
105             #pod
106             #pod The default implementation climbs the plugin's inheritance tree, calling
107             #pod C<_boolean_args> and pushing all the results onto a list from which unique
108             #pod results are then returned.
109             #pod
110             #pod =cut
111              
112             sub boolean_args {
113 57     57 1 87 my ($self) = shift;
114 57         74 return @{ $self->{_boolean_args} };
  57         197  
115             }
116              
117 302     302   522 sub _boolean_args { () }
118              
119             #pod =head2 C< provided_widgets >
120             #pod
121             #pod This method should be implemented by any plugin. It returns a list of method
122             #pod names which a factor should delegate to this plugin.
123             #pod
124             #pod =cut
125              
126             sub provided_widgets {
127 0     0 1   Carp::croak
128             "something called abstract provided_widgets in HTML::Widget::Plugin";
129             }
130              
131             1;
132              
133             __END__