iteraptor 0.3.1 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.codeclimate.yml +26 -0
- data/Gemfile +1 -0
- data/README.md +73 -2
- data/iteraptor.gemspec +1 -1
- data/lib/iteraptor/version.rb +1 -1
- data/lib/iteraptor.rb +14 -17
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a2d224b5bd2a1dc52a33ec80ef9ee26d91975c1
|
4
|
+
data.tar.gz: 4b6d3ad8d728849725c85395b30d68812dc026c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c09cb4f5408a425d3f0df9f1df14a98da347838baf5b6d28b5e2e434b7b18706391ee9f0f1ce6505c600e0dc0446b743f36b66d16518178d49cabf6924318d55
|
7
|
+
data.tar.gz: a138d6f5ff0cdf6d80901d5d52c960dcb5e44c94d15b6456b75ef0cc99aa8d7a1b1d1426ae9cd9ee6d8617fb91fe9e53f8ab456aea3d7b3ea5d9b4458dec7c10
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
---
|
2
|
+
engines:
|
3
|
+
duplication:
|
4
|
+
enabled: true
|
5
|
+
config:
|
6
|
+
languages:
|
7
|
+
- ruby
|
8
|
+
- javascript
|
9
|
+
- python
|
10
|
+
- php
|
11
|
+
fixme:
|
12
|
+
enabled: true
|
13
|
+
rubocop:
|
14
|
+
enabled: true
|
15
|
+
ratings:
|
16
|
+
paths:
|
17
|
+
- "**.inc"
|
18
|
+
- "**.js"
|
19
|
+
- "**.jsx"
|
20
|
+
- "**.module"
|
21
|
+
- "**.php"
|
22
|
+
- "**.py"
|
23
|
+
- "**.rb"
|
24
|
+
exclude_paths:
|
25
|
+
- features/
|
26
|
+
- spec/
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# Iteraptor
|
2
2
|
|
3
|
-
[](https://travis-ci.org/am-kantox/iteraptor)
|
4
3
|
This small mixin allows the deep iteration / mapping of `Enumerable`s instances.
|
5
4
|
|
5
|
+
[](https://travis-ci.org/am-kantox/iteraptor)
|
6
|
+
[](https://codeclimate.com/github/am-kantox/iteraptor)
|
7
|
+
[](https://codeclimate.com/github/am-kantox/iteraptor)
|
8
|
+
[](https://codeclimate.com/github/am-kantox/iteraptor/coverage)
|
9
|
+
|
6
10
|
Adopted to be used with hashes/arrays. It **is not** intended to be used with
|
7
11
|
large objects.
|
8
12
|
|
@@ -24,12 +28,27 @@ Or install it yourself as:
|
|
24
28
|
|
25
29
|
## Usage
|
26
30
|
|
31
|
+
```ruby
|
32
|
+
require 'iteraptor'
|
33
|
+
require 'iteraptor/greedy' # to patch Array and Hash
|
34
|
+
```
|
35
|
+
|
36
|
+
`Iteraptor` is intended to be used for iteration of complex nested structures.
|
37
|
+
The yielder is being called with two parameters: “current key” and “current value.”
|
38
|
+
The key is an index (converted to string for convenience) of an element for any
|
39
|
+
`Enumerable` save for `Hash`.
|
27
40
|
|
41
|
+
Nested `Enumerable`s are called with a compound key, represented as a “breadcrumb,”
|
42
|
+
which is a path to current key, joined with `Iteraptor::DELIMITER` constant. The
|
43
|
+
latter is just a dot in current release.
|
28
44
|
|
29
45
|
### Iteration
|
30
46
|
|
47
|
+
`Iteraptor#cada` iterates all the `Enumerable` elements, recursively. As it meets
|
48
|
+
the `Enumerable`, it yields it and then iterates items through.
|
49
|
+
|
31
50
|
```ruby
|
32
|
-
λ = ->(parent, element) { puts
|
51
|
+
λ = ->(parent, element) { puts "#{parent} » #{element.inspect}" }
|
33
52
|
|
34
53
|
[:a, b: {c: 42}].cada &λ
|
35
54
|
#⇒ 0 » :a
|
@@ -44,6 +63,58 @@ Or install it yourself as:
|
|
44
63
|
#⇒ b.1 » :d
|
45
64
|
```
|
46
65
|
|
66
|
+
### Mapping
|
67
|
+
|
68
|
+
Mapper function should return a pair `[k, v]` or `nil` when called from hash,
|
69
|
+
or just a value when called from an array. E. g., deep hash filtering:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
▶ hash = {a: true, b: {c: '', d: 42}, e: ''}
|
73
|
+
#⇒ {:a=>true, :b=>{:c=>"", :d=>42}, :e=>""}
|
74
|
+
▶ hash.mapa { |parent, (k, v)| v == '' ? nil : [k, v] }
|
75
|
+
#⇒ {:a=>true, :b=>{:d=>42}}
|
76
|
+
```
|
77
|
+
|
78
|
+
This is not quite convenient, but I currently have no idea how to help
|
79
|
+
the consumer to decide what to return, besides analyzing the arguments,
|
80
|
+
received by code block. That is because internally both `Hash` and `Array` are
|
81
|
+
iterated as `Enumerable`s.
|
82
|
+
|
83
|
+
## Examples
|
84
|
+
|
85
|
+
#### Find and report all empty values:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
▶ hash = {a: true, b: {c: '', d: 42}, e: ''}
|
89
|
+
#⇒ {:a=>true, :b=>{:c=>"", :d=>42}, :e=>""}
|
90
|
+
▶ hash.cada { |k, v| puts "#{k} has an empty value" if v == '' }
|
91
|
+
#⇒ b.c has an empty value
|
92
|
+
#⇒ e has an empty value
|
93
|
+
```
|
94
|
+
|
95
|
+
#### Filter keys, that meet a condition:
|
96
|
+
|
97
|
+
In the example below we yield all keys, that matches the regexp given as parameter.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
▶ hash.segar(/[abc]/) { |parent, elem| puts "Parent: #{parent.inspect}, Element: #{elem.inspect}" }
|
101
|
+
# Parent: "a", Element: true
|
102
|
+
# Parent: "b", Element: {:c=>"", :d=>42}
|
103
|
+
# Parent: "b.c", Element: ""
|
104
|
+
# Parent: "b.d", Element: 42
|
105
|
+
|
106
|
+
#⇒ {"a"=>true, "b"=>{:c=>"", :d=>42}, "b.c"=>"", "b.d"=>42}
|
107
|
+
```
|
108
|
+
|
109
|
+
#### Change all empty values in a hash to `'N/A'`:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
▶ hash = {a: true, b: {c: '', d: 42}, e: ''}
|
113
|
+
#⇒ {:a=>true, :b=>{:c=>"", :d=>42}, :e=>""}
|
114
|
+
▶ hash.mapa { |parent, (k, v)| [k, v == '' ? v = 'N/A' : v] }
|
115
|
+
#⇒ {:a=>true, :b=>{:c=>"N/A", :d=>42}, :e=>"N/A"}
|
116
|
+
```
|
117
|
+
|
47
118
|
## Development
|
48
119
|
|
49
120
|
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/iteraptor.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
25
25
|
spec.require_paths = ['lib']
|
26
26
|
|
27
|
-
spec.add_development_dependency 'bundler', '~> 1
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1'
|
28
28
|
spec.add_development_dependency 'rake', '~> 10.0'
|
29
29
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
30
|
spec.add_development_dependency 'pry', '~> 0.10'
|
data/lib/iteraptor/version.rb
CHANGED
data/lib/iteraptor.rb
CHANGED
@@ -40,17 +40,19 @@ module Iteraptor
|
|
40
40
|
|
41
41
|
##############################################################################
|
42
42
|
### cada
|
43
|
+
CADA_PROC = lambda do |e, root, p, &λ|
|
44
|
+
case e
|
45
|
+
when Iteraptor then e.cada(root, p, &λ)
|
46
|
+
when Enumerable then e.each(&λ.curry[p])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
43
50
|
def cada_in_array root = nil, parent = nil
|
44
51
|
λ = Proc.new
|
45
|
-
|
46
52
|
each.with_index do |e, idx|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
case e
|
52
|
-
when Iteraptor then e.cada(root, p, &λ)
|
53
|
-
when Enumerable then e.each(&λ.curry[p])
|
53
|
+
[parent, idx].compact.join(DELIMITER).tap do |p|
|
54
|
+
yield p, e
|
55
|
+
CADA_PROC.call(e, root, p, &λ)
|
54
56
|
end
|
55
57
|
end
|
56
58
|
end
|
@@ -58,15 +60,10 @@ module Iteraptor
|
|
58
60
|
|
59
61
|
def cada_in_hash root = nil, parent = nil
|
60
62
|
λ = Proc.new
|
61
|
-
|
62
63
|
each do |k, v|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
case v
|
68
|
-
when Iteraptor then v.cada(root, p, &λ)
|
69
|
-
when Enumerable then v.each(&λ.curry[p])
|
64
|
+
[parent, k].compact.join(DELIMITER).tap do |p|
|
65
|
+
yield p, v
|
66
|
+
CADA_PROC.call(v, root, p, &λ)
|
70
67
|
end
|
71
68
|
end
|
72
69
|
end
|
@@ -99,7 +96,7 @@ module Iteraptor
|
|
99
96
|
when Enumerable then [k, v.map(&λ.curry[p])]
|
100
97
|
else yield p, [k, v]
|
101
98
|
end
|
102
|
-
end.to_h
|
99
|
+
end.compact.to_h
|
103
100
|
end
|
104
101
|
|
105
102
|
##############################################################################
|
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.
|
4
|
+
version: 0.3.4
|
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-
|
11
|
+
date: 2016-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1
|
19
|
+
version: '1'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1
|
26
|
+
version: '1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,6 +76,7 @@ executables:
|
|
76
76
|
extensions: []
|
77
77
|
extra_rdoc_files: []
|
78
78
|
files:
|
79
|
+
- ".codeclimate.yml"
|
79
80
|
- ".gitignore"
|
80
81
|
- ".rspec"
|
81
82
|
- ".rubocop.yml"
|