iteraptor 0.6.1 → 0.7.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: cd67b42a75a6ed56774d77033653de940cfb1093
4
- data.tar.gz: 05ca53034c8fce538aecd7e74e4826eae4154285
3
+ metadata.gz: f9bae4e784ecbd35da45f7418b910a4d64707894
4
+ data.tar.gz: e594b3c0bd12351eb78b18c1dc9a84514e943360
5
5
  SHA512:
6
- metadata.gz: 9fc5336e833688e059b0b28459e55580a7158e0261dc7bea381dc965845d0831d7ff58f7ffb7ed923084091be3da8e8e9bdda8304060e797876bb195e87588ca
7
- data.tar.gz: 48ed7af274e24d80022b600853b7efa60083af54052343eb60e4b388cbb6ab876477cf80475c1226e0f0c4c6268024edc92dbfd8b8340ce462d4ff356a2c3be0
6
+ metadata.gz: b78f8e2885ebb7d2d66adf851c90a0a1aa4511d4ae616169d9a9af2b3f55f07c4cc6d5a3c9c68f486d4bc73ab19645f4307ab49b3c2f27b0aede0a2ba9b93134
7
+ data.tar.gz: 579f7f3a0904d54a8b56ca8e722b970676c01d0c3e3aad2625e778d0e772f61712c1d54991ec448553177c39b0400fea3cca1c6950923e20faaf818a268c890e
data/README.md CHANGED
@@ -16,6 +16,8 @@ large objects.
16
16
  require 'iteraptor'
17
17
  ```
18
18
 
19
+ **[Blog post](http://rocket-science.ru/hacking/2018/03/29/iteraptor-for-the-rescue) with detailed API documentation.**
20
+
19
21
  `Iteraptor` is intended to be used for iteration of complex nested structures.
20
22
  The yielder is being called with two parameters: “current key” and “current value.”
21
23
  The key is an index (converted to string for convenience) of an element for any
@@ -27,6 +29,22 @@ latter is just a dot in current release.
27
29
 
28
30
  ## Features
29
31
 
32
+ ### Boring (for users who are too conservative)
33
+
34
+ ```ruby
35
+ enum = [{foo: {bar: [:baz, 42]}}, [:foo, {bar: {baz: 42}}]].random
36
+
37
+ * `hash.iteraptor.each(**params, ->(full_key, value))`
38
+ * `hash.iteraptor.map(**params, ->(full_key, (key, value)))`
39
+ * `hash.iteraptor.select(*filters, **params, ->(full_key, value)`
40
+ * `hash.iteraptor.reject(*filters, **params, ->(full_key, value)`
41
+ * `hash.iteraptor.flat_map(**params, ->(full_key, value))`
42
+ * `hash.iteraptor.flatten(**params, ->(full_key, value))`
43
+ * `hash.iteraptor.collect(**params, ->(full_key, value))`
44
+ ```
45
+
46
+ ### Direct 🐒 patching in 🇪🇸
47
+
30
48
  * `cada` (_sp._ `each`) iterates through all the levels of the nested `Enumerable`,
31
49
  yielding `parent, element` tuple; parent is returned as a delimiter-joined string
32
50
  * `mapa` (_sp._ `map`) iterates all the elements, yielding `parent, (key, value)`;
data/lib/iteraptor.rb CHANGED
@@ -8,6 +8,10 @@ module Iteraptor
8
8
  raise "This module might be included into Enumerables only" unless base.ancestors.include? Enumerable
9
9
  end
10
10
 
11
+ def iteraptor
12
+ @__iteraptor__ ||= Iteraptor::Delegator.new(self)
13
+ end
14
+
11
15
  %i[cada mapa].each do |m|
12
16
  define_method m do |key = nil, value = nil, **params, &λ|
13
17
  return enum_for(m, key, value, **params) unless λ
@@ -201,9 +205,11 @@ module Iteraptor
201
205
  def safe_symbolize key
202
206
  key.respond_to?(:to_sym) ? key.to_sym : key
203
207
  end
208
+
204
209
  def iteraptor_delimiter(params)
205
210
  params[:delimiter] || DELIMITER
206
211
  end
212
+
207
213
  def push_flatten_compact array, tail
208
214
  case array
209
215
  when NilClass then [tail]
@@ -211,6 +217,7 @@ module Iteraptor
211
217
  else [array, tail]
212
218
  end.compact
213
219
  end
220
+
214
221
  def enumerable_parent?(receiver)
215
222
  [Hash, Array, Enumerable].detect(&receiver.method(:is_a?))
216
223
  end
@@ -219,3 +226,4 @@ module Iteraptor
219
226
  end
220
227
 
221
228
  require 'iteraptor/greedy'
229
+ require 'iteraptor/delegator'
@@ -0,0 +1,25 @@
1
+ module Iteraptor
2
+ class Delegator
3
+ MAPPING = {
4
+ each: :cada,
5
+ map: :mapa,
6
+
7
+ select: :escoger,
8
+ reject: :rechazar,
9
+
10
+ flatten: :aplanar,
11
+ collect: :recoger,
12
+ flat_map: :plana_mapa
13
+ }
14
+
15
+ def initialize(receiver)
16
+ @receiver = receiver
17
+ end
18
+
19
+ MAPPING.each do |m, delegate_to|
20
+ define_method m do |*args, **kw, &λ|
21
+ @receiver.send(delegate_to, *args, **kw, &λ)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Iteraptor
2
- VERSION = '0.6.1'
2
+ VERSION = '0.7.0'
3
3
  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.6.1
4
+ version: 0.7.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-30 00:00:00.000000000 Z
11
+ date: 2018-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -105,6 +105,7 @@ files:
105
105
  - bin/setup
106
106
  - iteraptor.gemspec
107
107
  - lib/iteraptor.rb
108
+ - lib/iteraptor/delegator.rb
108
109
  - lib/iteraptor/greedy.rb
109
110
  - lib/iteraptor/version.rb
110
111
  homepage: https://kantox.com
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  version: '0'
128
129
  requirements: []
129
130
  rubyforge_project:
130
- rubygems_version: 2.6.8
131
+ rubygems_version: 2.4.8
131
132
  signing_key:
132
133
  specification_version: 4
133
134
  summary: 'Iterator on steroids: allows thorough iteration through nested Enumerables'