iteraptor 0.5.1 → 0.5.2

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: bfa0333669ac2722234680072c6e4c062c4d66d4
4
- data.tar.gz: 193f191b05f778f547627c4b9560d305ef1144f5
3
+ metadata.gz: fc9af3898925670c691fd396f57a315f73ca71e5
4
+ data.tar.gz: 2a34471621ddb6cabfaa0f9c4c77af2feecef7de
5
5
  SHA512:
6
- metadata.gz: d7f4dfab10985de98764a59417c64224b7a21462589e03e4fbeec31460b5b0ec8c03cf611838434f819c881782c0a3f8d180f5b82da5a78820aeae692aa9b1ed
7
- data.tar.gz: b64575c124d45e50378c3c33c3a323cd4d3a29e532fecaf4420064ebdb8bec6d3fbfef4f2eae00238f32065dffb8502ae64d3f141c2cefb827ee5b7d10de4fbe
6
+ metadata.gz: 3cdef9589e987494eb07b316cac128d196ebe087d60ccd483aaa39d38334f16ddc283a8bbd8a18b3c5305a045ff3b9ad163c0b358d6a062665fb8203d906c6cb
7
+ data.tar.gz: f656e2dcebaa9788a93eda98c8be6e05ff01decef8e25d284fb2aff810772be744622f2c036aa15141e2457f0f0296e80f94c89f7070c85dee5f76635e6484d9
data/README.md CHANGED
@@ -44,6 +44,50 @@ the nested structure out of flattened hash
44
44
  and collect elelements
45
45
  * `rechazar` (_sp._ `reject`) allows to filter out and collect elelements.
46
46
 
47
+ ### Words are cheap, show me the code
48
+
49
+ ```ruby
50
+ ▶ require 'iteraptor'
51
+ #⇒ true
52
+
53
+ ▶ hash = {company: {name: "Me", currencies: ["A", "B", "C"],
54
+ ▷ password: "12345678",
55
+ ▷ details: {another_password: "QWERTYUI"}}}
56
+ #⇒ {:company=>{:name=>"Me", :currencies=>["A", "B", "C"],
57
+ # :password=>"12345678",
58
+ # :details=>{:another_password=>"QWERTYUI"}}}
59
+
60
+ ▶ hash.segar(/password/i) { "*" * 8 }
61
+ #⇒ {"company"=>{"password"=>"********",
62
+ # "details"=>{"another_password"=>"********"}}}
63
+
64
+ ▶ hash.segar(/password/i) { |*args| puts args.inspect }
65
+ ["company.password", "12345678"]
66
+ ["company.details.another_password", "QWERTYUI"]
67
+ #⇒ {"company"=>{"password"=>nil, "details"=>{"another_password"=>nil}}}
68
+
69
+ ▶ hash.rechazar(/password/)
70
+ #⇒ {"company"=>{"name"=>"Me", "currencies"=>["A", "B", "C"]}}
71
+
72
+ ▶ hash.aplanar
73
+ #⇒ {"company.name"=>"Me",
74
+ # "company.currencies.0"=>"A",
75
+ # "company.currencies.1"=>"B",
76
+ # "company.currencies.2"=>"C",
77
+ # "company.password"=>"12345678",
78
+ # "company.details.another_password"=>"QWERTYUI"}
79
+
80
+ ▶ hash.aplanar.recoger
81
+ #⇒ {"company"=>{"name"=>"Me", "currencies"=>["A", "B", "C"],
82
+ # "password"=>"12345678",
83
+ # "details"=>{"another_password"=>"QWERTYUI"}}}
84
+
85
+ ▶ hash.aplanar.recoger(symbolize_keys: true)
86
+ #⇒ {:company=>{:name=>"Me", :currencies=>["A", "B", "C"],
87
+ # :password=>"12345678",
88
+ # :details=>{:another_password=>"QWERTYUI"}}}
89
+ ```
90
+
47
91
  ### Iteration
48
92
 
49
93
  `Iteraptor#cada` iterates all the `Enumerable` elements, recursively. As it meets
@@ -1,3 +1,3 @@
1
1
  module Iteraptor
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
data/lib/iteraptor.rb CHANGED
@@ -10,12 +10,12 @@ module Iteraptor
10
10
  end
11
11
 
12
12
  %i[cada mapa].each do |m|
13
- define_method m do |root = nil, parent = nil, **params, &λ|
14
- return enum_for(m, root, parent, **params) unless λ
13
+ define_method m do |key = nil, value = nil, **params, &λ|
14
+ return enum_for(m, key, value, **params) unless λ
15
15
  return self if empty?
16
16
 
17
17
  send_to = [Hash, Array, Enumerable].detect(&method(:is_a?))
18
- send_to && send("#{m}_in_#{send_to.name.downcase}", root || self, parent, **params, &λ)
18
+ send_to && send("#{m}_in_#{send_to.name.downcase}", key || self, value, **params, &λ)
19
19
  end
20
20
  end
21
21
 
@@ -33,23 +33,22 @@ module Iteraptor
33
33
  alias_method :segar, :escoger
34
34
  # rubocop:enable Style/Alias
35
35
 
36
- def aplanar delimiter: DELIMITER, **params
36
+ def aplanar **params
37
37
  return self if empty?
