Class: CFC::APIObject

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ APIObject

Returns a new instance of APIObject.



12
13
14
15
16
# File 'lib/cfc/objects/object.rb', line 12

def initialize(data)
  @data = data
  @api = CFC::API.new
  initialize_relationships
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/cfc/objects/object.rb', line 18

def method_missing(name)
  if @data.include?(name.to_s)
    @data[name.to_s]
  else
    raise CFC::Errors::MissingProperty,
          CFC::Errors::MissingProperty.default_message(self, name)
  end
end

Class Attribute Details

.relationshipsObject (readonly)

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

Class Method Details

.opts(bind) ⇒ Object



44
45
46
47
48
# File 'lib/cfc/objects/object.rb', line 44

def self.opts(bind)
  bind.local_variables.map do |var|
    [var, bind.local_variable_get(var)]
  end.to_h
end

.relationship(property, cls, multiple: false) ⇒ Object



37
38
39
40
41
42
# File 'lib/cfc/objects/object.rb', line 37

def self.relationship(property, cls, multiple: false)
  unless defined?(@relationships)
    @relationships = []
  end
  @relationships << [property, cls, { multiple: multiple }]
end

Instance Method Details

#inspectObject Also known as: to_s



31
32
33
# File 'lib/cfc/objects/object.rb', line 31

def inspect
  "#<#{self.class.name}:0x#{(object_id << 1).to_s(16)} #{@data.map { |k, v| "#{k}=#{v.inspect}" }.join(', ')}>"
end

#respond_to_missing?(name, *_args, **_opts) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/cfc/objects/object.rb', line 27

def respond_to_missing?(name, *_args, **_opts)
  @data.include?(name.to_s)
end