| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Config::Neat::Array - Class for array-like config nodes | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com> | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | L<https://github.com/iafan/Config-Neat> | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package Config::Neat::Array; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | our $VERSION = '1.4'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | no warnings qw(uninitialized); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use Config::Neat::Util qw(is_any_array is_neat_array); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||||||||||
| 26 | 789 | ▶ | my ($class, $self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 789 | ▶ | $self = [] unless defined $self && ref($self) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self and ref $self eq "ARRAY"
MC/DC50%1/2
defined $self and ref $self eq "ARRAY"
defined $self
ref $self eq "ARRAY"
Truth table66%2/3
A and B
A= defined $selfB= ref $self eq "ARRAY"
| |||||||||||||||||||||||||||||
| 28 | 789 | bless $self, $class; | |||||||||||||||||||||||||||
| 29 | 789 | return $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub push { | ||||||||||||||||||||||||||||
| 33 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
push: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 18 | push @$self, @_; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # return a flattened one-dimensional array, where nested | ||||||||||||||||||||||||||||
| 38 | # Config::Neat arrays are expanded recursively | ||||||||||||||||||||||||||||
| 39 | sub as_flat_array { | ||||||||||||||||||||||||||||
| 40 | 215 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_flat_array: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # fist check if conversion will be needed | ||||||||||||||||||||||||||||
| 43 | 215 | my $need_conversion; | |||||||||||||||||||||||||||
| 44 | 215 | foreach my $val (@$self) { | |||||||||||||||||||||||||||
| 45 | 283 | ▶ | if (is_neat_array($val)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 10 | $need_conversion = 1; | |||||||||||||||||||||||||||
| 47 | 10 | last; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 215 | ▶ | return $self unless $need_conversion; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # flatten the array recursively | ||||||||||||||||||||||||||||
| 53 | 10 | my $result = Config::Neat::Array->new(); | |||||||||||||||||||||||||||
| 54 | 10 | foreach my $val (@$self) { | |||||||||||||||||||||||||||
| 55 | 28 | ▶ | if (is_neat_array($val)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 28 | $val = $val->as_flat_array; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 28 | ▶ | if (is_any_array($val)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | # expand arrays | ||||||||||||||||||||||||||||
| 61 | 28 | push @$result, @$val; | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | #push scalars and hashes as is | ||||||||||||||||||||||||||||
| 64 | 0 | push @$result, $val; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 10 | return $result; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Given ['foo', 'bar', 'baz'] as the contents of the array, returns 'foo bar baz' string. | ||||||||||||||||||||||||||||
| 71 | # Array is flattened before being converted into a string. | ||||||||||||||||||||||||||||
| 72 | # If string starts from a newline and the next line is indented, remove that amount of spaces | ||||||||||||||||||||||||||||
| 73 | # from each line and trim leading and trailing newline | ||||||||||||||||||||||||||||
| 74 | sub as_string { | ||||||||||||||||||||||||||||
| 75 | 159 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=7 SCAR=31.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 159 | my $val = join(' ', @{$self->as_flat_array}); | |||||||||||||||||||||||||||
| 78 | 159 | my $indent = undef; | |||||||||||||||||||||||||||
| 79 | 159 | ▶ | while ($val =~ m/\n(\s+)/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $len = length($1); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | $indent = $len unless defined $indent and $len > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $indent and $len > 0
MC/DC0%0/2
defined $indent and $len > 0
defined $indent
$len > 0
Truth table0%0/3
A and B
A= defined $indentB= $len > 0
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $indent = $len if $len > 0 and $indent > $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$len > 0 and $indent > $len
MC/DC0%0/2
$len > 0 and $indent > $len
$len > 0
$indent > $len
Truth table0%0/3
A and B
A= $len > 0B= $indent > $len
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 159 | ▶ | if ($indent > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $indent = ' ' x $indent; | |||||||||||||||||||||||||||
| 86 | 0 | $val =~ s/\n$indent/\n/sg; | |||||||||||||||||||||||||||
| 87 | 0 | $val =~ s/^\s*\n//s; # remove first single newline and preceeding whitespace | |||||||||||||||||||||||||||
| 88 | 0 | $val =~ s/\n\s*$//s; # remove last single newline and whitespace after it | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 159 | return $val; | |||||||||||||||||||||||||||
| 91 | } # end sub | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Returns true if the string representation of the array | ||||||||||||||||||||||||||||
| 94 | # evaluates case-insensitively to a known list of positive boolean strings | ||||||||||||||||||||||||||||
| 95 | sub as_boolean { | ||||||||||||||||||||||||||||
| 96 | 21 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_boolean: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 21 | return ($self->as_string =~ m/^(YES|Y|ON|TRUE|1)$/i); | |||||||||||||||||||||||||||
| 99 | } # end sub | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Returns true if the string representation of the array | ||||||||||||||||||||||||||||
| 102 | # evaluates case-insensitively to a known list of positive or negative boolean strings | ||||||||||||||||||||||||||||
| 103 | sub is_boolean { | ||||||||||||||||||||||||||||
| 104 | 11 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_boolean: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 11 | return ($self->as_string =~ m/^(YES|NO|Y|N|ON|OFF|TRUE|FALSE|1|0)$/i); | |||||||||||||||||||||||||||
| 107 | } # end sub | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Given ['foo', 'bar', 'baz'] as the contents of the array, | ||||||||||||||||||||||||||||
| 110 | # and property name 'x', returns the following hash reference: | ||||||||||||||||||||||||||||
| 111 | # { | ||||||||||||||||||||||||||||
| 112 | # 0 => {'x' => 'foo'}, | ||||||||||||||||||||||||||||
| 113 | # 1 => {'x' => 'bar'}, | ||||||||||||||||||||||||||||
| 114 | # 2 => {'x' => 'baz'} | ||||||||||||||||||||||||||||
| 115 | # } | ||||||||||||||||||||||||||||
| 116 | sub as_hash { | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my ($self, $propname) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_hash: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | die "Second parameter (propname) not provided" unless defined $propname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $result = {}; | |||||||||||||||||||||||||||
| 122 | 0 | tie(%$result, 'Tie::IxHash'); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $n = 0; | |||||||||||||||||||||||||||
| 125 | 0 | foreach my $val (@$self) { | |||||||||||||||||||||||||||
| 126 | 0 | $result->{$n++} = {$propname => $val}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | return $result; | |||||||||||||||||||||||||||
| 130 | } # end sub | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | 1; | |||||||||||||||||||||||||||