38
- cada.with_object({}) do |(parent, element), acc|
39
- key = parent.tr(DELIMITER, delimiter)
38
+ cada(**params).with_object({}) do |(key, value), acc|
40
39
  key = key.to_sym if params[:symbolize_keys]
41
- acc[key] = element unless element.is_a?(Enumerable)
42
- yield key, element if block_given?
40
+ acc[key] = value unless value.is_a?(Enumerable)
41
+ yield key, value if block_given?
43
42
  end
44
43
  end
45
44
 
46
- def recoger delimiter: DELIMITER, **params
45
+ def recoger **params
47
46
  return self if empty?
48
47
  # rubocop:disable Style/MultilineBlockChain
49
- aplanar.each_with_object(
48
+ aplanar(**params).each_with_object(
50
49
  Hash.new { |h, k| h[k] = h.clone.clear }
51
50
  ) do |(k, v), acc|
52
- keys = k.split(delimiter)
51
+ keys = k.to_s.split(params[:delimiter] || DELIMITER)
53
52
  parent = keys[0..-2].reduce(acc){ |h, kk| h[kk] }
54
53
  parent[keys.last] = v
55
54
  end.mapa(yield_all: true, **params) do |_parent, (k, v)|
@@ -58,14 +57,13 @@ module Iteraptor
58
57
  # rubocop:enable Style/MultilineBlockChain
59
58
  end
60
59
 
61
- def plana_mapa delimiter: DELIMITER, **params
62
- return enum_for(:plana_mapa, delimiter: delimiter, **params) unless block_given?
60
+ def plana_mapa **params
61
+ return enum_for(:plana_mapa, delimiter: params[:delimiter], **params) unless block_given?
63
62
  return self if empty?
64
63
 
65
- cada.with_object([]) do |(parent, element), acc|
66
- key = parent.tr(DELIMITER, delimiter)
64
+ cada(**params).with_object([]) do |(key, value), acc|
67
65
  key = key.to_sym if params[:symbolize_keys]
68
- acc << yield(key, element) unless element.is_a?(Enumerable)
66
+ acc << yield(key, value) unless value.is_a?(Enumerable)
69
67
  end
70
68
  end
71
69
 
@@ -73,30 +71,30 @@ module Iteraptor
73
71
 
74
72
  ##############################################################################
75
73
  ### cada
76
- CADA_PROC = lambda do |e, root, p, &λ|
74
+ CADA_PROC = lambda do |e, root, p, **params, &λ|
77
75
  case e
78
- when Iteraptor then e.cada(root, p, &λ)
76
+ when Iteraptor then e.cada(root, p, **params, &λ)
79
77
  when Enumerable then e.each(&λ.curry[p])
80
78
  end
81
79
  end
82
80
 
83
- def cada_in_array root = nil, parent = nil, **_
81
+ def cada_in_array root = nil, parent = nil, **params
84
82
  λ = Proc.new
85
83
  each.with_index do |e, idx|
86
- [parent, idx].compact.join(DELIMITER).tap do |p|
84
+ [parent, idx].compact.join(params[:delimiter] || DELIMITER).tap do |p|
87
85
  yield p, e
88
- CADA_PROC.call(e, root, p, &λ)
86
+ CADA_PROC.call(e, root, p, **params, &λ)
89
87
  end
90
88
  end
91
89
  end
92
90
  alias cada_in_enumerable cada_in_array
93
91
 
94
- def cada_in_hash root = nil, parent = nil, **_
92
+ def cada_in_hash root = nil, parent = nil, **params
95
93
  λ = Proc.new
96
94
  each do |k, v|
97
- [parent, k].compact.join(DELIMITER).tap do |p|
95
+ [parent, k].compact.join(params[:delimiter] || DELIMITER).tap do |p|
98
96
  yield p, v
99
- CADA_PROC.call(v, root, p, &λ)
97
+ CADA_PROC.call(v, root, p, **params, &λ)
100
98
  end
101
99
  end
102
100
  end
@@ -112,7 +110,7 @@ module Iteraptor
112
110
  λ = Proc.new
113
111
 
114
112
  map.with_index do |e, idx|
115
- p = [parent, idx].compact.join(DELIMITER)
113
+ p = [parent, idx].compact.join(params[:delimiter] || DELIMITER)
116
114
 
117
115
  e = yield p, (params[:with_index] ? [idx.to_s, e] : e) if !e.is_a?(Enumerable) || params[:yield_all]
118
116
 
@@ -132,7 +130,7 @@ module Iteraptor
132
130
  λ = Proc.new
133
131
 
134
132
  map do |k, v|
135
- p = [parent, k].compact.join(DELIMITER)
133
+ p = [parent, k].compact.join(params[:delimiter] || DELIMITER)
136
134
 
137
135
  k, v = yield p, [k, v] if !v.is_a?(Enumerable) || params[:yield_all]
138
136
 
@@ -179,7 +177,7 @@ module Iteraptor
179
177
 
180
178
  plough = method ? :none? : :any?
181
179
  aplanar.each_with_object({}) do |(key, value), acc|
182
- to_match = key.split(DELIMITER)
180
+ to_match = key.split(params[:delimiter] || DELIMITER)
183
181
  to_match = to_match.flat_map { |k| [k.to_s, k.to_s.to_sym] } if params[:soft_keys]
184
182
 
185
183
  next if filter.public_send(plough, &->(f){ to_match.any?(&f.method(:===)) })
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iteraptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Matiushkin