iteraptor 0.3.5 → 0.4.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: 7333782eaae52f82638bd82f741a1ac2cdec49af
4
- data.tar.gz: 1fc1b09cee6edfd7f350d049a4a9d13905d649fc
3
+ metadata.gz: 0e8cc46b604659e350fd66dd26157c0a0810373c
4
+ data.tar.gz: 10105442b0e2c0a807e55ff1decf52fbef0d5772
5
5
  SHA512:
6
- metadata.gz: 2b37702ea08e4b0a9a63ad35670c50411c223f1db8425d63cebaee6dc09d9a631e11a0ec08b138fee87ceff1cbfefd981829fd5e770db7a439db6954237ab44b
7
- data.tar.gz: fb4662a8024047f6269fd4fbc921fad75b289ce32f54061bebbd4409341b05933db16e32fba6dfe5f1079ab0c60dc675f5119d5dfc38304f89e35185e764f82c
6
+ metadata.gz: ff5aade1285567f7bf58d422316db68b3e42627c828c13ad54d6c1f533d7542b3c15ca666b8a0b33ff175e6f83363c17ad591c4b7351f031808019938c8e348f
7
+ data.tar.gz: bd271aae4e66a0528f0efb9eba9b5e2bd0cc87082ffee878e5a134fc5bf57358f498cb968c9fce0d81dca90a0cc384f877d0252c97b0c191825cdebde7024502
data/.codeclimate.yml CHANGED
@@ -2,6 +2,9 @@
2
2
  engines:
3
3
  duplication:
4
4
  enabled: true
5
+ checks:
6
+ Similar code:
7
+ enabled: false
5
8
  config:
6
9
  languages:
7
10
  - ruby
data/.travis.yml CHANGED
@@ -1,7 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.1.8
4
- - 2.2.0
4
+ - 2.2.3
5
+ - 2.3.2
6
+ - 2.4.1
7
+ - 2.5.0
5
8
  before_install: gem install bundler -v 1.11.2
6
9
  addons:
7
10
  code_climate:
data/Gemfile CHANGED
@@ -2,4 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in iteraptor.gemspec
4
4
  gemspec
5
- gem 'codeclimate-test-reporter', group: :test, require: nil
5
+ gem 'simplecov', require: false, group: :test
6
+ # gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -10,27 +10,10 @@ This small mixin allows the deep iteration / mapping of `Enumerable`s instances.
10
10
  Adopted to be used with hashes/arrays. It **is not** intended to be used with
11
11
  large objects.
12
12
 
13
- ## Installation
14
-
15
- Add this line to your application's Gemfile:
16
-
17
- ```ruby
18
- gem 'iteraptor'
19
- ```
20
-
21
- And then execute:
22
-
23
- $ bundle
24
-
25
- Or install it yourself as:
26
-
27
- $ gem install iteraptor
28
-
29
13
  ## Usage
30
14
 
31
15
  ```ruby
32
16
  require 'iteraptor'
33
- require 'iteraptor/greedy' # to patch Array and Hash
34
17
  ```
35
18
 
36
19
  `Iteraptor` is intended to be used for iteration of complex nested structures.
@@ -42,6 +25,22 @@ Nested `Enumerable`s are called with a compound key, represented as a “breadcr
42
25
  which is a path to current key, joined with `Iteraptor::DELIMITER` constant. The
43
26
  latter is just a dot in current release.
44
27
 
28
+ ## Features
29
+
30
+ * `cada` (_sp._ `each`) iterates through all the levels of the nested `Enumerable`,
31
+ yielding `parent, element` tuple; parent is returned as a delimiter-joined string
32
+ * `mapa` (_sp._ `map`) iterates all the elements, yielding `parent, (key, value)`;
33
+ the mapper should return either `[key, value]` array or `nil` to remove this
34
+ element;
35
+ * _NB_ this method always maps to `Hash`, to map to `Array` use `plana_mapa`
36
+ * _NB_ this method will raise if the returned value is neither `[key, value]` tuple nor `nil`
37
+ * `plana_mapa` iterates yielding `key, value`, maps to the yielded value,
38
+ whatever it is; `nil`s are not treated in some special way
39
+ * `aplanar` (_sp._ `flatten`) the analogue of `Array#flatten`, but flattens
40
+ the deep enumerable into `Hash` instance
41
+ * `segar` (_sp._ `yield`), alias `escoger` (_sp._ `select`) allows to filter
42
+ and collect elelements.
43
+
45
44
  ### Iteration
46
45
 
47
46
  `Iteraptor#cada` iterates all the `Enumerable` elements, recursively. As it meets
@@ -97,7 +96,9 @@ iterated as `Enumerable`s.
97
96
  In the example below we yield all keys, that matches the regexp given as parameter.
98
97
 
