hash-normalizekeys 1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +38 -0
  3. data/lib/hash/normalizekeys.rb +154 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f79ed0592697fd0613c1912b7c3c0bbe495525155eea2a4e00e69ce59685ed78
4
+ data.tar.gz: bdba8e1c44ecea82fa8f18c01fd8d95a1757d3b0ab3742ac9fcd82f1dd2a90cb
5
+ SHA512:
6
+ metadata.gz: 7502d8e23034bb00305c17915b477e02e2cd815fb20e3e7557478dc0d14cb9cb93e39eecef44851faf33ce54ec263807539bac779a54af11576e33c2c98c4251
7
+ data.tar.gz: abdba210fbd7914a805afd18b3c2c2c45e439200be0a87a1a722bc2d95942e13cdc9d9406ce99120d4dfc5e9f952ad9c80926ff0fbfcc52af1c08f6e3ca8cae1
@@ -0,0 +1,38 @@
1
+ # StringKeys
2
+
3
+ A StringKeys object works like a hash, except keys which are not strings are
4
+ converted to strings.
5
+
6
+ ```ruby
7
+ require 'stringkeys'
8
+ sk = StringKeys.new
9
+ sk[:name] = 'Fred'
10
+ sk.keys[0].class #=> String
11
+ ```
12
+
13
+ The idea is simple. When a key comes in, whether for `[]` or `[]=`, if it is
14
+ not a string, then its `.to_s` method is called and the result is used for the
15
+ key. So the following lines produce identical results:
16
+
17
+ ```ruby
18
+ sk[MyClass] = 1
19
+ sk[:MyClass] = 1
20
+ sk['MyClass'] = 1
21
+ ```
22
+
23
+ # Installation
24
+
25
+ ```
26
+ gem install stringkeys
27
+ ```
28
+
29
+ ## Author
30
+
31
+ Mike O'Sullivan
32
+ mike@idocs.com
33
+
34
+ ## History
35
+
36
+ | version | date | notes |
37
+ |---------|-------------|-------------------------------------------------------|
38
+ | 1.0 | Mar 1, 2020 | Initial upload. |
@@ -0,0 +1,154 @@
1
+ #===============================================================================
2
+ # Hash::NormalizeKeys
3
+ #
4
+ class Hash::NormalizeKeys
5
+ # version
6
+ VERSION = '1.3'
7
+
8
+ # new() takes the same params as Hash.new.
9
+ def initialize(*opts, &block)
10
+ @hsh = Hash.new(*opts, &block)
11
+ end
12
+
13
+ # Returns the underlying hash's respond_to? method.
14
+ def respond_to?(m)
15
+ return @hsh.respond_to?(m)
16
+ end
17
+
18
+ # Returns a new NormalizeKeys object by combining this object and the other
19
+ # object, with keys being normalized.
20
+ def merge(other)
21
+ rv = self.class.new
22
+
23
+ # initialize with own elements
24
+ self.each do |k, v|
25
+ rv[k] = v
26
+ end
27
+
28
+ # merge in other's elements
29
+ other.each do |k, v|
30
+ rv[k] = v
31
+ end
32
+
33
+ # return
34
+ return rv
35
+ end
36
+
37
+ # Merges other into the NormalizeKey object, with other's keys being normalized.
38
+ def merge!(other)
39
+ # merge in other's elements
40
+ other.each do |k, v|
41
+ self[k] = v
42
+ end
43
+
44
+ # return
45
+ return self
46
+ end
47
+
48
+ # works just like merge!
49
+ alias_method :update, :merge!
50
+
51
+ # Works like Hash#replace, with keys being normalized.
52
+ def replace(other)
53
+ # reset @hsh
54
+ @hsh.clear
55
+
56
+ # merge in other's elements
57
+ other.each do |k, v|
58
+ self[k] = v
59
+ end
60
+
61
+ # return
62
+ return self
63
+ end
64
+
65
+ # Works just like Hash#slice, except the key values are normalized.
66
+ def slice(*org_keys)
67
+ use_keys = {}
68
+
69
+ org_keys.each do |org_key|
70
+ use_keys[self.class.normalize_key(org_key)] = nil
71
+ end
72
+
73
+ return @hsh.slice(*use_keys.keys)
74
+ end
75
+
76
+ # Returns the results of the underlying hash's to_s method.
77
+ def to_s
78
+ return @hsh.to_s
79
+ end
80
+
81
+ # Works like Hash#invert, except the new object is a NormalizeKey object and
82
+ # the keys have been normalized.
83
+ def invert
84
+ rv = self.class.new
85
+
86
+ self.each do |k, v|
87
+ rv[v] = k
88
+ end
89
+
90
+ return rv
91
+ end
92
+
93
+ # Works like Hash#values_at, except the key values are normalized.
94
+ def values_at(*keys)
95
+ rv = []
96
+
97
+ keys.each do |key|
98
+ rv.push self[key]
99
+ end
100
+
101
+ return rv
102
+ end
103
+
104
+ # private
105
+ private
106
+
107
+ # a list of methods that need the first param normalized
108
+ COERCES = %w(
109
+ [] []=
110
+ include? has_key? key? member?
111
+ fetch delete assoc dig store);
112
+
113
+ # All methods not already defined are passed to @hsh. If the method is
114
+ # listed in COERCES then the first arg is normalized.
115
+ def method_missing(m, *args, &block)
116
+ if COERCES.include?(m.to_s) and (args.length > 0)
117
+ args = *args
118
+ args[0] = self.class.normalize_key(args[0])
119
+ end
120
+
121
+ return @hsh.send(m, *args, &block)
122
+ end
123
+
124
+ # normalize_key
125
+ def self.normalize_key(key)
126
+ raise 'override-normalize_key'
127
+ end
128
+ end
129
+ #
130
+ # Hash::NormalizeKeys
131
+ #===============================================================================
132
+
133
+
134
+ #===============================================================================
135
+ # Hash::NormalizeKeys::String
136
+ #
137
+
138
+ # An implementation of Hash::NormalizeKeys in which the keys are normalized
139
+ # to strings.
140
+ class Hash::NormalizeKeys::String < Hash::NormalizeKeys
141
+ private
142
+
143
+ # normalize_key
144
+ def self.normalize_key(key)
145
+ if key.is_a?(String)
146
+ return key
147
+ else
148
+ key.to_s
149
+ end
150
+ end
151
+ end
152
+ #
153
+ # Hash::NormalizeKeys::String
154
+ #===============================================================================
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-normalizekeys
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.3'
5
+ platform: ruby
6
+ authors:
7
+ - Mike O'Sullivan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Hash in which all keys are strings
14
+ email: mike@idocs.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/hash/normalizekeys.rb
21
+ homepage: https://rubygems.org/gems/hash-normalizekeys
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.7.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Hash-like structure in which all keys are converted to strings
45
+ test_files: []