| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package HTML::Widget::Plugin::Struct; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: dump data structures for CGI::Expand expansion | ||||||||||||||||||||||||||||
| 5 | 2 | $HTML::Widget::Plugin::Struct::VERSION = '0.005'; | |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use parent qw(HTML::Widget::Plugin); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||||||||||
| 10 | #pod This plugin provides a means to dump a (somewhat) complex Perl data structure | ||||||||||||||||||||||||||||
| 11 | #pod to hidden widgets which can then be reconstructed by L<CGI::Expand>. | ||||||||||||||||||||||||||||
| 12 | #pod | ||||||||||||||||||||||||||||
| 13 | #pod =cut | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | #pod =method provided_widgets | ||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||
| 19 | #pod This plugin provides the following widgets: struct | ||||||||||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||||||||||
| 21 | #pod =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub provided_widgets { qw(struct) } | ||||||||||||||||||||||||||
|
Subroutine
provided_widgets: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #pod =method struct | ||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||
| 27 | #pod C<struct> is the only widget provided by this plugin. It accepts four | ||||||||||||||||||||||||||||
| 28 | #pod arguments: | ||||||||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||||||||
| 30 | #pod * name - the base name for the widget (required, will default to id if given) | ||||||||||||||||||||||||||||
| 31 | #pod * id - the base id for the widget (optional) | ||||||||||||||||||||||||||||
| 32 | #pod * class - a class to apply to each element generated (optional) | ||||||||||||||||||||||||||||
| 33 | #pod * value - the structure to represent | ||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||
| 35 | #pod The value can be an arbitrarily deep structure built from simple scalars, hash | ||||||||||||||||||||||||||||
| 36 | #pod references, and array references. The inclusion of any other kind of data will | ||||||||||||||||||||||||||||
| 37 | #pod cause an exception to be raised. | ||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||
| 39 | #pod References which appear twice will be treated as multiple occurances of | ||||||||||||||||||||||||||||
| 40 | #pod identical structures. It won't be possible to tell that they were originally | ||||||||||||||||||||||||||||
| 41 | #pod references to the same datum. Any circularity in the structure will cause an | ||||||||||||||||||||||||||||
| 42 | #pod exception to be raised. | ||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||
| 44 | #pod =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | ▶ | sub struct { | ||||||||||||||||||||||||||
|
Subroutine
struct: called CC=6 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 5 | my ($self, $factory, $arg) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $arg->{attr}{name} = $arg->{attr}{id} | ||||||||||||||||||||||||||||
| 50 | 5 | ▶ | if ! defined $arg->{attr}{name} and defined $arg->{attr}{id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $arg->{"attr"}{"name"} and defined $arg->{"attr"}{"id"}
MC/DC0%0/2
not defined $arg->{"attr"}{"name"} and defined $arg->{"attr"}{"id"}
not defined $arg->{"attr"}{"name"}
defined $arg->{"attr"}{"id"}
Truth table33%1/3
not(A) and B
A= not defined $arg->{"attr"}{"name"}B= defined $arg->{"attr"}{"id"}
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Carp::croak "no name provided for struct widget" unless | ||||||||||||||||||||||||||||
| 53 | 5 | ▶ | defined $arg->{attr}{name} and length $arg->{attr}{name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $arg->{"attr"}{"name"} and length $arg->{"attr"}{"name"}
MC/DC0%0/2
defined $arg->{"attr"}{"name"} and length $arg->{"attr"}{"name"}
defined $arg->{"attr"}{"name"}
length $arg->{"attr"}{"name"}
Truth table33%1/3
A and B
A= defined $arg->{"attr"}{"name"}B= length $arg->{"attr"}{"name"}
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | return unless defined $arg->{value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 5 | my $ref_stack = []; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 5 | $self->_build_struct($factory, $arg, $ref_stack); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | my %DUMPER_FOR = ( | |||||||||||||||||||||||||||
| 63 | '' => '_build_scalar_struct', | ||||||||||||||||||||||||||||
| 64 | HASH => '_build_hash_struct', | ||||||||||||||||||||||||||||
| 65 | ARRAY => '_build_array_struct', | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 24 | ▶ | sub _build_struct { | ||||||||||||||||||||||||||
|
Subroutine
_build_struct: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 69 | 24 | my ($self, $factory, $arg, $ref_stack) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 24 | ▶ | return '' unless defined $arg->{value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Carp::croak "looping data structure detected while dumping struct" | ||||||||||||||||||||||||||||
| 74 | if ref $arg->{value} | ||||||||||||||||||||||||||||
| 75 | 24 | ▶ | and grep { $_ == Scalar::Util::refaddr($arg->{value}) } @$ref_stack; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $arg->{"value"} and grep {$_ == builtin::refaddr($arg->{"value"});} @$ref_stack
MC/DC100%2/2
ref $arg->{"value"} and grep {$_ == builtin::refaddr($arg->{"value"});} @$ref_stack
ref $arg->{"value"}
grep {$_ == builtin::refaddr($arg->{"value"});} @$ref_stack
Truth table100%3/3
A and B
A= ref $arg->{"value"}B= grep {$_ == builtin::refaddr($arg->{"value"});} @$ref_stack
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 23 | $self->_assert_value_ok($arg->{value}); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 23 | my $method = $DUMPER_FOR{ ref $arg->{value} }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 23 | return $self->$method($factory, $arg, $ref_stack); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 16 | ▶ | sub _build_scalar_struct { | ||||||||||||||||||||||||||
|
Subroutine
_build_scalar_struct: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 16 | my ($self, $factory, $arg) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | return $factory->hidden({ | ||||||||||||||||||||||||||||
| 88 | name => $arg->{attr}{name}, | ||||||||||||||||||||||||||||
| 89 | id => $arg->{attr}{id}, | ||||||||||||||||||||||||||||
| 90 | value => $arg->{value}, | ||||||||||||||||||||||||||||
| 91 | class => $arg->{attr}{class}, | ||||||||||||||||||||||||||||
| 92 | 16 | }); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | ▶ | sub _build_hash_struct { | ||||||||||||||||||||||||||
|
Subroutine
_build_hash_struct: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 96 | 3 | my ($self, $factory, $arg, $ref_stack) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | my $has_id = defined $arg->{attr}{id} && length $arg->{attr}{id}; | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $arg->{'attr'}{'id'} && length $arg->{'attr'}{'id'}
MC/DC0%0/2
defined $arg->{'attr'}{'id'} && length $arg->{'attr'}{'id'}
defined $arg->{'attr'}{'id'}
length $arg->{'attr'}{'id'}
Truth table33%1/3
A && B
A= defined $arg->{'attr'}{'id'}B= length $arg->{'attr'}{'id'}
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 3 | my $widget = ''; | |||||||||||||||||||||||||||
| 101 | 3 | push @$ref_stack, Scalar::Util::refaddr($arg->{value}); | |||||||||||||||||||||||||||
| 102 | 3 | for my $key (keys %{ $arg->{value} }) { | |||||||||||||||||||||||||||
| 103 | $widget .= $self->_build_struct( | ||||||||||||||||||||||||||||
| 104 | $factory, | ||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | value => $arg->{value}{$key}, | ||||||||||||||||||||||||||||
| 107 | attr => { | ||||||||||||||||||||||||||||
| 108 | ($has_id ? (id => "$arg->{attr}{id}.$key") : ()), | ||||||||||||||||||||||||||||
| 109 | name => "$arg->{attr}{name}.$key", | ||||||||||||||||||||||||||||
| 110 | class => $arg->{attr}{class}, | ||||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||
| 112 | }, | ||||||||||||||||||||||||||||
| 113 | 5 | ▶ | $ref_stack, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 3 | pop @$ref_stack; | |||||||||||||||||||||||||||
| 117 | 3 | return $widget; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 4 | ▶ | sub _build_array_struct { | ||||||||||||||||||||||||||
|
Subroutine
_build_array_struct: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 121 | 4 | my ($self, $factory, $arg, $ref_stack) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 4 | ▶ | my $has_id = defined $arg->{attr}{id} && length $arg->{attr}{id}; | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $arg->{'attr'}{'id'} && length $arg->{'attr'}{'id'}
MC/DC0%0/2
defined $arg->{'attr'}{'id'} && length $arg->{'attr'}{'id'}
defined $arg->{'attr'}{'id'}
length $arg->{'attr'}{'id'}
Truth table33%1/3
A && B
A= defined $arg->{'attr'}{'id'}B= length $arg->{'attr'}{'id'}
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 4 | my $widget = ''; | |||||||||||||||||||||||||||
| 126 | 4 | push @$ref_stack, Scalar::Util::refaddr($arg->{value}); | |||||||||||||||||||||||||||
| 127 | 4 | for my $index (0 .. $#{ $arg->{value} }) { | |||||||||||||||||||||||||||
| 128 | 14 | ▶ | next unless defined $arg->{value}[$index]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | $widget .= $self->_build_struct( | ||||||||||||||||||||||||||||
| 130 | $factory, | ||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||
| 132 | value => $arg->{value}[$index], | ||||||||||||||||||||||||||||
| 133 | attr => { | ||||||||||||||||||||||||||||
| 134 | name => "$arg->{attr}{name}.$index", | ||||||||||||||||||||||||||||
| 135 | ($has_id ? (id => "$arg->{attr}{id}.$index") : ()), | ||||||||||||||||||||||||||||
| 136 | class => $arg->{attr}{class}, | ||||||||||||||||||||||||||||
| 137 | }, | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | 14 | ▶ | $ref_stack, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 3 | pop @$ref_stack; | |||||||||||||||||||||||||||
| 143 | 3 | return $widget; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 23 | ▶ | sub _assert_value_ok { | ||||||||||||||||||||||||||
|
Subroutine
_assert_value_ok: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 147 | 23 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 23 | ▶ | return unless length (my $ref = ref $value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 7 | ▶ | Carp::croak "can't widgetize objects" if Scalar::Util::blessed($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 7 | ▶ | Carp::croak "can't serialize $ref references" unless $DUMPER_FOR{ $ref }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | #pod =head1 TODO | ||||||||||||||||||||||||||||
| 155 | #pod | ||||||||||||||||||||||||||||
| 156 | #pod =for :list | ||||||||||||||||||||||||||||
| 157 | #pod * improve the test suite | ||||||||||||||||||||||||||||
| 158 | #pod | ||||||||||||||||||||||||||||
| 159 | #pod =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||