iteraptor 0.5.2 → 0.6.0

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: fc9af3898925670c691fd396f57a315f73ca71e5
4
- data.tar.gz: 2a34471621ddb6cabfaa0f9c4c77af2feecef7de
3
+ metadata.gz: 51a190bd902813b3f2831501f2e2c8c8fd9cf555
4
+ data.tar.gz: fc10ba4bb1997340931716bea23d10dc8b8f0587
5
5
  SHA512:
6
- metadata.gz: 3cdef9589e987494eb07b316cac128d196ebe087d60ccd483aaa39d38334f16ddc283a8bbd8a18b3c5305a045ff3b9ad163c0b358d6a062665fb8203d906c6cb
7
- data.tar.gz: f656e2dcebaa9788a93eda98c8be6e05ff01decef8e25d284fb2aff810772be744622f2c036aa15141e2457f0f0296e80f94c89f7070c85dee5f76635e6484d9
6
+ metadata.gz: 7804c4da0e00f7077fca99af258e0bec1cfee7c4170926682492b8cda5d76192485bc3ee7e4d945dd55dced7e7ee77ba979a0cc608598333d6e7474450020876
7
+ data.tar.gz: 8afc7e6932c32412e97b1f95fe84d413a89dd4d9fdc174809e2a2cf7574c721434b5907b3a2186d1efeae51a1835a849668faa9ad01417650a9169561cc2d0af
data/README.md CHANGED
@@ -190,7 +190,9 @@ Or install it yourself as:
190
190
 
191
191
  ## Changelog
192
192
 
193
- #### 0.4.0 `aplanar` and `plana_mapa`
193
+ - **`0.6.0`** — experimental support for `full_parent: true` param
194
+ - **`0.5.0`** — `rechazar` and `escoger`
195
+ - **`0.4.0`** — `aplanar` and `plana_mapa`
194
196
 
195
197
  ## Development
196
198
 
@@ -1,3 +1,3 @@
1
1
  module Iteraptor
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.0'
3
3
  end
data/lib/iteraptor.rb CHANGED
@@ -14,7 +14,7 @@ module Iteraptor
14
14
  return enum_for(m, key, value, **params) unless λ
15
15
  return self if empty?
16
16
 
17
- send_to = [Hash, Array, Enumerable].detect(&method(:is_a?))
17
+ send_to = enumerable_parent?
18
18
  send_to && send("#{m}_in_#{send_to.name.downcase}", key || self, value, **params, &λ)
19
19
  end
20
20
  end
@@ -36,6 +36,7 @@ module Iteraptor
36
36
  def aplanar **params
37
37
  return self if empty?
38
38
  cada(**params).with_object({}) do |(key, value), acc|
39
+ key = key.join(iteraptor_delimiter(params)) if params[:full_parent]
39
40
  key = key.to_sym if params[:symbolize_keys]
40
41
  acc[key] = value unless value.is_a?(Enumerable)
41
42
  yield key, value if block_given?
@@ -48,7 +49,7 @@ module Iteraptor
48
49
  aplanar(**params).each_with_object(
49
50
  Hash.new { |h, k| h[k] = h.clone.clear }
50
51
  ) do |(k, v), acc|
51
- keys = k.to_s.split(params[:delimiter] || DELIMITER)
52
+ keys = k.to_s.split(iteraptor_delimiter(params))
52
53
  parent = keys[0..-2].reduce(acc){ |h, kk| h[kk] }
53
54
  parent[keys.last] = v
54
55
  end.mapa(yield_all: true, **params) do |_parent, (k, v)|
@@ -62,6 +63,7 @@ module Iteraptor
62
63
  return self if empty?
63
64
 
64
65
  cada(**params).with_object([]) do |(key, value), acc|
66
+ key = key.join(iteraptor_delimiter(params)) if params[:full_parent]
65
67
  key = key.to_sym if params[:symbolize_keys]
66
68
  acc << yield(key, value) unless value.is_a?(Enumerable)
67
69
  end
@@ -78,21 +80,21 @@ module Iteraptor
78
80
  end
79
81
  end
80
82
 
81
- def cada_in_array root = nil, parent = nil, **params
82
- λ = Proc.new
83
- each.with_index do |e, idx|
84
- [parent, idx].compact.join(params[:delimiter] || DELIMITER).tap do |p|
85
- yield p, e
86
- CADA_PROC.call(e, root, p, **params, &λ)
87
- end
88
- end
83
+ def cada_in_array root = nil, parent = nil, **params, &λ
84
+ cada_in_array_or_hash true, root, parent, **params, &λ
89
85
  end
90
86
  alias cada_in_enumerable cada_in_array
91
87
 
