hash_wia 0.7.3 → 0.7.8
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 +4 -4
- data/.version +1 -1
- data/lib/hash_wia/class.rb +51 -0
- data/lib/hash_wia/module.rb +31 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21e40421e8ced866ff9c01722a96aa65ce053e1b390a20d72cb0787675b27a7f
|
4
|
+
data.tar.gz: df0fe8a0757afe5f4b1da1532f7be50a6dc74e2b0849e0daf8827364b544461e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cec9c611371fb8714af48b9167a26685a142bc6051a82b85d021d49b9fa45a21727094be6db1b2c9f39ebe408e3e1957ccb3cabfc1083d1ee47891791426820b
|
7
|
+
data.tar.gz: e0fdc27f40a2bb965194f50ca43d08b784cc0d5e1649a62879cd3deec5af4b4e61c597ba6a24703f3f136cafd091047e73da07b3499693bef2bd912b761ba496
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.8
|
data/lib/hash_wia/class.rb
CHANGED
@@ -1,3 +1,54 @@
|
|
1
1
|
class HashWia < Hash
|
2
2
|
include HashWiaModule
|
3
3
|
end
|
4
|
+
|
5
|
+
class HashWia
|
6
|
+
class NamedOptions
|
7
|
+
def initialize hash
|
8
|
+
@hash = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def set constant, code, name
|
12
|
+
if code.class == Symbol
|
13
|
+
code = code.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
@hash[constant.to_s] = code
|
17
|
+
@hash[code] = name.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing code, key_val
|
21
|
+
self.set code, key_val.keys.first, key_val.values.first
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# to create Task::STATUS and Task.status
|
27
|
+
# HashWia self, :status do |opt|
|
28
|
+
#
|
29
|
+
# or just to get a hash
|
30
|
+
# HashWia do |opt|
|
31
|
+
# opt.DONE d: 'Done'
|
32
|
+
# # or
|
33
|
+
# opt.set 'DONE', :d, 'Done'
|
34
|
+
# end
|
35
|
+
def HashWia klass = nil, name = nil, opts = nil
|
36
|
+
if block_given?
|
37
|
+
hash = HashWia.new
|
38
|
+
|
39
|
+
if name
|
40
|
+
if !opts || opts[:constant] != false
|
41
|
+
constant = name.to_s.upcase
|
42
|
+
klass.const_set constant, hash
|
43
|
+
end
|
44
|
+
|
45
|
+
klass.define_singleton_method(name) { klass.const_get(constant) }
|
46
|
+
end
|
47
|
+
|
48
|
+
named_opts = HashWia::NamedOptions.new hash
|
49
|
+
yield named_opts
|
50
|
+
hash
|
51
|
+
else
|
52
|
+
raise ArgumentError, 'Block not provided'
|
53
|
+
end
|
54
|
+
end
|
data/lib/hash_wia/module.rb
CHANGED
@@ -5,18 +5,31 @@ module HashWiaModule
|
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
|
+
# overload common key names with
|
9
|
+
%i(size length zip minmax store cycle chunk sum uniq chain).each do |el|
|
10
|
+
define_method el do
|
11
|
+
self[el]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
8
15
|
def [] key
|
9
16
|
data = super key
|
10
17
|
data = super key.to_s if data.nil?
|
11
18
|
data = super key.to_sym if key.respond_to?(:to_sym) && data.nil?
|
12
19
|
|
13
20
|
# if we are returning hash as a value, just include with wia methods hash
|
14
|
-
|
21
|
+
if data.is_a?(Hash)
|
22
|
+
data.extend HashWiaModule
|
23
|
+
end
|
15
24
|
|
16
25
|
data
|
17
26
|
end
|
18
27
|
|
19
28
|
def []= key, value
|
29
|
+
if @frozen_keys
|
30
|
+
raise NoMethodError, "Hash keys are frozen and can't be modified"
|
31
|
+
end
|
32
|
+
|
20
33
|
delete key
|
21
34
|
super key, value
|
22
35
|
end
|
@@ -51,6 +64,23 @@ module HashWiaModule
|
|
51
64
|
end
|
52
65
|
end
|
53
66
|
|
67
|
+
def freeze_keys!
|
68
|
+
@frozen_keys = true
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
def each &block
|
73
|
+
to_a.each do |key, data|
|
74
|
+
if data.is_a?(Hash)
|
75
|
+
data.extend HashWiaModule
|
76
|
+
end
|
77
|
+
|
78
|
+
block.call key, data
|
79
|
+
end
|
80
|
+
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
54
84
|
def method_missing name, *args, &block
|
55
85
|
strname = name.to_s
|
56
86
|
|
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.
|
4
|
+
version: 0.7.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-05 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
|
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '0'
|
43
43
|
requirements: []
|
44
|
-
rubygems_version: 3.
|
44
|
+
rubygems_version: 3.2.22
|
45
45
|
signing_key:
|
46
46
|
specification_version: 4
|
47
47
|
summary: Hash with indifferent access + goodies
|