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



54
55
56
57
58
# File 'lib/cfc/objects/object.rb', line 54

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

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



47
48
49
50
51
52
# File 'lib/cfc/objects/object.rb', line 47

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

#to_hObject Also known as: as_json



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

def to_h
  @data
end

#to_jsonObject



43
44
45
# File 'lib/cfc/objects/object.rb', line 43

def to_json(*)
  as_json.to_json(*)
end