92
- def cada_in_hash root = nil, parent = nil, **params
93
- λ = Proc.new
94
- each do |k, v|
95
- [parent, k].compact.join(params[:delimiter] || DELIMITER).tap do |p|
88
+ def cada_in_hash root = nil, parent = nil, **params, &λ
89
+ cada_in_array_or_hash false, root, parent, **params, &λ
90
+ end
91
+
92
+ def cada_in_array_or_hash in_array, root = nil, parent = nil, **params, &λ
93
+ (in_array ? each_with_index : each).each do |k, v|
94
+ k, v = v, k if in_array
95
+ result = [parent, k].flatten.compact
96
+ result = result.join(iteraptor_delimiter(params)) unless params[:full_parent]
97
+ result.tap do |p|
96
98
  yield p, v
97
99
  CADA_PROC.call(v, root, p, **params, &λ)
98
100
  end
@@ -106,18 +108,26 @@ module Iteraptor
106
108
  # - with_index
107
109
  # - yield_all
108
110
  # - symbolize_keys
109
- def mapa_in_array root = nil, parent = nil, **params
110
- λ = Proc.new
111
-
111
+ def mapa_in_array root = nil, parent = nil, **params, &λ
112
112
  map.with_index do |e, idx|
113
- p = [parent, idx].compact.join(params[:delimiter] || DELIMITER)
114
-
115
- e = yield p, (params[:with_index] ? [idx.to_s, e] : e) if !e.is_a?(Enumerable) || params[:yield_all]
116
-
117
- case e
118
- when Iteraptor then e.mapa(root, p, **params, )
119
- when Enumerable then e.map(&λ.curry[p])
120
- else e
113
+ p = [parent, idx].flatten.compact
114
+ p = p.join(iteraptor_delimiter(params)) unless params[:full_parent]
115
+
116
+ yielded =
117
+ if !enumerable_parent?(e) || params[:yield_all]
118
+ yield p, (params[:with_index] ? [idx.to_s, e] : e)
119
+ else
120
+ e
121
+ end
122
+ # allow blindly return [k, v] instead of check for an array instance
123
+ # when block arguments are matched as (k, v)
124
+ yielded = yielded.last || yielded.first if
125
+ yielded.is_a?(Array) && yielded.size == 2 && (yielded.last.nil? || yielded.first == e)
126
+
127
+ case yielded
128
+ when Iteraptor then yielded.mapa(root, p, **params, &λ)
129
+ when Enumerable then yielded.map(&λ.curry[p])
130
+ else yielded
121
131
  end
122
132
  end
123
133
  end
@@ -126,11 +136,10 @@ module Iteraptor
126
136
  # Params:
127
137
  # - yield_all
128
138
  # - symbolize_keys
129
- def mapa_in_hash root = nil, parent = nil, **params
130
- λ = Proc.new
131
-
139
+ def mapa_in_hash root = nil, parent = nil, **params, &λ
132
140
  map do |k, v|
133
- p = [parent, k].compact.join(params[:delimiter] || DELIMITER)
141
+ p = [parent, k].flatten.compact
142
+ p = p.join(iteraptor_delimiter(params)) unless params[:full_parent]
134
143
 
135
144
  k, v = yield p, [k, v] if !v.is_a?(Enumerable) || params[:yield_all]
136
145
 
@@ -176,8 +185,8 @@ module Iteraptor
176
185
  raise ArgumentError, "no filter given in call to #{method ? :escoger : :rechazar}" if filter.empty?
177
186
 
178
187
  plough = method ? :none? : :any?
179
- aplanar.each_with_object({}) do |(key, value), acc|
180
- to_match = key.split(params[:delimiter] || DELIMITER)
188
+ aplanar(**params).each_with_object({}) do |(key, value), acc|
189
+ to_match = key.to_s.split(iteraptor_delimiter(params)) unless params[:full_parent]
181
190
  to_match = to_match.flat_map { |k| [k.to_s, k.to_s.to_sym] } if params[:soft_keys]
182
191
 
183
192
  next if filter.public_send(plough, &->(f){ to_match.any?(&f.method(:===)) })
@@ -189,6 +198,12 @@ module Iteraptor
189
198
 
190
199
  ##############################################################################
191
200
  ### 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
192
207
  def leaf? e
193
208
  [Iteraptor, Enumerable].none?(&e.method(:is_a?))
194
209
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iteraptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Matiushkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-29 00:00:00.000000000 Z
11
+ date: 2018-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -127,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  version: '0'
128
128
  requirements: []
129
129
  rubyforge_project:
130
- rubygems_version: 2.4.8
130
+ rubygems_version: 2.6.8
131
131
  signing_key:
132
132
  specification_version: 4
133
133
  summary: 'Iterator on steroids: allows thorough iteration through nested Enumerables'