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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51a190bd902813b3f2831501f2e2c8c8fd9cf555
4
- data.tar.gz: fc10ba4bb1997340931716bea23d10dc8b8f0587
3
+ metadata.gz: cd67b42a75a6ed56774d77033653de940cfb1093
4
+ data.tar.gz: 05ca53034c8fce538aecd7e74e4826eae4154285
5
5
  SHA512:
6
- metadata.gz: 7804c4da0e00f7077fca99af258e0bec1cfee7c4170926682492b8cda5d76192485bc3ee7e4d945dd55dced7e7ee77ba979a0cc608598333d6e7474450020876
7
- data.tar.gz: 8afc7e6932c32412e97b1f95fe84d413a89dd4d9fdc174809e2a2cf7574c721434b5907b3a2186d1efeae51a1835a849668faa9ad01417650a9169561cc2d0af
6
+ metadata.gz: 9fc5336e833688e059b0b28459e55580a7158e0261dc7bea381dc965845d0831d7ff58f7ffb7ed923084091be3da8e8e9bdda8304060e797876bb195e87588ca
7
+ data.tar.gz: 48ed7af274e24d80022b600853b7efa60083af54052343eb60e4b388cbb6ab876477cf80475c1226e0f0c4c6268024edc92dbfd8b8340ce462d4ff356a2c3be0
@@ -1,3 +1,3 @@
1
1
  module Iteraptor
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
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 = [parent, k].flatten.compact
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 = [parent, idx].flatten.compact
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 = [parent, k].flatten.compact
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
- def enumerable_parent?(receiver = self)
202
- [Hash, Array, Enumerable].detect(&receiver.method(:is_a?))
203
- end
204
- def iteraptor_delimiter(params)
205
- params[:delimiter] || DELIMITER
206
- end
207
- def leaf? e
208
- [Iteraptor, Enumerable].none?(&e.method(:is_a?))
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iteraptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Matiushkin