99
98
  ```ruby
100
- ▶ hash.segar(/[abc]/) { |parent, elem| puts "Parent: #{parent.inspect}, Element: #{elem.inspect}" }
99
+ ▶ hash.segar(/[abc]/) do |parent, elem|
100
+ ▷ puts "Parent: #{parent.inspect}, Element: #{elem.inspect}"
101
+ ▷ end
101
102
  # Parent: "a", Element: true
102
103
  # Parent: "b", Element: {:c=>"", :d=>42}
103
104
  # Parent: "b.c", Element: ""
@@ -115,6 +116,35 @@ In the example below we yield all keys, that matches the regexp given as paramet
115
116
  #⇒ {:a=>true, :b=>{:c=>"N/A", :d=>42}, :e=>"N/A"}
116
117
  ```
117
118
 
119
+ #### Flatten the deeply nested hash:
120
+
121
+ ```ruby
122
+ ▶ hash = {a: true, b: {c: '', d: 42}, e: ''}
123
+ #⇒ {:a=>true, :b=>{:c=>"", :d=>42}, :e=>""}
124
+ ▶ hash.aplanar(delimiter: '_', symbolize_keys: true)
125
+ #⇒ {:a=>true, :b_c=>"", :b_d=>42, :e=>""}
126
+ ```
127
+
128
+ ## Installation
129
+
130
+ Add this line to your application's Gemfile:
131
+
132
+ ```ruby
133
+ gem 'iteraptor'
134
+ ```
135
+
136
+ And then execute:
137
+
138
+ $ bundle
139
+
140
+ Or install it yourself as:
141
+
142
+ $ gem install iteraptor
143
+
144
+ ## Changelog
145
+
146
+ #### 0.4.0 `aplanar` and `plana_mapa`
147
+
118
148
  ## Development
119
149
 
120
150
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/iteraptor.rb CHANGED
@@ -7,7 +7,7 @@ module Iteraptor
7
7
  raise "This module might be included into Enumerables only" unless base.ancestors.include? Enumerable
8
8
  end
9
9
 
10
- %i(cada mapa).each do |m|
10
+ %i[cada mapa].each do |m|
11
11
  define_method m do |root = nil, parent = nil, &λ|
12
12
  return enum_for(m, root, parent) unless λ
13
13
  send_to = [Hash, Array, Enumerable].detect(&method(:is_a?))
@@ -15,6 +15,7 @@ module Iteraptor
15
15
  end
16
16
  end
17
17
 
18
+ # rubocop:disable Style/MultilineIfModifier
18
19
  # rubocop:disable Metrics/CyclomaticComplexity
19
20
  # rubocop:disable Metrics/MethodLength
20
21
  def segar filter
@@ -33,8 +34,31 @@ module Iteraptor
33
34
  end
34
35
  end
35
36
  end
37
+
38
+ def aplanar delimiter: DELIMITER, symbolize_keys: false
39
+ cada.with_object({}) do |(parent, element), acc|
40
+ key = parent.tr(DELIMITER, delimiter)
41
+ key = key.to_sym if symbolize_keys
42
+ acc[key] = element unless element.is_a?(Enumerable)
43
+ yield key, element if block_given?
44
+ end
45
+ end
46
+
47
+ def plana_mapa delimiter: DELIMITER, symbolize_keys: false
48
+ return enum_for(
49
+ :plana_mapa, delimiter: delimiter, symbolize_keys: symbolize_keys
50
+ ) unless block_given?
51
+
52
+ cada.with_object([]) do |(parent, element), acc|
53
+ key = parent.tr(DELIMITER, delimiter)
54
+ key = key.to_sym if symbolize_keys
55
+ acc << yield(key, element) unless element.is_a?(Enumerable)
56
+ end
57
+ end
58
+
36
59
  # rubocop:enable Metrics/MethodLength
37
60
  # rubocop:enable Metrics/CyclomaticComplexity
61
+ # rubocop:enable Style/MultilineIfModifier
38
62
 
39
63
  private
40
64
 
@@ -70,7 +94,7 @@ module Iteraptor
70
94
 
71
95
  ##############################################################################
72
96
  ### mapa
73
- def mapa_in_array root = nil, parent = nil
97
+ def mapa_in_array root = nil, parent = nil, with_index: false
74
98
  λ = Proc.new
75
99
 
76
100
  map.with_index do |e, idx|
@@ -79,7 +103,7 @@ module Iteraptor
79
103
  case e
80
104
  when Iteraptor then e.mapa(root, p, &λ)
81
105
  when Enumerable then e.map(&λ.curry[p])
82
- else yield p, e
106
+ else yield p, (with_index ? [idx.to_s, e] : e)
83
107
  end
84
108
  end
85
109
  end
@@ -1,3 +1,3 @@
1
1
  module Iteraptor
2
- VERSION = '0.3.5'
2
+ VERSION = '0.4.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.3.5
4
+ version: 0.4.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: 2016-08-22 00:00:00.000000000 Z
11
+ date: 2018-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler