| 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 | our $VERSION = '1.4'; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | |||
| 21 | no warnings qw(uninitialized); | ||
| 22 | |||
| 23 | use Config::Neat::Util qw(is_any_array is_neat_array); | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my ($class, $self) = @_; | ||
| 27 | $self = [] unless defined $self && ref($self) eq 'ARRAY'; | ||
| 28 | bless $self, $class; | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub push { | ||
| 33 | my $self = shift; | ||
| 34 | 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 | my ($self) = @_; | ||
| 41 | |||
| 42 | # fist check if conversion will be needed | ||
| 43 | my $need_conversion; | ||
| 44 | foreach my $val (@$self) { | ||
| 45 | if (is_neat_array($val)) { | ||
| 46 | $need_conversion = 1; | ||
| 47 | last; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | return $self unless $need_conversion; | ||
| 51 | |||
| 52 | # flatten the array recursively | ||
| 53 | my $result = Config::Neat::Array->new(); | ||
| 54 | foreach my $val (@$self) { | ||
| 55 | if (is_neat_array($val)) { | ||
| 56 | $val = $val->as_flat_array; | ||
| 57 | } | ||
| 58 | |||
| 59 | if (is_any_array($val)) { | ||
| 60 | # expand arrays | ||
| 61 | push @$result, @$val; | ||
| 62 | } else { | ||
| 63 | #push scalars and hashes as is | ||
| 64 | push @$result, $val; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | 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 | my ($self) = @_; | ||
| 76 | |||
| 77 | my $val = join(' ', @{$self->as_flat_array}); | ||
| 78 | my $indent = undef; | ||
| 79 | while ($val =~ m/\n(\s+)/g) { | ||
| 80 | my $len = length($1); | ||
| 81 | $indent = $len unless defined $indent and $len > 0; | ||
| 82 | $indent = $len if $len > 0 and $indent > $len; | ||
| 83 | } | ||
| 84 | if ($indent > 0) { | ||
| 85 | $indent = ' ' x $indent; | ||
| 86 | $val =~ s/\n$indent/\n/sg; | ||
| 87 | $val =~ s/^\s*\n//s; # remove first single newline and preceeding whitespace | ||
| 88 | $val =~ s/\n\s*$//s; # remove last single newline and whitespace after it | ||
| 89 | } | ||
| 90 | 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 | my ($self) = @_; | ||
| 97 | |||
| 98 | 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 | my ($self) = @_; | ||
| 105 | |||
| 106 | 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 | my ($self, $propname) = @_; | ||
| 118 | |||
| 119 | die "Second parameter (propname) not provided" unless defined $propname; | ||
| 120 | |||
| 121 | my $result = {}; | ||
| 122 | tie(%$result, 'Tie::IxHash'); | ||
| 123 | |||
| 124 | my $n = 0; | ||
| 125 | foreach my $val (@$self) { | ||
| 126 | $result->{$n++} = {$propname => $val}; | ||
| 127 | } | ||
| 128 | |||
| 129 | return $result; | ||
| 130 | } # end sub | ||
| 131 | |||
| 132 | 1; |