Class: JsonObject

Inherits:
Object
  • Object
show all
Defined in:
lib/random_api/json_object.rb

Direct Known Subclasses

RandomApi::User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ JsonObject

Returns a new instance of JsonObject.



16
17
18
# File 'lib/random_api/json_object.rb', line 16

def initialize(json)
  self._json = json
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/random_api/json_object.rb', line 25

def method_missing(name, *args)
  name = name.to_sym
  if respond_to_missing?(name)
    accessor = self.class.accessors[name]
    accessor.(self._json)
  else
    super
  end
end

Instance Attribute Details

#_jsonObject

Returns the value of attribute _json.



14
15
16
# File 'lib/random_api/json_object.rb', line 14

def _json
  @_json
end

Class Method Details

.accessorsObject



9
10
11
# File 'lib/random_api/json_object.rb', line 9

def accessors
  @accessors ||= {}
end

.map_method(name, json_path, mutator = nil) ⇒ Object



5
6
7
# File 'lib/random_api/json_object.rb', line 5

def map_method(name, json_path, mutator = nil)
  accessors[name] = JsonAccessor.new(json_path, mutator)
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/random_api/json_object.rb', line 20

def respond_to_missing?(name, include_private = false)
  name = name.to_sym
  self.class.accessors.has_key?(name)
end