« lib/Perl/ToPerl6/Transformer.pm
↑ Summary
lib/Perl/ToPerl6/Transformer/Variables/RewriteSpecialVariables.pm »
| 1 | package Perl::ToPerl6::TransformerParameter; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 23 | ▶ | use 5.006001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 23 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 23 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 23 | ▶ | use Readonly; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 23 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 23 | Readonly::Array our @EXPORT_OK => qw{ $NO_DESCRIPTION_AVAILABLE }; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 23 | ▶ | use String::Format qw{ stringf }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | use Perl::ToPerl6::Exception::Fatal::TransformerDefinition | ||||||||||||||||||||
| 15 | 23 | ▶ | qw{ throw_transformer_definition }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 23 | ▶ | use Perl::ToPerl6::TransformerParameter::Behavior; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 23 | ▶ | use Perl::ToPerl6::TransformerParameter::Behavior::Boolean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 23 | ▶ | use Perl::ToPerl6::TransformerParameter::Behavior::Enumeration; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 23 | ▶ | use Perl::ToPerl6::TransformerParameter::Behavior::Integer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 23 | ▶ | use Perl::ToPerl6::TransformerParameter::Behavior::String; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 23 | ▶ | use Perl::ToPerl6::TransformerParameter::Behavior::StringList; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 23 | ▶ | use Perl::ToPerl6::Utils qw{ :characters &interpolate }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 23 | ▶ | use Perl::ToPerl6::Utils::DataConversion qw{ &defined_or_empty }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 23 | Readonly::Scalar our $NO_DESCRIPTION_AVAILABLE => 'No description available.'; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Grrr... one of the OO limitations of Perl: you can't put references to | ||||||||||||||||||||
| 31 | # subclases in a superclass (well, not nicely). This map and method belong | ||||||||||||||||||||
| 32 | # in Behavior.pm. | ||||||||||||||||||||
| 33 | 23 | Readonly::Hash my %BEHAVIORS => | |||||||||||||||||||
| 34 | ( | ||||||||||||||||||||
| 35 | 'boolean' => Perl::ToPerl6::TransformerParameter::Behavior::Boolean->new(), | ||||||||||||||||||||
| 36 | 'enumeration' => Perl::ToPerl6::TransformerParameter::Behavior::Enumeration->new(), | ||||||||||||||||||||
| 37 | 'integer' => Perl::ToPerl6::TransformerParameter::Behavior::Integer->new(), | ||||||||||||||||||||
| 38 | 'string' => Perl::ToPerl6::TransformerParameter::Behavior::String->new(), | ||||||||||||||||||||
| 39 | 'string list' => Perl::ToPerl6::TransformerParameter::Behavior::StringList->new(), | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 59 | ▶ | sub _get_behavior_for_name { | ||||||||||||||||||
|
Subroutine
_get_behavior_for_name: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 43 | 59 | my $behavior_name = shift; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 59 | ▶ | my $behavior = $BEHAVIORS{$behavior_name} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | or throw_transformer_definition( qq{There's no "$behavior_name" behavior.} ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 59 | return $behavior; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 59 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.9
Pod
undocumented
| |||||||||||||||||||||
| 54 | 59 | my ($class, $specification) = @_; | |||||||||||||||||||
| 55 | 59 | my $self = bless {}, $class; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 59 | ▶ | defined $specification | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | or throw_transformer_definition( | ||||||||||||||||||||
| 59 | 'Attempt to create a ', __PACKAGE__, ' without a specification.' ); | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 59 | my $behavior_specification; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 59 | my $specification_type = ref $specification; | |||||||||||||||||||
| 64 | 59 | ▶ | if ( not $specification_type ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 0 | $self->{_name} = $specification; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | $behavior_specification = {}; | |||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||
| 69 | 59 | ▶ | $specification_type eq 'HASH' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | or throw_transformer_definition( | ||||||||||||||||||||
| 71 | 'Attempt to create a ', | ||||||||||||||||||||
| 72 | __PACKAGE__, | ||||||||||||||||||||
| 73 | " with a $specification_type as a specification.", | ||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | defined $specification->{name} | ||||||||||||||||||||
| 77 | 59 | ▶ | or throw_transformer_definition( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 'Attempt to create a ', __PACKAGE__, ' without a name.' ); | ||||||||||||||||||||
| 79 | 59 | $self->{_name} = $specification->{name}; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 59 | $behavior_specification = $specification; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 59 | $self->_initialize_from_behavior($behavior_specification); | |||||||||||||||||||
| 85 | 55 | $self->_finish_standard_initialization($behavior_specification); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 55 | return $self; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # See if the specification includes a Behavior name, and if so, let the | ||||||||||||||||||||
| 91 | # Behavior with that name plug in its implementations of parser, etc. | ||||||||||||||||||||
| 92 | 59 | ▶ | sub _initialize_from_behavior { | ||||||||||||||||||
|
Subroutine
_initialize_from_behavior: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 93 | 59 | my ($self, $specification) = @_; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 59 | my $behavior_name = $specification->{behavior}; | |||||||||||||||||||
| 96 | 59 | my $behavior; | |||||||||||||||||||
| 97 | 59 | ▶ | if ($behavior_name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 59 | $behavior = _get_behavior_for_name($behavior_name); | |||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||
| 100 | 0 | $behavior = _get_behavior_for_name('string'); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 59 | $self->{_behavior} = $behavior; | |||||||||||||||||||
| 104 | 59 | $self->{_behavior_values} = {}; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 59 | $behavior->initialize_parameter($self, $specification); | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 55 | return; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | # Grab the rest of the values out of the specification, including overrides | ||||||||||||||||||||
| 112 | # of what the Behavior specified. | ||||||||||||||||||||
| 113 | 55 | ▶ | sub _finish_standard_initialization { | ||||||||||||||||||
|
Subroutine
_finish_standard_initialization: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 114 | 55 | my ($self, $specification) = @_; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | my $description = | ||||||||||||||||||||
| 117 | 55 | ▶ | $specification->{description} || $NO_DESCRIPTION_AVAILABLE; | ||||||||||||||||||
|
Condition33%1/3
$specification->{'description'} || $Perl::ToPerl6::TransformerParameter::NO_DESCRIPTION_AVAILABLE
MC/DC0%0/2
$specification->{'description'} || $Perl::ToPerl6::TransformerParameter::NO_DESCRIPTION_AVAILABLE
$specification->{'description'}
$Perl::ToPerl6::TransformerParameter::NO_DESCRIPTION_AVAILABLE
Truth table33%1/3
A || B
A= $specification->{'description'}B= $Perl::ToPerl6::TransformerParameter::NO_DESCRIPTION_AVAILABLE
| |||||||||||||||||||||
| 118 | 55 | $self->_set_description($description); | |||||||||||||||||||
| 119 | 55 | $self->_set_default_string($specification->{default_string}); | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 55 | $self->_set_parser($specification->{parser}); | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 55 | return; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 233 | ▶ | sub get_name { | ||||||||||||||||||
|
Subroutine
get_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 129 | 233 | my $self = shift; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 233 | return $self->{_name}; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 6 | ▶ | sub get_description { | ||||||||||||||||||
|
Subroutine
get_description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 137 | 6 | my $self = shift; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 6 | return $self->{_description}; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 55 | ▶ | sub _set_description { | ||||||||||||||||||
|
Subroutine
_set_description: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 143 | 55 | my ($self, $new_value) = @_; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 55 | ▶ | return if not defined $new_value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 55 | $self->{_description} = $new_value; | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 55 | return; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 5 | ▶ | sub _get_description_with_trailing_period { | ||||||||||||||||||
|
Subroutine
_get_description_with_trailing_period: called CC=3 SCAR=11.6
| |||||||||||||||||||||
| 152 | 5 | my $self = shift; | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 5 | my $description = $self->get_description(); | |||||||||||||||||||
| 155 | 5 | ▶ | if ($description) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | 5 | ▶ | if ( $PERIOD ne substr $description, ( length $description ) - 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 5 | $description .= $PERIOD; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||
| 160 | 0 | $description = $EMPTY; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 5 | return $description; | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 99 | ▶ | sub get_default_string { | ||||||||||||||||||
|
Subroutine
get_default_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 169 | 99 | my $self = shift; | |||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 99 | return $self->{_default_string}; | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 55 | ▶ | sub _set_default_string { | ||||||||||||||||||
|
Subroutine
_set_default_string: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 175 | 55 | my ($self, $new_value) = @_; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 55 | ▶ | return if not defined $new_value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | 49 | $self->{_default_string} = $new_value; | |||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 49 | return; | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 5 | ▶ | sub _get_behavior { | ||||||||||||||||||
|
Subroutine
_get_behavior: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 186 | 5 | my $self = shift; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 5 | return $self->{_behavior}; | |||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 17 | ▶ | sub _get_behavior_values { | ||||||||||||||||||
|
Subroutine
_get_behavior_values: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 192 | 17 | my $self = shift; | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 17 | return $self->{_behavior_values}; | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 93 | ▶ | sub _get_parser { | ||||||||||||||||||
|
Subroutine
_get_parser: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 200 | 93 | my $self = shift; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 93 | return $self->{_parser}; | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 110 | ▶ | sub _set_parser { | ||||||||||||||||||
|
Subroutine
_set_parser: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 206 | 110 | my ($self, $new_value) = @_; | |||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 110 | ▶ | return if not defined $new_value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 209 | 55 | $self->{_parser} = $new_value; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 55 | return; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 93 | ▶ | sub parse_and_validate_config_value { | ||||||||||||||||||
|
Subroutine
parse_and_validate_config_value: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 217 | 93 | my ($self, $transformer, $config) = @_; | |||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 93 | my $config_string = $config->{$self->get_name()}; | |||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 93 | my $parser = $self->_get_parser(); | |||||||||||||||||||
| 222 | 93 | ▶ | if ($parser) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | 93 | $parser->($transformer, $self, $config_string); | |||||||||||||||||||
| 224 | } | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | 86 | return; | |||||||||||||||||||
| 227 | } | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 5 | ▶ | sub generate_full_description { | ||||||||||||||||||
|
Subroutine
generate_full_description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 232 | 5 | my ($self) = @_; | |||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | 5 | return $self->_get_behavior()->generate_parameter_description($self); | |||||||||||||||||||
| 235 | } | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 5 | ▶ | sub _generate_full_description { | ||||||||||||||||||
|
Subroutine
_generate_full_description: called CC=3 SCAR=11.6
| |||||||||||||||||||||
| 240 | 5 | my ($self, $prefix) = @_; | |||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | 5 | my $description = $self->generate_full_description(); | |||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 5 | ▶ | if (not $description) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 245 | 0 | return $EMPTY; | |||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | 5 | ▶ | if ($prefix) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 249 | 5 | $description =~ s/ ^ /$prefix/xmsg; | |||||||||||||||||||
| 250 | } | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 5 | return $description; | |||||||||||||||||||
| 253 | } | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | 5 | ▶ | sub to_formatted_string { | ||||||||||||||||||
|
Subroutine
to_formatted_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 258 | 5 | my ($self, $format) = @_; | |||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | my %specification = ( | ||||||||||||||||||||
| 261 | 5 | ▶ | n => sub { $self->get_name() }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 262 | 0 | ▶ | d => sub { defined_or_empty( $self->get_description() ) }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 263 | 5 | ▶ | D => sub { defined_or_empty( $self->get_default_string() ) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 264 | 5 | ▶ | f => sub { $self->_generate_full_description(@_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 265 | 5 | ); | |||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 5 | return stringf( interpolate($format), %specification ); | |||||||||||||||||||
| 268 | } | ||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | 23 | 1; | |||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | __END__ | ||||||||||||||||||||