iteraptor 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/iteraptor/version.rb +1 -1
- data/lib/iteraptor.rb +34 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd67b42a75a6ed56774d77033653de940cfb1093
|
4
|
+
data.tar.gz: 05ca53034c8fce538aecd7e74e4826eae4154285
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fc5336e833688e059b0b28459e55580a7158e0261dc7bea381dc965845d0831d7ff58f7ffb7ed923084091be3da8e8e9bdda8304060e797876bb195e87588ca
|
7
|
+
data.tar.gz: 48ed7af274e24d80022b600853b7efa60083af54052343eb60e4b388cbb6ab876477cf80475c1226e0f0c4c6268024edc92dbfd8b8340ce462d4ff356a2c3be0
|
data/lib/iteraptor/version.rb
CHANGED
data/lib/iteraptor.rb
CHANGED
@@ -3,7 +3,6 @@ require "iteraptor/version"
|
|
3
3
|
# rubocop:disable Style/VariableNumber
|
4
4
|
# rubocop:disable Metrics/ModuleLength
|
5
5
|
module Iteraptor
|
6
|
-
DELIMITER = '.'.freeze
|
7
6
|
|
8
7
|
def self.included base
|
9
8
|
raise "This module might be included into Enumerables only" unless base.ancestors.include? Enumerable
|
@@ -14,7 +13,7 @@ module Iteraptor
|
|
14
13
|
return enum_for(m, key, value, **params) unless λ
|
15
14
|
return self if empty?
|
16
15
|
|
17
|
-
send_to = enumerable_parent?
|
16
|
+
send_to = H.enumerable_parent?(self)
|
18
17
|
send_to && send("#{m}_in_#{send_to.name.downcase}", key || self, value, **params, &λ)
|
19
18
|
end
|
20
19
|
end
|
@@ -36,7 +35,7 @@ module Iteraptor
|
|
36
35
|
def aplanar **params
|
37
36
|
return self if empty?
|
38
37
|
cada(**params).with_object({}) do |(key, value), acc|
|
39
|
-
key = key.join(iteraptor_delimiter(params)) if params[:full_parent]
|
38
|
+
key = key.join(H.iteraptor_delimiter(params)) if params[:full_parent]
|
40
39
|
key = key.to_sym if params[:symbolize_keys]
|
41
40
|
acc[key] = value unless value.is_a?(Enumerable)
|
42
41
|
yield key, value if block_given?
|
@@ -49,7 +48,7 @@ module Iteraptor
|
|
49
48
|
aplanar(**params).each_with_object(
|
50
49
|
Hash.new { |h, k| h[k] = h.clone.clear }
|
51
50
|
) do |(k, v), acc|
|
52
|
-
keys = k.to_s.split(iteraptor_delimiter(params))
|
51
|
+
keys = k.to_s.split(H.iteraptor_delimiter(params))
|
53
52
|
parent = keys[0..-2].reduce(acc){ |h, kk| h[kk] }
|
54
53
|
parent[keys.last] = v
|
55
54
|
end.mapa(yield_all: true, **params) do |_parent, (k, v)|
|
@@ -63,7 +62,7 @@ module Iteraptor
|
|
63
62
|
return self if empty?
|
64
63
|
|
65
64
|
cada(**params).with_object([]) do |(key, value), acc|
|
66
|
-
key = key.join(iteraptor_delimiter(params)) if params[:full_parent]
|
65
|
+
key = key.join(H.iteraptor_delimiter(params)) if params[:full_parent]
|
67
66
|
key = key.to_sym if params[:symbolize_keys]
|
68
67
|
acc << yield(key, value) unless value.is_a?(Enumerable)
|
69
68
|
end
|
@@ -92,8 +91,8 @@ module Iteraptor
|
|
92
91
|
def cada_in_array_or_hash in_array, root = nil, parent = nil, **params, &λ
|
93
92
|
(in_array ? each_with_index : each).each do |k, v|
|
94
93
|
k, v = v, k if in_array
|
95
|
-
result =
|
96
|
-
result = result.join(iteraptor_delimiter(params)) unless params[:full_parent]
|
94
|
+
result = H.push_flatten_compact(parent, k)
|
95
|
+
result = result.join(H.iteraptor_delimiter(params)) unless params[:full_parent]
|
97
96
|
result.tap do |p|
|
98
97
|
yield p, v
|
99
98
|
CADA_PROC.call(v, root, p, **params, &λ)
|
@@ -110,11 +109,11 @@ module Iteraptor
|
|
110
109
|
# - symbolize_keys
|
111
110
|
def mapa_in_array root = nil, parent = nil, **params, &λ
|
112
111
|
map.with_index do |e, idx|
|
113
|
-
p =
|
114
|
-
p = p.join(iteraptor_delimiter(params)) unless params[:full_parent]
|
112
|
+
p = H.push_flatten_compact(parent, idx)
|
113
|
+
p = p.join(H.iteraptor_delimiter(params)) unless params[:full_parent]
|
115
114
|
|
116
115
|
yielded =
|
117
|
-
if !enumerable_parent?(e) || params[:yield_all]
|
116
|
+
if !H.enumerable_parent?(e) || params[:yield_all]
|
118
117
|
yield p, (params[:with_index] ? [idx.to_s, e] : e)
|
119
118
|
else
|
120
119
|
e
|
@@ -138,8 +137,8 @@ module Iteraptor
|
|
138
137
|
# - symbolize_keys
|
139
138
|
def mapa_in_hash root = nil, parent = nil, **params, &λ
|
140
139
|
map do |k, v|
|
141
|
-
p =
|
142
|
-
p = p.join(iteraptor_delimiter(params)) unless params[:full_parent]
|
140
|
+
p = H.push_flatten_compact(parent, k)
|
141
|
+
p = p.join(H.iteraptor_delimiter(params)) unless params[:full_parent]
|
143
142
|
|
144
143
|
k, v = yield p, [k, v] if !v.is_a?(Enumerable) || params[:yield_all]
|
145
144
|
|
@@ -153,9 +152,6 @@ module Iteraptor
|
|
153
152
|
|
154
153
|
##############################################################################
|
155
154
|
### helpers
|
156
|
-
def safe_symbolize key
|
157
|
-
key.respond_to?(:to_sym) ? key.to_sym : key
|
158
|
-
end
|
159
155
|
|
160
156
|
def to_hash_or_array **params
|
161
157
|
# rubocop:disable Style/MultilineTernaryOperator
|
@@ -170,7 +166,7 @@ module Iteraptor
|
|
170
166
|
return receiver.values if receiver.keys.each_with_index.all? { |key, idx| key == idx.to_s }
|
171
167
|
return receiver unless params[:symbolize_keys]
|
172
168
|
|
173
|
-
receiver.map { |k, v| [safe_symbolize(k), v] }.to_h
|
169
|
+
receiver.map { |k, v| [H.safe_symbolize(k), v] }.to_h
|
174
170
|
# rubocop:enable Style/MultilineTernaryOperator
|
175
171
|
end
|
176
172
|
|
@@ -186,7 +182,7 @@ module Iteraptor
|
|
186
182
|
|
187
183
|
plough = method ? :none? : :any?
|
188
184
|
aplanar(**params).each_with_object({}) do |(key, value), acc|
|
189
|
-
to_match = key.to_s.split(iteraptor_delimiter(params)) unless params[:full_parent]
|
185
|
+
to_match = key.to_s.split(H.iteraptor_delimiter(params)) unless params[:full_parent]
|
190
186
|
to_match = to_match.flat_map { |k| [k.to_s, k.to_s.to_sym] } if params[:soft_keys]
|
191
187
|
|
192
188
|
next if filter.public_send(plough, &->(f){ to_match.any?(&f.method(:===)) })
|
@@ -198,14 +194,27 @@ module Iteraptor
|
|
198
194
|
|
199
195
|
##############################################################################
|
200
196
|
### helpers
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
197
|
+
module H
|
198
|
+
class << self
|
199
|
+
DELIMITER = '.'.freeze
|
200
|
+
|
201
|
+
def safe_symbolize key
|
202
|
+
key.respond_to?(:to_sym) ? key.to_sym : key
|
203
|
+
end
|
204
|
+
def iteraptor_delimiter(params)
|
205
|
+
params[:delimiter] || DELIMITER
|
206
|
+
end
|
207
|
+
def push_flatten_compact array, tail
|
208
|
+
case array
|
209
|
+
when NilClass then [tail]
|
210
|
+
when Array then array + [tail]
|
211
|
+
else [array, tail]
|
212
|
+
end.compact
|
213
|
+
end
|
214
|
+
def enumerable_parent?(receiver)
|
215
|
+
[Hash, Array, Enumerable].detect(&receiver.method(:is_a?))
|
216
|
+
end
|
217
|
+
end
|
209
218
|
end
|
210
219
|
end
|
211
220
|
|