Module: NeoHash::Support
- Included in:
- NeoHash
- Defined in:
- lib/neo_hash/support.rb
Overview
Provides attribute readers and Hash features A class that includes this has ‘@h` as inner hash.
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/neo_hash/support.rb', line 51
def method_missing(method, *args, &block)
if Hash.public_method_defined?(method)
@h.send method, *args, &block
else
super
end
end
|
Instance Method Details
#[](key) ⇒ Object
20
21
22
|
# File 'lib/neo_hash/support.rb', line 20
def [](key)
@h[key.to_sym]
end
|
#[]=(key, value) ⇒ Object
25
26
27
28
29
|
# File 'lib/neo_hash/support.rb', line 25
def []=(key, value)
sk = key.to_sym
define_attr_accessor(sk) unless @h.key?(sk)
@h[sk] = convert_val(value)
end
|
#delete(key, &block) ⇒ Object
41
42
43
|
# File 'lib/neo_hash/support.rb', line 41
def delete(key, &block)
@h.delete(key.to_sym, &block)
end
|
#fetch(*args, &block) ⇒ Object
46
47
48
49
|
# File 'lib/neo_hash/support.rb', line 46
def fetch(*args, &block)
args[0] = args[0].to_sym
@h.fetch(*args, &block)
end
|
#include?(key) ⇒ Boolean
Also known as:
has_key?, key?, member?
32
33
34
|
# File 'lib/neo_hash/support.rb', line 32
def include?(key)
@h.include?(key.to_sym)
end
|
#to_h(opts = {}) ⇒ Hash
64
65
66
67
68
69
70
|
# File 'lib/neo_hash/support.rb', line 64
def to_h(opts = {})
symbolize = opts.fetch(:symbolize_keys, true)
@h.map {|key, val|
[symbolize ? key : key.to_s, to_primitive(val, opts)]
}.to_h
end
|
#to_hash ⇒ Hash
Implicitly convert to Hash
74
75
76
|
# File 'lib/neo_hash/support.rb', line 74
def to_hash
to_h
end
|