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   9654 use strict;
  15         29  
  15         534  
2 15     15   80 use warnings;
  15         30  
  15         678  
3             package HTML::Widget::Plugin;
4             # ABSTRACT: base class for HTML widgets
5             $HTML::Widget::Plugin::VERSION = '0.202';
6 15     15   81 use Carp ();
  15         28  
  15         294  
7 15     15   13397 use List::MoreUtils qw(uniq);
  15         20507  
  15         1430  
8 15     15   111 use MRO::Compat;
  15         29  
  15         375  
9 15     15   78 use Scalar::Util qw(reftype);
  15         26  
  15         1187  
10 15     15   12836 use Sub::Install;
  15         26492  
  15         399  
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 12132 my ($class, $arg) = @_;
29 200 100       498 $arg = {} unless defined $arg;
30              
31 200 50 33     1355 Carp::confess("illegal argument to $class->new: $arg")
32             unless ref $arg and reftype $arg eq 'HASH';
33              
34 200         232 my @attribute_args;
35 200         229 for (@{ mro::get_linear_isa($class) }) {
  200         973  
36 449 50       3662 next unless $_->can('_attribute_args');
37 449         1460 push @attribute_args, $_->_attribute_args(@_);
38             }
39 200         2467 @attribute_args = uniq @attribute_args;
40              
41 200         689 my @boolean_args;
42 200         241 for ($class, @{ mro::get_linear_isa($class) }) {
  200         798  
43 649 50       3466 next unless $_->can('_boolean_args');
44 649         1819 push @boolean_args, $_->_boolean_args(@_);
45             }
46 200         885 @boolean_args = uniq @boolean_args;
47              
48 200         3353 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 56     56 1 125 my ($class, $given_arg) = @_;
69 56 100       539 my $arg = { $given_arg ? %$given_arg : () };
70              
71 56         266 my %bool = map { $_ => 1 } $class->boolean_args;
  48         205  
72              
73 56         228 for ($class->attribute_args) {
74 348 100       879 if (exists $arg->{$_}) {
75 82         259 $arg->{attr}{$_} = delete $arg->{$_};
76 82 50       278 $arg->{attr}{$_} = $arg->{attr}{$_} ? $_ : undef if $bool{$_};
    100          
77             }
78             }
79              
80 56         294 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 56     56 1 105 my ($self) = shift;
96 56         74 return @{ $self->{_attribute_args} };
  56         301  
97             }
98              
99 219     219   918 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 56     56 1 104 my ($self) = shift;
114 56         91 return @{ $self->{_boolean_args} };
  56         307  
115             }
116              
117 302     302   677 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__