hash_wia 0.7.9 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa2a2ed3249abb84825df2511879f1c22623dd8f9ec05fb4fa6f37936a9cf081
4
- data.tar.gz: 204b0273b5e6ffd7f79f2d7191949da08afe1308fcad30636199b26616efc265
3
+ metadata.gz: 47e5d1296ecbfb525c7f42577fc5b4206384c6a39a28d7ad85b277f208e6f779
4
+ data.tar.gz: 224acde315b8df0b2d04a606ee866261f5af52f066c74b39119ee2af8e901ea9
5
5
  SHA512:
6
- metadata.gz: 498de3720f21412cde5b33db25f84c5c82b24fa6888030c16b86d694a31d9496f2f8437dea87026c23befa741527a2baf769708f01ffb08c0ee3444b2755f5fb
7
- data.tar.gz: 2f6df1a68b560de8d3e011da5661a860de31e19486a79d90bb8876d04f27e32058e144c34dcd662f763d2c001d269ee00232978bf388fa5c1af53275a3ea563c
6
+ metadata.gz: cbafc17a9cfdcc4425388ee8c7cb8314c2e1fa1d379ab06a15b210e007dc8a8cb9185479a5bc9ddb3b158090e31d2eed3b1ccf7fed32fca14bf54be6f75ca2dc
7
+ data.tar.gz: 3ce638343e78ea595620113d1c632cfaf5b6bcb962a9c95d4c42a3728c10457a286d04243be5aca208356a8bc1fc964ca2528ef78964624511d75fc978d850eb
data/.version CHANGED
@@ -1,2 +1 @@
1
- 0.7.9
2
-
1
+ 0.8.0
@@ -4,13 +4,15 @@ end
4
4
 
5
5
  class HashWia
6
6
  class NamedOptions
7
- def initialize hash
8
- @hash = hash
7
+ def initialize hash, &block
8
+ @hash = hash
9
+ @block = block
9
10
  end
10
11
 
11
- def set constant, code, name
12
+ def set constant, code, value
13
+ @block.call constant.to_s, code, value
12
14
  @hash[constant.to_s] = code
13
- @hash[code] = name.to_s
15
+ @hash[code] = value
14
16
  end
15
17
 
16
18
  def method_missing code, key_val
@@ -28,21 +30,37 @@ end
28
30
  # # or
29
31
  # opt.set 'DONE', :d, 'Done'
30
32
  # end
31
- def HashWia klass = nil, name = nil, opts = nil
33
+ def HashWia klass = nil, opts = nil
32
34
  if block_given?
33
35
  hash = HashWia.new
34
36
 
35
- if name
36
- if !opts || opts[:constant] != false
37
- constant = name.to_s.upcase
38
- klass.const_set constant, hash
39
- end
37
+ if klass.class == Hash
38
+ opts = klass
39
+ klass = nil
40
+ end
41
+
42
+ opts ||= {}
40
43
 
41
- klass.define_singleton_method(name) { klass.const_get(constant) }
44
+ named_opts = HashWia::NamedOptions.new hash do |constant, code, value|
45
+ if opts[:constants]
46
+ if klass
47
+ klass.const_set "#{opts[:constants]}_#{constant}".upcase, code
48
+ else
49
+ raise "Host class not given (call as HashWia self, constants: ...)"
50
+ end
51
+ end
42
52
  end
43
53
 
44
- named_opts = HashWia::NamedOptions.new hash
45
54
  yield named_opts
55
+
56
+ if opts[:method]
57
+ klass.define_singleton_method(opts[:method]) { hash }
58
+ end
59
+
60
+ unless opts[:freeze] == false
61
+ hash.freeze
62
+ end
63
+
46
64
  hash
47
65
  else
48
66
  raise ArgumentError, 'Block not provided'
@@ -26,8 +26,8 @@ module HashWiaModule
26
26
  end
27
27
 
28
28
  def []= key, value
29
- if @frozen_keys
30
- raise NoMethodError, "Hash keys are frozen and can't be modified"
29
+ if @frozen_keys && !keys.include?(key)
30
+ raise FrozenError, "HashWia keys are frozen and can't be modified (key: #{key})"
31
31
  end
32
32
 
33
33
  delete key
@@ -16,6 +16,8 @@ class Hash
16
16
  list.each do |k|
17
17
  o[k] = self[k]
18
18
  end
19
+
20
+ o.freeze_keys!
19
21
  end
20
22
  end
21
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_wia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.9
4
+ version: 0.8.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: 2022-02-05 00:00:00.000000000 Z
11
+ date: 2022-02-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Gem provides simple access to common Ruby hash types bundled in one simple
14
14
  class