Class: Roast::Workflow::DotAccessHash
- Inherits:
-
Object
- Object
- Roast::Workflow::DotAccessHash
show all
- Defined in:
- lib/roast/workflow/dot_access_hash.rb
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DotAccessHash.
6
7
8
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 6
def initialize(hash)
@hash = hash || {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 19
def method_missing(method_name, *args, &block)
method_str = method_name.to_s
if method_str.end_with?("?")
key = method_str.chomp("?")
return false unless has_key?(key)
!!self[key]
elsif method_str.end_with?("=")
key = method_str.chomp("=")
self[key] = args.first
elsif method_str.end_with?("!")
super
elsif args.empty? && block.nil?
self[method_str]
else
super
end
end
|
Instance Method Details
#==(other) ⇒ Object
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 180
def ==(other)
case other
when DotAccessHash
@hash == other.instance_variable_get(:@hash)
when Hash
@hash == other
else
false
end
end
|
#[](key) ⇒ Object
10
11
12
13
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 10
def [](key)
value = @hash[key.to_sym] || @hash[key.to_s]
wrap_value(value)
end
|
#[]=(key, value) ⇒ Object
15
16
17
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 15
def []=(key, value)
@hash[key.to_sym] = value
end
|
#clear ⇒ Object
175
176
177
178
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 175
def clear
@hash.clear
self
end
|
#compact ⇒ Object
148
149
150
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 148
def compact
DotAccessHash.new(@hash.compact)
end
|
#delete(key) ⇒ Object
171
172
173
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 171
def delete(key)
@hash.delete(key.to_sym) || @hash.delete(key.to_s)
end
|
#dig(*keys) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 118
def dig(*keys)
keys.inject(self) do |obj, key|
break nil unless obj.is_a?(DotAccessHash) || obj.is_a?(Hash)
if obj.is_a?(DotAccessHash)
obj[key]
else
obj[key.to_sym] || obj[key.to_s]
end
end
end
|
#each(&block) ⇒ Object
71
72
73
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 71
def each(&block)
@hash.each(&block)
end
|
#empty? ⇒ Boolean
67
68
69
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 67
def empty?
@hash.empty?
end
|
#except(*keys) ⇒ Object
162
163
164
165
166
167
168
169
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 162
def except(*keys)
excluded = @hash.dup
keys.each do |key|
excluded.delete(key.to_sym)
excluded.delete(key.to_s)
end
DotAccessHash.new(excluded)
end
|
#fetch(key, *args) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 106
def fetch(key, *args)
if has_key?(key) self[key]
elsif block_given?
yield(key)
elsif !args.empty?
args[0]
else
raise KeyError, "key not found: #{key.inspect}"
end
end
|
#has_key?(key_name) ⇒ Boolean
Also known as:
member?
191
192
193
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 191
def has_key?(key_name)
@hash.key?(key_name.to_sym) || @hash.key?(key_name.to_s)
end
|
#include?(key) ⇒ Boolean
102
103
104
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 102
def include?(key)
has_key?(key) end
|
#inspect ⇒ Object
79
80
81
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 79
def inspect
@hash.inspect
end
|
#key?(key) ⇒ Boolean
98
99
100
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 98
def key?(key)
has_key?(key) end
|
#keys ⇒ Object
63
64
65
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 63
def keys
@hash.keys
end
|
#map(&block) ⇒ Object
136
137
138
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 136
def map(&block)
@hash.map(&block)
end
|
#merge(other) ⇒ Object
87
88
89
90
91
92
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 87
def merge(other)
merged_hash = @hash.dup
other_hash = other.is_a?(DotAccessHash) ? other.to_h : other
merged_hash.merge!(other_hash)
DotAccessHash.new(merged_hash)
end
|
#reject(&block) ⇒ Object
144
145
146
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 144
def reject(&block)
DotAccessHash.new(@hash.reject(&block))
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 45
def respond_to_missing?(method_name, include_private = false)
method_str = method_name.to_s
if method_str.end_with?("!")
false elsif method_str.end_with?("?")
true elsif method_str.end_with?("=")
true else
true end
end
|
#select(&block) ⇒ Object
140
141
142
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 140
def select(&block)
DotAccessHash.new(@hash.select(&block))
end
|
#size ⇒ Object
Also known as:
length
130
131
132
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 130
def size
@hash.size
end
|
#slice(*keys) ⇒ Object
152
153
154
155
156
157
158
159
160
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 152
def slice(*keys)
sliced = {}
keys.each do |key|
if has_key?(key) sliced[key.to_sym] = @hash[key.to_sym] || @hash[key.to_s]
end
end
DotAccessHash.new(sliced)
end
|
#to_h ⇒ Object
59
60
61
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 59
def to_h
@hash
end
|
#to_json(*args) ⇒ Object
83
84
85
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 83
def to_json(*args)
@hash.to_json(*args)
end
|
#to_s ⇒ Object
75
76
77
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 75
def to_s
@hash.to_s
end
|
#values ⇒ Object
94
95
96
|
# File 'lib/roast/workflow/dot_access_hash.rb', line 94
def values
@hash.values
end
|