Class: Stannp::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/stannp/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.



9
10
11
# File 'lib/stannp/object.rb', line 9

def initialize(attributes)
  @attributes = OpenStruct.new(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stannp/object.rb', line 13

def method_missing(method, *args, &block)
  value = attributes.send(method, *args, &block)
  case value
  when Hash
    Object.new(value)
  when Array
    value.map { |item| item.is_a?(Hash) ? Object.new(item) : item }
  else
    value
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/stannp/object.rb', line 7

def attributes
  @attributes
end

Instance Method Details

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/stannp/object.rb', line 25

def respond_to_missing?(method, include_private = false)
  attributes.to_h.keys.include?(method) || super
end