clean-hash 0.5.11 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/clean-hash.rb +0 -3
- data/lib/clean-hash/hash_pollute.rb +2 -3
- data/lib/clean-hash/types/indifferent_type.rb +73 -63
- data/lib/clean-hash/types/safe_type.rb +1 -1
- metadata +1 -4
- data/lib/clean-hash/base.rb +0 -18
- data/lib/clean-hash/types/mutex_type.rb +0 -17
- data/lib/clean-hash/types/strict_type.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed7704c53ff11469472d74438b7c3b99a663e2c6226a930da13850425db16453
|
4
|
+
data.tar.gz: 4ecfc3fac7c9e3a957b11e3d89e2cf0f7d1bf468d5e8c5ca3ac6f2bc800f147d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d96dd9791146a09714230fed546573c53e0551ac65203a21e4dbd0da0f7b82841d6ccb5f3d6b09c3a71c6d052142bffda5da563b4a53e8aec668a741162bb66
|
7
|
+
data.tar.gz: 64d668f59ae76a6464f4e8109654350ea289df671d83ccb98ddf4f0a0b6983a35b4609203a8e7aa165d5db163f2882471943048bbe41228d19c6f78fc8a0633d
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/clean-hash.rb
CHANGED
@@ -1,10 +1,7 @@
|
|
1
1
|
require 'hashie'
|
2
2
|
|
3
|
-
require_relative 'clean-hash/base'
|
4
3
|
require_relative 'clean-hash/types/indifferent_type'
|
5
|
-
require_relative 'clean-hash/types/mutex_type'
|
6
4
|
require_relative 'clean-hash/types/safe_type'
|
7
|
-
require_relative 'clean-hash/types/strict_type'
|
8
5
|
require_relative 'clean-hash/types/struct_type'
|
9
6
|
require_relative 'clean-hash/deep_merge'
|
10
7
|
require_relative 'clean-hash/hash_pollute'
|
@@ -19,10 +19,9 @@ class CleanHash
|
|
19
19
|
end
|
20
20
|
|
21
21
|
mode.each { |el| self[el] = nil if self[el].nil? }
|
22
|
-
|
22
|
+
CleanHash.create_struct self
|
23
23
|
else
|
24
|
-
|
25
|
-
::CleanHash.create mode, self
|
24
|
+
CleanHash.new self
|
26
25
|
end
|
27
26
|
end
|
28
27
|
end
|
@@ -1,84 +1,94 @@
|
|
1
|
+
# raise error if accessing hash with method and key not found
|
2
|
+
# stict_hash = { foo: :bar }.to_ch :strict
|
3
|
+
# stict_hash[:not_found] # nil
|
4
|
+
# stict_hash.foo # :bar
|
5
|
+
# stict_hash.not_found # ArgumentError
|
6
|
+
|
1
7
|
class CleanHash
|
2
|
-
|
3
|
-
def initialize data
|
4
|
-
@data = data
|
5
|
-
end
|
8
|
+
MUTEX ||= Mutex.new
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
10
|
+
def initialize data
|
11
|
+
@data = data
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
# for debug
|
15
|
+
def to_json
|
16
|
+
JSON.pretty_generate @data
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
19
|
+
# for puts
|
20
|
+
def to_ary
|
21
|
+
[@data]
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
# overload key ruby method because it is common hash key name
|
25
|
+
# still accessible via to_h.key
|
26
|
+
def key
|
27
|
+
self[:key]
|
28
|
+
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
out.push @data.delete(key.to_s)
|
31
|
-
out.push @data.delete(key.to_sym) if key.respond_to?(:to_sym)
|
32
|
-
out.each { |el| return el unless el.nil? }
|
33
|
-
nil
|
34
|
-
end
|
30
|
+
def key? name
|
31
|
+
@data.key?(name) || @data.key?(name.to_s) || (name.respond_to?(:to_sym) ? @data.key?(name.to_sym) : nil)
|
32
|
+
end
|
35
33
|
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
def delete key
|
35
|
+
out = []
|
36
|
+
out.push @data.delete(key)
|
37
|
+
out.push @data.delete(key.to_s)
|
38
|
+
out.push @data.delete(key.to_sym) if key.respond_to?(:to_sym)
|
39
|
+
out.each { |el| return el unless el.nil? }
|
40
|
+
nil
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
+
def to_h
|
44
|
+
@data
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_json *args
|
48
|
+
@data.to_json *args
|
49
|
+
end
|
43
50
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
51
|
+
def [] key
|
52
|
+
data = @data[key]
|
53
|
+
data = @data[key.to_s] if data.nil? && !key.is_a?(String)
|
54
|
+
data = @data[key.to_sym] if data.nil? && key.respond_to?(:to_sym)
|
48
55
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
56
|
+
if data.is_a?(Hash)
|
57
|
+
self.class.new data
|
58
|
+
else
|
59
|
+
data
|
54
60
|
end
|
61
|
+
end
|
55
62
|
|
56
|
-
|
57
|
-
|
63
|
+
def []= key, value
|
64
|
+
delete key
|
65
|
+
MUTEX.synchronize do
|
58
66
|
@data[key] = value
|
59
67
|
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def method_missing name, *args, &block
|
71
|
+
return @data.send(name, *args, &block) if @data.respond_to?(name)
|
60
72
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
m =
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
self[m] = block
|
73
|
-
else
|
74
|
-
_method_missing_key m
|
75
|
-
end
|
73
|
+
m = name.to_s
|
74
|
+
|
75
|
+
if m.end_with?('=')
|
76
|
+
m = m.sub('=', '')
|
77
|
+
self[m] = args.first
|
78
|
+
elsif m.end_with?('?')
|
79
|
+
!self[m.sub('?', '')].nil?
|
80
|
+
elsif block
|
81
|
+
self[m] = block
|
82
|
+
else
|
83
|
+
_method_missing_key m
|
76
84
|
end
|
85
|
+
end
|
77
86
|
|
78
|
-
|
87
|
+
private
|
79
88
|
|
80
|
-
|
81
|
-
|
89
|
+
def _method_missing_key key
|
90
|
+
self[key].tap do |value|
|
91
|
+
raise ArgumentError.new('Key not found: %s' % key) if value.nil?
|
82
92
|
end
|
83
93
|
end
|
84
94
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clean-hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
@@ -32,14 +32,11 @@ extra_rdoc_files: []
|
|
32
32
|
files:
|
33
33
|
- "./.version"
|
34
34
|
- "./lib/clean-hash.rb"
|
35
|
-
- "./lib/clean-hash/base.rb"
|
36
35
|
- "./lib/clean-hash/deep_merge.rb"
|
37
36
|
- "./lib/clean-hash/hash_pollute.rb"
|
38
37
|
- "./lib/clean-hash/pollute.rb"
|
39
38
|
- "./lib/clean-hash/types/indifferent_type.rb"
|
40
|
-
- "./lib/clean-hash/types/mutex_type.rb"
|
41
39
|
- "./lib/clean-hash/types/safe_type.rb"
|
42
|
-
- "./lib/clean-hash/types/strict_type.rb"
|
43
40
|
- "./lib/clean-hash/types/struct_type.rb"
|
44
41
|
homepage: https://github.com/dux/clean-hash
|
45
42
|
licenses:
|
data/lib/clean-hash/base.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
class CleanHash
|
2
|
-
def self.create type, data={}
|
3
|
-
case type
|
4
|
-
when :indifferent
|
5
|
-
::CleanHash::Indifferent.new data
|
6
|
-
when :safe
|
7
|
-
::CleanHash::Safe.new data
|
8
|
-
when :strict
|
9
|
-
::CleanHash::Strict.new data
|
10
|
-
when :stuct
|
11
|
-
::CleanHash.create_strut data
|
12
|
-
when :mutex
|
13
|
-
::CleanHash::MutexHash.new data
|
14
|
-
else
|
15
|
-
raise ArgumentError, 'Unsupported type: %s' % type
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# Thread safe hash
|
2
|
-
class CleanHash
|
3
|
-
class MutexHash < Indifferent
|
4
|
-
MUTEX = Mutex.new
|
5
|
-
|
6
|
-
def []= name, value
|
7
|
-
MUTEX.synchronize do
|
8
|
-
@data[name] =
|
9
|
-
if value.is_a?(Hash)
|
10
|
-
self.class.new value
|
11
|
-
else
|
12
|
-
value
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
# Type good for configurations
|
2
|
-
|
3
|
-
class CleanHash
|
4
|
-
class Strict < Indifferent
|
5
|
-
private
|
6
|
-
|
7
|
-
# raise error if accessing hash with method and key not found
|
8
|
-
# stict_hash = { foo: :bar }.to_ch :strict
|
9
|
-
# stict_hash[:not_found] # nil
|
10
|
-
# stict_hash.foo # :bar
|
11
|
-
# stict_hash.not_found # ArgumentError
|
12
|
-
def _method_missing_key key
|
13
|
-
self[key].tap do |value|
|
14
|
-
raise ArgumentError.new('Key not found: %s' % key) if value.nil?
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|