-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.rb
42 lines (34 loc) · 1.04 KB
/
sample.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require_relative './lib/named_hash'
blank = ~{}
config = ~{
"name": "Hampton",
deep: {
value: 123
}
}
# Indifferent access
config[:name] == "Hampton" #true
config["name"] == "Hampton" #true
config[:Name] == nil #true
config.is_a? Hash #true
config.instance_of? Hash #false
# Protects against invalid key types
config[1982] #raise NamedHash::InvalidKeyError
config[{}] #raise NamedHash::InvalidKeyError
config[OpenStruct.new] #raise NamedHash::InvalidKeyError
# Doesn't by default do a 'deep' protection
config["deep"].is_a?(NamedHash) #true
config["deep"][:value] #123
config["deep"]["value"] #null
config["deep"][1982] = true #true
# You can do a deep coercion by using ~~ either at hash-creation or calling on an existing NamedHash
# TODO: NOT YET IMPLEMENTED!!!!!!
config = ~~config
config["deep"][:value] #123
config["deep"]["value"] #123
config["deep"][1982] #raise NamedHash::InvalidKeyError
# Example way to use the object without fear of invalid key types
def initialize(options = {})
options = ~options
options[:living_legend] ||= true
end