1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
package Catmandu::Fix::string;
use Catmandu::Sane;
our $VERSION = '1.2024';
use List::Util qw(all);
use Catmandu::Util qw(is_string is_value is_array_ref is_hash_ref);
use Catmandu::Util::Path qw(as_path);
use Moo;
use namespace::clean;
use Catmandu::Fix::Has;
with 'Catmandu::Fix::Builder';
has path => (fix_arg => 1);
sub _build_fixer {
my ($self) = @_;
as_path($self->path)->updater(
sub {
my $val = $_[0];
if (is_string($val)) {
"${val}";
}
elsif (is_array_ref($val) && all {is_value($_)} @$val) {
join('', @$val);
}
elsif (is_hash_ref($val) && all {is_value($_)} values %$val) {
join('', map {$val->{$_}} sort keys %$val);
}
else {
'';
}
}
);
}
1;
__END__
=pod
=head1 NAME
Catmandu::Fix::string - convert a value to a string
=head1 SYNOPSIS
# year => 2016
string(year)
# year => "2016"
# foo => ["a", "b", "c"]
string(foo)
# foo => "abc"
# foo => ["a", {b => "c"}, "d"]
string(foo)
# foo => ""
# foo => {2 => "b", 1 => "a"}
string(foo)
# foo => "ab"
# foo => {a => ["b"]}
string(foo)
# foo => ""
=head1 SEE ALSO
L<Catmandu::Fix>
=cut
|