named_hash 0.1.0

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/lib/hash.rb +6 -0
  3. data/lib/named_hash.rb +46 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8b53750ff1bd05f513b147b7bbd0c8333211cd699354df4b9c098b295fa21276
4
+ data.tar.gz: 215a9ff51aa2186c1fe1340eccd5afe95e017bbdd7768b3debae596ef515bf8c
5
+ SHA512:
6
+ metadata.gz: 9ed58ed53822adc2b2a661c7b6515853a657dffe8e84f49f615f6a8c303fdce81fa73154d6904cb4b8eea4f2bf1b99729311e69c53bd7d939a8e368a1917971a
7
+ data.tar.gz: a7b13635e8905e9a7c3f9bf466fa9b1946e9fa9bf4f88c2775dbe7900e9526bb4103d99bbcbb42a9ebed24de05125f2a7e0f9d445081f9b4e1d94f570ee41bdf
data/lib/hash.rb ADDED
@@ -0,0 +1,6 @@
1
+ class Hash
2
+ def to_named_hash
3
+ NamedHash.new(self)
4
+ end
5
+ alias_method :~, :to_named_hash
6
+ end
data/lib/named_hash.rb ADDED
@@ -0,0 +1,46 @@
1
+ class NamedHash < Hash
2
+ def initialize(hash = nil)
3
+ super
4
+ if hash
5
+ merge(hash)
6
+ end
7
+ end
8
+
9
+ def set(key, value)
10
+ check_key_type(key)
11
+ if value.is_a?(Hash) && !value.is_a?(NamedHash)
12
+ value = NamedHash.new(value)
13
+ end
14
+ native_set(key.to_s, value)
15
+ end
16
+ alias_method :native_set, :[]=
17
+ alias_method :[]=, :set
18
+
19
+ def [](key)
20
+ check_key_type(key)
21
+ super(key.to_s)
22
+ end
23
+
24
+ def merge(hash)
25
+ hash.each do |key, value|
26
+ set(key, value)
27
+ end
28
+ end
29
+
30
+ def to_named_hash
31
+ self
32
+ end
33
+ alias :~ :to_named_hash
34
+
35
+ class InvalidKeyError < StandardError
36
+ def initialize
37
+ super("Only Strings and Symbols are Valid")
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def check_key_type(key)
44
+ raise InvalidKeyError unless (key.is_a?(String) || key.is_a?(Symbol))
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: named_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hampton Lintorn-Catlin <hcatlin@gmail.com>
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A new take on HashWithIndifferentAccess
14
+ email: hcatlin@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/hash.rb
20
+ - lib/named_hash.rb
21
+ homepage: https://rubygems.org/gems/named_hash
22
+ licenses:
23
+ - MIT
24
+ metadata:
25
+ source_code_uri: https://github.com/HamptonMakes/named_hash
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.5.11
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Hashes for People
45
+ test_files: []