line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Collection::Role::Transform; |
2
|
5
|
|
|
5
|
|
5133
|
use Mojo::Base -role; |
|
5
|
|
|
|
|
16
|
|
|
5
|
|
|
|
|
41
|
|
3
|
5
|
|
|
5
|
|
2391
|
use Carp (); |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
6737
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
requires 'reduce'; |
8
|
|
|
|
|
|
|
|
9
|
18
|
|
|
18
|
1
|
16744
|
sub hashify { _reduce(shift, \&_multi_key_hash_assign, {}, @_) } |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub hashify_collect { |
12
|
57
|
|
|
57
|
1
|
105338
|
my $self = shift; |
13
|
|
|
|
|
|
|
|
14
|
57
|
100
|
100
|
|
|
240
|
if (ref $_[0] eq 'HASH' and _parse_flatten_option(shift)) { |
15
|
7
|
|
|
|
|
23
|
return _reduce($self, \&_multi_key_hash_collect_and_flatten, {}, @_) |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
48
|
|
|
|
|
149
|
return _reduce($self, \&_multi_key_hash_collect, {}, @_); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub collect_by { |
22
|
57
|
|
|
57
|
1
|
104873
|
my $self = shift; |
23
|
|
|
|
|
|
|
|
24
|
57
|
100
|
100
|
|
|
282
|
if (ref $_[0] eq 'HASH' and _parse_flatten_option(shift)) { |
25
|
7
|
|
|
|
|
31
|
return _reduce($self, \&_collect_by_and_flatten, [Mojo::Collection->new, {}], @_)->[0]; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
48
|
|
|
|
|
199
|
return _reduce($self, \&_collect_by, [Mojo::Collection->new, {}], @_)->[0]; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _multi_key_hash_assign { |
32
|
23
|
|
|
23
|
|
73
|
my ($hash, $keys, $value, @extra_values) = @_; |
33
|
23
|
100
|
|
|
|
75
|
Carp::confess 'multiple values returned from get_value sub when one is expected' if @extra_values; |
34
|
|
|
|
|
|
|
|
35
|
21
|
|
|
|
|
43
|
_create_leading_key_hashes($hash, $keys)->{$keys->[-1]} = $value; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _multi_key_hash_collect { |
39
|
105
|
|
|
105
|
|
381
|
my ($hash, $keys, @values) = @_; |
40
|
|
|
|
|
|
|
|
41
|
105
|
|
66
|
|
|
144
|
push @{ _create_leading_key_hashes($hash, $keys)->{$keys->[-1]} ||= Mojo::Collection->new }, @values; |
|
105
|
|
|
|
|
198
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _multi_key_hash_collect_and_flatten { |
45
|
18
|
|
|
18
|
|
96
|
my ($hash, $keys, @values) = @_; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
push |
48
|
18
|
|
66
|
|
|
29
|
@{ _create_leading_key_hashes($hash, $keys)->{$keys->[-1]} ||= Mojo::Collection->new }, |
|
18
|
|
|
|
|
33
|
|
49
|
|
|
|
|
|
|
_flatten(@values); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _collect_by { |
53
|
105
|
|
|
105
|
|
311
|
my ($collection, $hash, $keys, @values) = (@{+shift}, @_); |
|
105
|
|
|
|
|
232
|
|
54
|
|
|
|
|
|
|
|
55
|
105
|
|
|
|
|
202
|
my $leading_hash = _create_leading_key_hashes($hash, $keys); |
56
|
105
|
100
|
|
|
|
268
|
unless (exists $leading_hash->{$keys->[-1]}) { |
57
|
89
|
|
|
|
|
226
|
push @$collection, $leading_hash->{$keys->[-1]} = Mojo::Collection->new; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
105
|
|
|
|
|
582
|
push @{ $leading_hash->{$keys->[-1]} }, @values; |
|
105
|
|
|
|
|
258
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _collect_by_and_flatten { |
64
|
18
|
|
|
18
|
|
97
|
my ($collection, $hash, $keys, @values) = (@{+shift}, @_); |
|
18
|
|
|
|
|
42
|
|
65
|
|
|
|
|
|
|
|
66
|
18
|
|
|
|
|
36
|
my $leading_hash = _create_leading_key_hashes($hash, $keys); |
67
|
18
|
100
|
|
|
|
47
|
unless (exists $leading_hash->{$keys->[-1]}) { |
68
|
11
|
|
|
|
|
29
|
push @$collection, $leading_hash->{$keys->[-1]} = Mojo::Collection->new; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
18
|
|
|
|
|
78
|
push @{ $leading_hash->{$keys->[-1]} }, _flatten(@values); |
|
18
|
|
|
|
|
40
|
|
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _create_leading_key_hashes { |
75
|
267
|
|
|
267
|
|
457
|
my ($hash, $keys) = @_; |
76
|
|
|
|
|
|
|
|
77
|
267
|
|
|
|
|
394
|
my $cur_hash = $hash; |
78
|
267
|
|
|
|
|
646
|
for my $key (@$keys[0..$#$keys - 1]) { |
79
|
123
|
|
100
|
|
|
635
|
$cur_hash = $cur_hash->{$key} ||= {}; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
267
|
|
|
|
|
842
|
return $cur_hash; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _reduce { |
86
|
128
|
|
|
128
|
|
564
|
my ($self, $apply_key_and_value, $initial) = (shift, shift, shift); |
87
|
|
|
|
|
|
|
|
88
|
128
|
100
|
|
|
|
303
|
unless (@_) { |
89
|
3
|
|
|
|
|
36
|
Carp::croak 'must provide get_keys sub'; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
125
|
|
|
|
|
188
|
my $get_keys = shift; |
93
|
125
|
|
100
|
|
|
324
|
my $get_keys_ref = ref $get_keys || 'scalar value'; |
94
|
125
|
100
|
|
|
|
367
|
Carp::croak qq{get_keys sub must be a subroutine, but was '$get_keys_ref'} unless $get_keys_ref eq 'CODE'; |
95
|
|
|
|
|
|
|
|
96
|
116
|
|
|
|
|
172
|
my $get_value; |
97
|
116
|
100
|
|
|
|
254
|
if (@_) { |
98
|
58
|
|
|
|
|
123
|
$get_value = shift; |
99
|
58
|
|
100
|
|
|
143
|
my $get_value_ref = ref $get_value || 'scalar value'; |
100
|
58
|
100
|
|
|
|
215
|
Carp::croak qq{get_value must be a subroutine if provided, but was '$get_value_ref'} if $get_value_ref ne 'CODE'; |
101
|
|
|
|
|
|
|
} else { |
102
|
58
|
|
|
135
|
|
167
|
$get_value = sub { $_ }; |
|
135
|
|
|
|
|
607
|
|
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
return $self->reduce(sub { |
106
|
269
|
|
|
269
|
|
1247
|
local $_ = $b; |
107
|
269
|
|
|
|
|
657
|
$apply_key_and_value->($a, [$get_keys->($b)], $get_value->($b)); |
108
|
|
|
|
|
|
|
|
109
|
267
|
|
|
|
|
1429
|
$a; |
110
|
107
|
|
|
|
|
508
|
}, $initial); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
92
|
100
|
|
92
|
|
207
|
sub _flatten { map { ref($_) ? _flatten(@$_) : $_ } @_ } |
|
100
|
|
|
|
|
277
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _parse_flatten_option { |
116
|
94
|
|
|
94
|
|
200
|
my ($options) = @_; |
117
|
94
|
100
|
|
|
|
378
|
return unless %$options; |
118
|
|
|
|
|
|
|
|
119
|
30
|
100
|
|
|
|
119
|
Carp::confess 'only one option can be provided' if keys %$options > 1; |
120
|
|
|
|
|
|
|
|
121
|
28
|
|
|
|
|
64
|
my $flatten = delete $options->{flatten}; |
122
|
|
|
|
|
|
|
|
123
|
28
|
100
|
|
|
|
88
|
Carp::confess 'unknown options provided: ' . Mojo::Util::dumper $options if %$options; |
124
|
|
|
|
|
|
|
|
125
|
26
|
|
|
|
|
96
|
return $flatten; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; |
129
|
|
|
|
|
|
|
__END__ |