clean-hash 0.4.1 → 0.5.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 +5 -2
- data/lib/clean-hash/base.rb +13 -99
- data/lib/clean-hash/hash_pollute.rb +4 -25
- data/lib/clean-hash/types/indifferent_type.rb +73 -0
- data/lib/clean-hash/{mutex_hash.rb → types/mutex_type.rb} +0 -0
- data/lib/clean-hash/types/safe_type.rb +13 -0
- data/lib/clean-hash/types/strict_type.rb +21 -0
- data/lib/clean-hash/types/struct_type.rb +18 -0
- metadata +7 -4
- data/lib/clean-hash/opts_hash.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bca462b038bfca67047b36bb224811ddc6e0bd479418c015b6fb4db81aa27a5f
|
4
|
+
data.tar.gz: 12cd3f411a29764539e7f4056a37edb1b7aecb9fb6b10a3c074c4f828f9c3e8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e79d1c984d8afa266df0bb1b38d8c0c868d5f1446d97c37887568766156273f009a8ae22b900e12e3188ea5af9aff5c1de574fc583db463d1de725358a241e1b
|
7
|
+
data.tar.gz: 7073960b6690ab77393f8612ff134d26db236149d66ad9934d1b4b04dcc2502522c0ab2eebeb4daf87a2504c8c0a559e450633f5d5a5f664d8ae9ab4086effdc
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/lib/clean-hash.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'hashie'
|
2
2
|
|
3
3
|
require_relative 'clean-hash/base'
|
4
|
+
require_relative 'clean-hash/types/indifferent_type'
|
5
|
+
require_relative 'clean-hash/types/mutex_type'
|
6
|
+
require_relative 'clean-hash/types/safe_type'
|
7
|
+
require_relative 'clean-hash/types/strict_type'
|
8
|
+
require_relative 'clean-hash/types/struct_type'
|
4
9
|
require_relative 'clean-hash/deep_merge'
|
5
|
-
require_relative 'clean-hash/mutex_hash'
|
6
|
-
require_relative 'clean-hash/opts_hash'
|
7
10
|
require_relative 'clean-hash/hash_pollute'
|
8
11
|
|
data/lib/clean-hash/base.rb
CHANGED
@@ -1,104 +1,18 @@
|
|
1
1
|
class CleanHash
|
2
|
-
def
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
case mode
|
2
|
+
def self.create type, data={}
|
3
|
+
case type
|
4
|
+
when :indifferent
|
5
|
+
::CleanHash::Indifferent.new data
|
7
6
|
when :safe
|
8
|
-
|
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
|
9
14
|
else
|
10
|
-
|
15
|
+
raise ArgumentError, 'Unsupported type: %s' % type
|
11
16
|
end
|
12
17
|
end
|
13
|
-
|
14
|
-
# for debug
|
15
|
-
def to_json
|
16
|
-
JSON.pretty_generate @data
|
17
|
-
end
|
18
|
-
|
19
|
-
# for puts
|
20
|
-
def to_ary
|
21
|
-
[@data]
|
22
|
-
end
|
23
|
-
|
24
|
-
def [] key
|
25
|
-
data = @data[key.to_sym]
|
26
|
-
data = @data[key.to_s] if data.nil?
|
27
|
-
|
28
|
-
if data.nil?
|
29
|
-
if @is_strict && !keys.include?(key.to_sym)
|
30
|
-
raise NoMethodError, 'Clean hash: key "%s" not found (%s)' % [key, keys]
|
31
|
-
else
|
32
|
-
nil
|
33
|
-
end
|
34
|
-
elsif data.is_a?(Hash)
|
35
|
-
CleanHash.new data, @mode
|
36
|
-
else
|
37
|
-
data
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def []= key, value
|
42
|
-
key = key.to_sym unless key.class == Symbol
|
43
|
-
|
44
|
-
__error_set if @is_strict && !key?(key)
|
45
|
-
|
46
|
-
if @is_safe
|
47
|
-
value = value.to_s if value.is_a?(Symbol)
|
48
|
-
|
49
|
-
unless value.nil? || value.is_a?(Hash) || value.is_a?(Numeric) || value.is_a?(String)
|
50
|
-
raise ArgumentError.new('Unsupported safe type: %s' % value.class)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
@data.delete(key.to_s)
|
55
|
-
|
56
|
-
@data[key] = value
|
57
|
-
end
|
58
|
-
|
59
|
-
def key? name
|
60
|
-
self[name]
|
61
|
-
true
|
62
|
-
rescue NoMethodError
|
63
|
-
false
|
64
|
-
end
|
65
|
-
|
66
|
-
def keys
|
67
|
-
@data.keys
|
68
|
-
end
|
69
|
-
|
70
|
-
def values
|
71
|
-
@data.values
|
72
|
-
end
|
73
|
-
|
74
|
-
def to_h
|
75
|
-
@data
|
76
|
-
end
|
77
|
-
|
78
|
-
def to_json *args
|
79
|
-
@data.to_json *args
|
80
|
-
end
|
81
|
-
|
82
|
-
def method_missing name, *args
|
83
|
-
m = name.to_s
|
84
|
-
|
85
|
-
if m.end_with?('=')
|
86
|
-
m = m.sub('=', '')
|
87
|
-
self[m] = args.first
|
88
|
-
elsif m.end_with?('?')
|
89
|
-
begin
|
90
|
-
!self[m.sub('?', '')].nil?
|
91
|
-
rescue NoMethodError
|
92
|
-
false
|
93
|
-
end
|
94
|
-
else
|
95
|
-
self[m]
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
private
|
100
|
-
|
101
|
-
def __error_set
|
102
|
-
raise NoMethodError, 'Clean hash: setting a key value is not allowedß'
|
103
|
-
end
|
104
|
-
end
|
18
|
+
end
|
@@ -14,34 +14,13 @@ class CleanHash
|
|
14
14
|
|
15
15
|
def to_ch mode=nil
|
16
16
|
if mode.is_a?(Array)
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
for key in mode
|
22
|
-
self[key] = nil unless key?(key)
|
23
|
-
end
|
24
|
-
|
25
|
-
::CleanHash::OptsHash.new self
|
17
|
+
mode.each { |el| self[el] = nil if self[el].nil? }
|
18
|
+
::CleanHash.create_struct self
|
26
19
|
else
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
if mode == :mutex
|
31
|
-
::CleanHash::MutexHash.new self
|
32
|
-
else
|
33
|
-
::CleanHash.new self, mode
|
34
|
-
end
|
20
|
+
mode ||= :indifferent
|
21
|
+
::CleanHash.create mode, self
|
35
22
|
end
|
36
23
|
end
|
37
24
|
end
|
38
|
-
|
39
|
-
::Array.class_eval do
|
40
|
-
# coverts keys to empty hash methods and returns read_only hash
|
41
|
-
def to_ch strict=false
|
42
|
-
hash = inject({}) { |h, key| h[key] = nil; h }
|
43
|
-
::CleanHash::OptsHash.new hash
|
44
|
-
end
|
45
|
-
end
|
46
25
|
end
|
47
26
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
class CleanHash
|
2
|
+
class Indifferent
|
3
|
+
def initialize data
|
4
|
+
@data = data
|
5
|
+
end
|
6
|
+
|
7
|
+
# for debug
|
8
|
+
def to_json
|
9
|
+
JSON.pretty_generate @data
|
10
|
+
end
|
11
|
+
|
12
|
+
# for puts
|
13
|
+
def to_ary
|
14
|
+
[@data]
|
15
|
+
end
|
16
|
+
|
17
|
+
def key? name
|
18
|
+
@data.key?(name.to_sym) || @data.key?(name.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
def keys
|
22
|
+
@data.keys
|
23
|
+
end
|
24
|
+
|
25
|
+
def values
|
26
|
+
@data.values
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_h
|
30
|
+
@data
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_json *args
|
34
|
+
@data.to_json *args
|
35
|
+
end
|
36
|
+
|
37
|
+
def [] key
|
38
|
+
data = @data[key.to_sym]
|
39
|
+
data = @data[key.to_s] if data.nil?
|
40
|
+
|
41
|
+
if data.is_a?(Hash)
|
42
|
+
self.class.new data
|
43
|
+
else
|
44
|
+
data
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def []= key, value
|
49
|
+
key = key.to_sym unless key.class == Symbol
|
50
|
+
@data.delete(key.to_s)
|
51
|
+
@data[key] = value
|
52
|
+
end
|
53
|
+
|
54
|
+
def method_missing name, *args, &block
|
55
|
+
m = name.to_s
|
56
|
+
|
57
|
+
if m.end_with?('=')
|
58
|
+
m = m.sub('=', '')
|
59
|
+
self[m] = args.first
|
60
|
+
elsif m.end_with?('?')
|
61
|
+
begin
|
62
|
+
!self[m.sub('?', '')].nil?
|
63
|
+
rescue NoMethodError
|
64
|
+
false
|
65
|
+
end
|
66
|
+
elsif block
|
67
|
+
self[m] = block
|
68
|
+
else
|
69
|
+
self[m]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CleanHash
|
2
|
+
class Safe < Indifferent
|
3
|
+
def []= key, value
|
4
|
+
value = value.to_s if value.is_a?(Symbol)
|
5
|
+
|
6
|
+
unless value.nil? || value.is_a?(Hash) || value.is_a?(Numeric) || value.is_a?(String)
|
7
|
+
raise ArgumentError.new('Unsupported safe type: %s' % value.class)
|
8
|
+
end
|
9
|
+
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Type good for configurations
|
2
|
+
|
3
|
+
class CleanHash
|
4
|
+
class Strict < Indifferent
|
5
|
+
def [] key
|
6
|
+
_check_key_existance key
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def []= key, value
|
11
|
+
_check_key_existance key
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def _check_key_existance key
|
18
|
+
raise ArgumentError.new('Key not found: %s' % key) unless key?(key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Creates dynamic struct based on arguments
|
2
|
+
|
3
|
+
class CleanHash
|
4
|
+
STRUCTS = {}
|
5
|
+
|
6
|
+
def self.create_struct hash
|
7
|
+
raise ArgumentError.new('Not a hash') unless hash.is_a?(Hash)
|
8
|
+
|
9
|
+
name = 'DynStruct_' + hash.keys.join('_')
|
10
|
+
STRUCTS[name] ||= ::Struct.new(name, *hash.keys.sort)
|
11
|
+
|
12
|
+
STRUCTS[name].new.tap do |o|
|
13
|
+
hash.each do |k, v|
|
14
|
+
o.send('%s=' % k, v) unless v.nil?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clean-hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -35,9 +35,12 @@ files:
|
|
35
35
|
- "./lib/clean-hash/base.rb"
|
36
36
|
- "./lib/clean-hash/deep_merge.rb"
|
37
37
|
- "./lib/clean-hash/hash_pollute.rb"
|
38
|
-
- "./lib/clean-hash/mutex_hash.rb"
|
39
|
-
- "./lib/clean-hash/opts_hash.rb"
|
40
38
|
- "./lib/clean-hash/pollute.rb"
|
39
|
+
- "./lib/clean-hash/types/indifferent_type.rb"
|
40
|
+
- "./lib/clean-hash/types/mutex_type.rb"
|
41
|
+
- "./lib/clean-hash/types/safe_type.rb"
|
42
|
+
- "./lib/clean-hash/types/strict_type.rb"
|
43
|
+
- "./lib/clean-hash/types/struct_type.rb"
|
41
44
|
homepage: https://github.com/dux/clean-hash
|
42
45
|
licenses:
|
43
46
|
- MIT
|
data/lib/clean-hash/opts_hash.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
# @ivars = [:foo, :bar].to_ch
|
2
|
-
# @ivars = {foo: nil, bar: 2}.to_ch [:foo, :bar, :baz]
|
3
|
-
|
4
|
-
class CleanHash
|
5
|
-
class OptsHash
|
6
|
-
def initialize *hash
|
7
|
-
if hash.first.class == Hash
|
8
|
-
@hash = hash.first
|
9
|
-
else
|
10
|
-
@hash = hash.inject({}) { |h, el| h[el.to_sym] = nil; h }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def [] key
|
15
|
-
method_missing key
|
16
|
-
end
|
17
|
-
|
18
|
-
def []= key, value
|
19
|
-
method_missing '%s=' % key, value
|
20
|
-
end
|
21
|
-
|
22
|
-
def method_missing name, value=nil
|
23
|
-
name = name.to_s
|
24
|
-
is_set = !!name.sub!('=', '')
|
25
|
-
name = name.to_sym
|
26
|
-
|
27
|
-
raise NoMethodError.new('Key %s not found' % name) unless @hash.has_key?(name)
|
28
|
-
|
29
|
-
if is_set
|
30
|
-
@hash[name] = value
|
31
|
-
else
|
32
|
-
@hash[name]
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def to_h
|
37
|
-
@hash.dup
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|