Class: Mashie
- Inherits:
-
Hash
show all
- Defined in:
- lib/kurchatov/mashie.rb
Instance Method Summary
collapse
-
#custom_reader(key) {|value| ... } ⇒ Object
(also: #[])
-
#custom_writer(key, value) ⇒ Object
(also: #[]=)
-
#deep_merge(other_hash, &blk) ⇒ Object
(also: #merge)
-
#deep_update(other_hash, &blk) ⇒ Object
(also: #deep_merge!, #update)
-
#delete(key) ⇒ Object
-
#dup ⇒ Object
Duplicates the current mash as a new mash.
-
#fetch(key, *args) ⇒ Object
-
#id ⇒ Object
-
#initialize(source_hash = nil, default = nil, &blk) ⇒ Mashie
constructor
A new instance of Mashie.
-
#initializing_reader(key) ⇒ Object
-
#key?(key) ⇒ Boolean
(also: #has_key?, #include?, #member?)
-
#method_missing(method_name, *args, &blk) ⇒ Object
-
#regular_dup ⇒ Object
-
#replace(other_hash) ⇒ Object
-
#respond_to?(method_name, include_private = false) ⇒ Boolean
-
#shallow_merge(other_hash) ⇒ Object
-
#shallow_update(other_hash) ⇒ Object
-
#type ⇒ Object
-
#underbang_reader(key) ⇒ Object
Constructor Details
#initialize(source_hash = nil, default = nil, &blk) ⇒ Mashie
Returns a new instance of Mashie.
3
4
5
6
|
# File 'lib/kurchatov/mashie.rb', line 3
def initialize(source_hash = nil, default = nil, &blk)
deep_update(source_hash) if source_hash
default ? super(default) : super(&blk)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/kurchatov/mashie.rb', line 119
def method_missing(method_name, *args, &blk)
return self.[](method_name, &blk) if key?(method_name)
match = method_name.to_s.match(/(.*?)([?=!_]?)$/)
case match[2]
when '='
self[match[1]] = args.first
when '?'
!!self[match[1]]
when '!'
initializing_reader(match[1])
when '_'
underbang_reader(match[1])
else
default(method_name, *args, &blk)
end
end
|
Instance Method Details
#custom_reader(key) {|value| ... } ⇒ Object
Also known as:
[]
23
24
25
26
27
|
# File 'lib/kurchatov/mashie.rb', line 23
def custom_reader(key)
value = regular_reader(convert_key(key))
yield value if block_given?
value
end
|
#custom_writer(key, value) ⇒ Object
Also known as:
[]=
29
30
31
|
# File 'lib/kurchatov/mashie.rb', line 29
def custom_writer(key, value) regular_writer(convert_key(key), convert_value(value))
end
|
#deep_merge(other_hash, &blk) ⇒ Object
Also known as:
merge
73
74
75
|
# File 'lib/kurchatov/mashie.rb', line 73
def deep_merge(other_hash, &blk)
dup.deep_update(other_hash, &blk)
end
|
#deep_update(other_hash, &blk) ⇒ Object
Also known as:
deep_merge!, update
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/kurchatov/mashie.rb', line 79
def deep_update(other_hash, &blk)
other_hash.each_pair do |k, v|
key = convert_key(k)
if regular_reader(key).is_a?(Mash) and v.is_a?(::Hash)
custom_reader(key).deep_update(v, &blk)
else
value = convert_value(v, true)
value = blk.call(key, self[k], value) if blk
custom_writer(key, value)
end
end
self
end
|
#delete(key) ⇒ Object
55
56
57
|
# File 'lib/kurchatov/mashie.rb', line 55
def delete(key)
super(convert_key(key))
end
|
#dup ⇒ Object
Duplicates the current mash as a new mash.
61
62
63
|
# File 'lib/kurchatov/mashie.rb', line 61
def dup
self.class.new(self, self.default)
end
|
#fetch(key, *args) ⇒ Object
51
52
53
|
# File 'lib/kurchatov/mashie.rb', line 51
def fetch(key, *args)
super(convert_key(key), *args)
end
|
#id ⇒ Object
12
13
14
|
# File 'lib/kurchatov/mashie.rb', line 12
def id self['id']
end
|
#initializing_reader(key) ⇒ Object
36
37
38
39
40
|
# File 'lib/kurchatov/mashie.rb', line 36
def initializing_reader(key)
ck = convert_key(key)
regular_writer(ck, self.class.new) unless key?(ck)
regular_reader(ck)
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?, include?, member?
65
66
67
|
# File 'lib/kurchatov/mashie.rb', line 65
def key?(key)
super(convert_key(key))
end
|
#regular_dup ⇒ Object
59
|
# File 'lib/kurchatov/mashie.rb', line 59
alias_method :regular_dup, :dup
|
#replace(other_hash) ⇒ Object
108
109
110
111
112
|
# File 'lib/kurchatov/mashie.rb', line 108
def replace(other_hash)
(keys - other_hash.keys).each { |key| delete(key) }
other_hash.each { |key, value| self[key] = value }
self
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
114
115
116
117
|
# File 'lib/kurchatov/mashie.rb', line 114
def respond_to?(method_name, include_private=false)
return true if key?(method_name) || method_name.to_s.slice(/[=?!_]\Z/)
super
end
|
#shallow_merge(other_hash) ⇒ Object
97
98
99
|
# File 'lib/kurchatov/mashie.rb', line 97
def shallow_merge(other_hash)
dup.shallow_update(other_hash)
end
|
#shallow_update(other_hash) ⇒ Object
101
102
103
104
105
106
|
# File 'lib/kurchatov/mashie.rb', line 101
def shallow_update(other_hash)
other_hash.each_pair do |k, v|
regular_writer(convert_key(k), convert_value(v, true))
end
self
end
|
#type ⇒ Object
16
17
18
|
# File 'lib/kurchatov/mashie.rb', line 16
def type self['type']
end
|
#underbang_reader(key) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/kurchatov/mashie.rb', line 42
def underbang_reader(key)
ck = convert_key(key)
if key?(ck)
regular_reader(ck)
else
self.class.new
end
end
|