object_flatten 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3263780559edc43308e4f14129120e97f638266c
4
- data.tar.gz: 44bd49d64126aceb160b262a3bd34851a219a62e
3
+ metadata.gz: 330f41e3b644c51dd35dffc7c853afa1f8e05b31
4
+ data.tar.gz: 655aca8b572fdb8393815194b91cc9bdd828d9ac
5
5
  SHA512:
6
- metadata.gz: ed4cf0e9399032894746656d1a2d0579632e1db07c26c7b9af0a72c39e34ad2d0d1bab68eecdc9d29e106f0cca627e29092f3e1ecf28234a2f5434bd1102292f
7
- data.tar.gz: cf7e9d97f143d00c01f661bdc907db842d4a21239207cd8d461f9dc3406efa3dfcad5e3ad7a2658398b7488dd949df7a20016768f581db1f3a2d3b683cf939a4
6
+ metadata.gz: 9e4f5c52e394516067cc3b0a9099aea9ad4ca675dea0cdebd3ce99e51ae4385390e4db86960fa586f87580c6c31685f08cc541b3615dfc08a1dd2faeeafe3527
7
+ data.tar.gz: 6f7fd409b8b9125eca130910a8b2c816449901947eb3cab8c2d4c1cae51780d12a6e9fa3483db11157ecd755be16828a3696e38d73e8e42c9d01b94cba5b1eeb
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
2
  --color
3
+ --require spec_helper
data/README.md CHANGED
@@ -42,12 +42,17 @@ ObjectFlatten.flatten({"foo"=>{"bar1"=>"zoo", "bar2"=>"baz"}})
42
42
  # {"foo.bar2"=>"baz"}
43
43
  # ]
44
44
 
45
- ObjectFlatten.flatten({"foo"=>{"bar1"=>"zoo", "bar2"=>"baz"}}, '/')
45
+ ObjectFlatten.flatten({"foo"=>{"bar1"=>"zoo", "bar2"=>"baz"}}, separator: '/')
46
46
  #=> [
47
47
  # {"foo/bar1"=>"zoo"},
48
48
  # {"foo/bar2"=>"baz"}
49
49
  # ]
50
50
 
51
+ ObjectFlatten.flatten({"foo bar"=>{"zoo"=>"baz"}}, tr: [' ', '_'])
52
+ #=> [
53
+ # {"foo_bar.zoo"=>"baz"}
54
+ # ]
55
+
51
56
  ObjectFlatten.flatten({
52
57
  "foo1"=>{"bar1"=>"zoo", "bar2"=>"baz"},
53
58
  "foo2"=>{"bar"=>["zoo", "baz"], "zoo"=>"baz"}
@@ -1,24 +1,28 @@
1
1
  class ObjectFlatten
2
2
  class << self
3
- def flatten(obj, sep = '.')
3
+ def flatten(obj, options = {})
4
4
  unless obj.is_a?(Hash)
5
5
  raise TypeError, "TypeError: wrong argument type #{obj.class} (expected Hash)"
6
6
  end
7
7
 
8
- flatten0(nil, obj, sep.to_s)
8
+ options = {
9
+ :separator => '.',
10
+ }.merge(options)
11
+
12
+ flatten0(nil, obj, options)
9
13
  end
10
14
 
11
15
  private
12
16
 
13
- def flatten0(root, obj, sep, acc = [])
17
+ def flatten0(root, obj, options, acc = [])
14
18
  case obj
15
19
  when Array
16
20
  obj.each do |value|
17
- flatten0(root, value, sep, acc)
21
+ flatten0(root, value, options, acc)
18
22
  end
19
23
  when Hash
20
24
  obj.each do |key, value|
21
- flatten0(join(root, key, sep), value, sep, acc)
25
+ flatten0(join(root, key, options), value, options, acc)
22
26
  end
23
27
  else
24
28
  acc << {root => obj}
@@ -27,9 +31,16 @@ class ObjectFlatten
27
31
  acc
28
32
  end
29
33
 
30
- def join(root, key, sep)
34
+ def join(root, key, options)
31
35
  key = key.to_s
32
- root ? root + sep + key : key
36
+ sep = options[:separator]
37
+ tr = options[:tr]
38
+
39
+ if tr
40
+ key = key.tr(*tr)
41
+ end
42
+
43
+ root ? (root + sep + key) : key
33
44
  end
34
45
  end
35
46
  end
@@ -1,3 +1,3 @@
1
1
  class ObjectFlatten
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_flatten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara