clean-hash 0.4.0 → 0.4.1

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
  SHA256:
3
- metadata.gz: 45971a3560ed13e95d8f9b94230f7e1595b4e8c1244264cfe5f714d4a165c239
4
- data.tar.gz: 0fe785d5fc385985182f762779c3edfb3236250a0a5df2dbc058f8efaf803ecc
3
+ metadata.gz: c8da8615041dbba1d87e61ea20b29829ec35a5c6143020b2da7195cd39807dc1
4
+ data.tar.gz: 03e0e0588325dca5eb1ad9c61fbace8efc0541f7c46a54c669a9aa2b798bd9ea
5
5
  SHA512:
6
- metadata.gz: 43ee2cd7cec97d3bc6219c6402a8e7351d358b4bb7c8f2d2b8c0664d27a65b45f37c7396e8adf23755095431212bccfaacd1347279e60b9817566e785ff047cc
7
- data.tar.gz: 57dfc84f72665d1ee5a5738afaeea9a5c3bc2befe5292303542d7454f6a85f5d73e2c8afb0a24ca45362bb39bebe0ee6553e2abd65ace7b9a64bbaf3be56a873
6
+ metadata.gz: 16687111810848bf0fad3a7f00560621d96b0be2aadc6593536f1e11a9e787f25716a42ff1e92ac2efe41a6cb713d7ad3776dea72b897143a292717e21679a85
7
+ data.tar.gz: ebd5a5eafbf830059730a6b1e9c1a375f2a0e5cc194f6b4bee279b9d1bcaebe994d702d57226b48ce7b5f1e244f06343343c09115b28a7e774acf20e93a10194
data/.version CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -3,5 +3,6 @@ require 'hashie'
3
3
  require_relative 'clean-hash/base'
4
4
  require_relative 'clean-hash/deep_merge'
5
5
  require_relative 'clean-hash/mutex_hash'
6
+ require_relative 'clean-hash/opts_hash'
6
7
  require_relative 'clean-hash/hash_pollute'
7
8
 
@@ -27,7 +27,7 @@ class CleanHash
27
27
 
28
28
  if data.nil?
29
29
  if @is_strict && !keys.include?(key.to_sym)
30
- raise NoMethodError, 'Clean hash: key "%s" not found' % key
30
+ raise NoMethodError, 'Clean hash: key "%s" not found (%s)' % [key, keys]
31
31
  else
32
32
  nil
33
33
  end
@@ -22,7 +22,7 @@ class CleanHash
22
22
  self[key] = nil unless key?(key)
23
23
  end
24
24
 
25
- ::CleanHash.new self, :strict
25
+ ::CleanHash::OptsHash.new self
26
26
  else
27
27
  supported = %i{safe mutex strict}
28
28
  raise ArgumentError, 'Unsupported type "%s", supported: %s' % [mode, supported] if mode && !supported.include?(mode)
@@ -40,7 +40,7 @@ class CleanHash
40
40
  # coverts keys to empty hash methods and returns read_only hash
41
41
  def to_ch strict=false
42
42
  hash = inject({}) { |h, key| h[key] = nil; h }
43
- ::CleanHash.new hash, :strict
43
+ ::CleanHash::OptsHash.new hash
44
44
  end
45
45
  end
46
46
  end
@@ -0,0 +1,41 @@
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
+
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.0
4
+ version: 0.4.1
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-02-29 00:00:00.000000000 Z
11
+ date: 2020-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -36,6 +36,7 @@ files:
36
36
  - "./lib/clean-hash/deep_merge.rb"
37
37
  - "./lib/clean-hash/hash_pollute.rb"
38
38
  - "./lib/clean-hash/mutex_hash.rb"
39
+ - "./lib/clean-hash/opts_hash.rb"
39
40
  - "./lib/clean-hash/pollute.rb"
40
41
  homepage: https://github.com/dux/clean-hash
41
42
  licenses: