dot_hash 1.1.3 → 1.1.5

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: 2d5f5b5bc01c1658cb038945ccda5d6338021934
4
- data.tar.gz: a138603a469cafba4ca1d925546a993d30d91a50
3
+ metadata.gz: 204c2956dd13177e9bda794e4b1f335fa2e86f15
4
+ data.tar.gz: 8d194f674b666d999e7435c2c4ba6dc8d32dbe85
5
5
  SHA512:
6
- metadata.gz: d08a88ae42624709175af9b7566ca88f786a680e88e255c28c587049f8f538c655dda4d1378fb17d93d21849bd5b989a036cda2f54f7b0d6ca061640a27df76e
7
- data.tar.gz: 718c33b9a6031bceac6f2cdbd382174236c4da920b500027699fee040f3c643f4834231fd190f6b8d05685c6cfb280c3e98ff0570f5227f0a59583d2a6cb387e
6
+ metadata.gz: 11a3a166c97133802ce7f236bd5755460fec4fe17d20c2c4fe297baf63498aa270875a0a67abdca3d60fc2b589918f788bd0e2c404d9f4824d769d20d5b22b9d
7
+ data.tar.gz: 74c27217e04d9037b129ee265b71a3022d94db8d693ebae4943a0714c15844cc8bc2cbbc359178ef548c47c49399e1be2df7ff9187155666a7611080cdaf5a38
@@ -20,7 +20,7 @@ module DotHash
20
20
  end
21
21
 
22
22
  def ==(other)
23
- super || other.to_hash == hash
23
+ super(other) or other.to_hash == hash
24
24
  end
25
25
 
26
26
  def to_s
@@ -32,10 +32,16 @@ module DotHash
32
32
  end
33
33
 
34
34
  def [](key)
35
- get_value(key)
35
+ fetch(key, nil)
36
36
  end
37
37
 
38
- private
38
+ def fetch(key, *args, &block)
39
+ key = hash.has_key?(key.to_s) ? key.to_s : key.to_sym
40
+ value = hash.fetch(key, *args, &block)
41
+
42
+ return value unless value.is_a?(Hash)
43
+ hash[key] = self.class.new value
44
+ end
39
45
 
40
46
  def has_key?(key)
41
47
  hash.has_key?(key.to_s) or
@@ -43,8 +49,10 @@ module DotHash
43
49
  hash.respond_to?(key)
44
50
  end
45
51
 
52
+ private
53
+
46
54
  def execute(key, *args, &block)
47
- get_value(key) do
55
+ fetch(key) do
48
56
  hash.public_send(key, *args) do |*values|
49
57
  block.call(*hashify_args(values))
50
58
  end
@@ -52,23 +60,14 @@ module DotHash
52
60
  end
53
61
 
54
62
  def hashify_args(args)
55
- args.map do |a|
56
- if a.is_a?(Hash)
57
- self.class.new(a)
58
- elsif a.is_a?(Array)
59
- hashify_args(a)
60
- else
61
- a
62
- end
63
+ args.each_with_index do |a, i|
64
+ args[i] =
65
+ case a
66
+ when Hash then self.class.new(a)
67
+ when Array then hashify_args(a)
68
+ else a
69
+ end
63
70
  end
64
71
  end
65
-
66
- def get_value(key, &fallback)
67
- key = hash.has_key?(key.to_s) ? key.to_s : key.to_sym
68
- value = hash.fetch(key) { fallback ? fallback.call : nil }
69
-
70
- return value unless value.is_a?(Hash)
71
- hash[key] = self.class.new value
72
- end
73
72
  end
74
73
  end
@@ -1,3 +1,3 @@
1
1
  module DotHash
2
- VERSION = "1.1.3"
2
+ VERSION = "1.1.5"
3
3
  end
@@ -74,6 +74,14 @@ module DotHash
74
74
 
75
75
  assert_equal result, Properties.new({ninja: 'Naruto'})
76
76
  end
77
+
78
+ it '#each_with_object goes nested on on array' do
79
+ result = properties.each_with_object([]) do |(k, v), acc|
80
+ acc.push v.name
81
+ end
82
+
83
+ assert_equal result, ['Naruto']
84
+ end
77
85
  end
78
86
  end
79
87
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dot_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Eden