Class: GrubY::BaseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/gruubY/types/base_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, api: nil, client: nil) ⇒ BaseObject

Returns a new instance of BaseObject.



15
16
17
18
19
20
21
22
# File 'lib/gruubY/types/base_object.rb', line 15

def initialize(data = {}, api: nil, client: nil)
  @data = normalize_hash(data)
  @api = api
  @client = client
  self.class.field_names.each do |field|
    instance_variable_set("@#{field}", @data[field.to_s])
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



39
40
41
42
43
44
# File 'lib/gruubY/types/base_object.rb', line 39

def method_missing(name, *args, &block)
  return super unless args.empty? && block.nil?
  return @data[name.to_s] if @data.key?(name.to_s)

  super
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



24
25
26
# File 'lib/gruubY/types/base_object.rb', line 24

def api
  @api
end

#clientObject (readonly)

Returns the value of attribute client.



24
25
26
# File 'lib/gruubY/types/base_object.rb', line 24

def client
  @client
end

Class Method Details

.field_namesObject



10
11
12
# File 'lib/gruubY/types/base_object.rb', line 10

def field_names
  @field_names || []
end

.fields(*names) ⇒ Object



4
5
6
7
8
# File 'lib/gruubY/types/base_object.rb', line 4

def fields(*names)
  @field_names ||= []
  @field_names.concat(names.map(&:to_sym))
  attr_reader(*names)
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/gruubY/types/base_object.rb', line 26

def [](key)
  @data[key.to_s]
end

#dig(*keys) ⇒ Object



30
31
32
33
# File 'lib/gruubY/types/base_object.rb', line 30

def dig(*keys)
  normalized = keys.map(&:to_s)
  @data.dig(*normalized)
end

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

Returns:

  • (Boolean)


46
47
48
# File 'lib/gruubY/types/base_object.rb', line 46

def respond_to_missing?(name, include_private = false)
  @data.key?(name.to_s) || super
end

#to_hObject



35
36
37
# File 'lib/gruubY/types/base_object.rb', line 35

def to_h
  @data.dup
end