dot_hash 1.1.5 → 1.1.6

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: 204c2956dd13177e9bda794e4b1f335fa2e86f15
4
- data.tar.gz: 8d194f674b666d999e7435c2c4ba6dc8d32dbe85
3
+ metadata.gz: 3a348ba75c80c353302b1857142a1c41c4e7cee8
4
+ data.tar.gz: 9e88b56233a96201a90f0bb6052659fc9188949d
5
5
  SHA512:
6
- metadata.gz: 11a3a166c97133802ce7f236bd5755460fec4fe17d20c2c4fe297baf63498aa270875a0a67abdca3d60fc2b589918f788bd0e2c404d9f4824d769d20d5b22b9d
7
- data.tar.gz: 74c27217e04d9037b129ee265b71a3022d94db8d693ebae4943a0714c15844cc8bc2cbbc359178ef548c47c49399e1be2df7ff9187155666a7611080cdaf5a38
6
+ metadata.gz: 52ac95b9d2bd388bcb773c032fa2553f0f73b8a127585983d9d22758a5b12ca88b8a9e02af5e212de3759642a45570fee6b0197908b61f8a4e98e9b3173b0d3e
7
+ data.tar.gz: 7a83b68221204451034086766eb55910a49f9c3b4ce3a83bcb3e605e9b93753261e77f9e264e606732040a2ec122ba02274d0b6a85c281d443c7a9a4ab2f483c
@@ -20,7 +20,8 @@ module DotHash
20
20
  end
21
21
 
22
22
  def ==(other)
23
- super(other) or other.to_hash == hash
23
+ super(other) ||
24
+ (other.respond_to?(:to_hash) && other.to_hash == hash)
24
25
  end
25
26
 
26
27
  def to_s
@@ -39,8 +40,14 @@ module DotHash
39
40
  key = hash.has_key?(key.to_s) ? key.to_s : key.to_sym
40
41
  value = hash.fetch(key, *args, &block)
41
42
 
42
- return value unless value.is_a?(Hash)
43
- hash[key] = self.class.new value
43
+ if value.nil? or hash.respond_to?(key)
44
+ hashify_item value
45
+ elsif cached.has_key?(key)
46
+ value
47
+ else
48
+ cached[key] = true
49
+ hash[key] = hashify_item(value)
50
+ end
44
51
  end
45
52
 
46
53
  def has_key?(key)
@@ -51,22 +58,33 @@ module DotHash
51
58
 
52
59
  private
53
60
 
61
+ def cached
62
+ @cached ||= self.class.new({})
63
+ end
64
+
54
65
  def execute(key, *args, &block)
55
66
  fetch(key) do
56
- hash.public_send(key, *args) do |*values|
57
- block.call(*hashify_args(values))
67
+ if block
68
+ hash.public_send(key, *args) do |*values|
69
+ block.call(*hashify_list(values))
70
+ end
71
+ else
72
+ hash.public_send(key, *args)
58
73
  end
59
74
  end
60
75
  end
61
76
 
62
- def hashify_args(args)
77
+ def hashify_list(args)
63
78
  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
79
+ args[i] = hashify_item(a)
80
+ end
81
+ end
82
+
83
+ def hashify_item(item)
84
+ case item
85
+ when Hash then self.class.new(item)
86
+ when Array then hashify_list(item)
87
+ else item
70
88
  end
71
89
  end
72
90
  end
@@ -1,3 +1,3 @@
1
1
  module DotHash
2
- VERSION = "1.1.5"
2
+ VERSION = "1.1.6"
3
3
  end
@@ -54,13 +54,35 @@ module DotHash
54
54
  end
55
55
  end
56
56
 
57
- describe 'method delegation' do
57
+ describe 'with method delegation' do
58
58
  before do
59
59
  @properties = Properties.new({
60
60
  ninja: {name: 'Naruto'}
61
61
  })
62
62
  end
63
63
 
64
+ it 'does not cache method calls' do
65
+ assert_equal(
66
+ properties.map { |k, v| v },
67
+ [{name: 'Naruto'}]
68
+ )
69
+
70
+ assert_equal(
71
+ properties.map { |k, v| {k => v.name} },
72
+ [{ninja: 'Naruto'}]
73
+ )
74
+ end
75
+
76
+ it '#merge! without crashing' do
77
+ properties.merge!({hero: 'One Punch'})
78
+ properties.merge!({hero: 'One Punch 2'})
79
+
80
+ assert_equal(properties, {
81
+ hero: 'One Punch 2',
82
+ ninja: {name: 'Naruto'}
83
+ })
84
+ end
85
+
64
86
  it '#map sets inner-hashes to Properties' do
65
87
  result = properties.map { |k, v| "#{k}: #{v.name}" }
66
88
 
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.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Eden