class CFC::APIObject

Attributes

relationships[R]

Public Class Methods

new(data) click to toggle source
# File lib/cfc/objects/object.rb, line 12
def initialize(data)
  @data = data
  @api = CFC::API.new
  initialize_relationships
end
opts(bind) click to toggle source
# File lib/cfc/objects/object.rb, line 54
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) click to toggle source
# 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

Public Instance Methods

as_json()
Alias for: to_h
inspect() click to toggle source
# 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
Also aliased as: to_s
method_missing(name) click to toggle source
# 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
respond_to_missing?(name, *_args, **_opts) click to toggle source
# File lib/cfc/objects/object.rb, line 27
def respond_to_missing?(name, *_args, **_opts)
  @data.include?(name.to_s)
end
to_h() click to toggle source
# File lib/cfc/objects/object.rb, line 37
def to_h
  @data
end
Also aliased as: as_json
to_json(*args) click to toggle source
# File lib/cfc/objects/object.rb, line 43
def to_json(*args)
  as_json.to_json(*args)
end
to_s()
Alias for: inspect

Private Instance Methods

initialize_relationships() click to toggle source
# File lib/cfc/objects/object.rb, line 62
def initialize_relationships
  (self.class.relationships || []).each do |rel|
    property, cls, opts = rel
    @data[property.to_s] = if opts[:multiple]
                             @data[property.to_s].map { |o| cls.new(o) }
                           else
                             cls.new(@data[property.to_s])
                           end
  end
end