Class: RedditGet::Data
- Inherits:
-
Object
show all
- Defined in:
- lib/reddit_get.rb
Overview
Allow to use method call chains instead of Hash keys navigation
Instance Method Summary
collapse
Constructor Details
#initialize(data) ⇒ Data
Returns a new instance of Data.
14
15
16
|
# File 'lib/reddit_get.rb', line 14
def initialize(data)
@data = data
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *_args) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/reddit_get.rb', line 35
def method_missing(method, *_args)
@data.send(method)
rescue NoMethodError
out = @data.fetch(method.to_s)
case out
when Hash
Data.new(out)
when Array
out.map { |i| Data.new(i) }
else
out
end
end
|
Instance Method Details
#[](key) ⇒ Object
31
32
33
|
# File 'lib/reddit_get.rb', line 31
def [](key)
@data.fetch(key)
end
|
#objectify(data) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/reddit_get.rb', line 18
def objectify(data)
data.transform_values! do |v|
case v
when Hash
Data.new(v)
when Array
v.map { |i| Data.new(i) }
else
v
end
end
end
|
#respond_to_missing?(method) ⇒ Boolean
49
50
51
|
# File 'lib/reddit_get.rb', line 49
def respond_to_missing?(method)
@data.key?(method_name.to_s) || super
end
|