iteraptor 0.3.1 → 0.3.4

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: 88220785136babdaf6fb26f65d54e8862b04866e
4
- data.tar.gz: 8a4c700a14fb121386b5f3327890fe473bb24075
3
+ metadata.gz: 1a2d224b5bd2a1dc52a33ec80ef9ee26d91975c1
4
+ data.tar.gz: 4b6d3ad8d728849725c85395b30d68812dc026c6
5
5
  SHA512:
6
- metadata.gz: 489b11522d244df103cfdf187f12cac2de6e066e3be4a64472d6aea8d8dea2d4cffb9f6990eeac3783722a7a9ceaa47e8794f665c09052b14d266fab8e7d0478
7
- data.tar.gz: f531257ed2ba8999dbe607a80a4284a518b28340802d4cf78736d6e978c0821629856e1217f6c043e14574622c9e418184d5cce26a0ff6302e14a3e130da56b2
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
@@ -2,3 +2,4 @@ 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
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # Iteraptor
2
2
 
3
- [![Build Status](https://travis-ci.org/am-kantox/iteraptor.svg?branch=master)](https://travis-ci.org/am-kantox/iteraptor)
4
3
  This small mixin allows the deep iteration / mapping of `Enumerable`s instances.
5
4
 
5
+ [![Build Status](https://travis-ci.org/am-kantox/iteraptor.svg?branch=master)](https://travis-ci.org/am-kantox/iteraptor)
6
+ [![Code Climate](https://codeclimate.com/github/am-kantox/iteraptor/badges/gpa.svg)](https://codeclimate.com/github/am-kantox/iteraptor)
7
+ [![Issue Count](https://codeclimate.com/github/am-kantox/iteraptor/badges/issue_count.svg)](https://codeclimate.com/github/am-kantox/iteraptor)
8
+ [![Test Coverage](https://codeclimate.com/github/am-kantox/iteraptor/badges/coverage.svg)](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.11'
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'
@@ -1,3 +1,3 @@
1
1
  module Iteraptor
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.4"
3
3
  end
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
- p = [parent, idx].compact.join(DELIMITER)
48
-
49
- yield p, e
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
- p = [parent, k].compact.join(DELIMITER)
64
-
65
- yield p, v
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.1
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-04-19 00:00:00.000000000 Z
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.11'
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.11'
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"