Class: HandyHash
- Inherits:
-
HashWithIndifferentAccess
- Object
- HashWithIndifferentAccess
- HandyHash
show all
- Defined in:
- lib/handy_hash.rb
Defined Under Namespace
Classes: Builder, Patch, ValueMissingError
Constant Summary
collapse
- VERSION =
'0.1.2'
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(value = nil) ⇒ HandyHash
Returns a new instance of HandyHash.
8
9
10
11
12
13
14
|
# File 'lib/handy_hash.rb', line 8
def initialize(value=nil)
if value
value.each do |k, v|
self[k] = __wrap v
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/handy_hash.rb', line 33
def method_missing(m, *args, &block)
if m =~ /\Ato_ary\Z/ || m =~ /\=$/ || args.size > 1 || block_given?
super
else
m = m.to_s
if m[-1] == '!'
m = m.to_s.sub!(/!$/, '')
required = true
end
if m =~ /\A_(.+)_\Z/
m = $1
end
__fetch_value m, required: required, default: args.first
end
end
|
Class Method Details
.Nil ⇒ Object
64
65
66
|
# File 'lib/handy_hash.rb', line 64
def Nil
@Nil ||= new.tap(&:freeze)
end
|
Instance Method Details
#[]=(k, v) ⇒ Object
21
22
23
|
# File 'lib/handy_hash.rb', line 21
def []=(k,v)
super k, __wrap(v)
end
|
#freeze ⇒ Object
16
17
18
19
|
# File 'lib/handy_hash.rb', line 16
def freeze
super
each{|_, v| v.freeze unless v.frozen?}
end
|
#patch(hash = nil, &block) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/handy_hash.rb', line 25
def patch(hash=nil, &block)
change_set = [].tap do |ch|
ch << __wrap(hash) if hash
ch << Builder.new(&block).data if block_given?
end
Patch.(self, *change_set)
end
|