class Arbor::Model::Abstract

Attributes

api_client[RW]
attribute_lock[RW]
attribute_names[RW]
entity_type[RW]
href[RW]

Public Class Methods

new(attributes) click to toggle source
# File lib/arbor/model/abstract.rb, line 8
def initialize(attributes)
  @attribute_names = []
  load_attributes(attributes)
end

Public Instance Methods

attach_client(client) click to toggle source
# File lib/arbor/model/abstract.rb, line 56
def attach_client(client)
  self.api_client = client
  self.attribute_names.each do |name|
    if send(name).respond_to?("api_client=")
      send(name).attach_client(client)
    end
  end
end
attribute(name) click to toggle source
# File lib/arbor/model/abstract.rb, line 46
def attribute(name)
  if instance_variable_defined?("@#{name}")
    instance_variable_get("@#{name}")
  else
    raise Errors::UnknownAttributeError if attribute_lock
    refresh_data
    attribute(name)
  end
end
attributes() click to toggle source
# File lib/arbor/model/abstract.rb, line 69
def attributes
  Hash[attribute_names.map { |a| [a, send(a)] }]
end
inspect() click to toggle source
# File lib/arbor/model/abstract.rb, line 73
def inspect
  "#<#{self.class.name} #{attribute_names.map { |a| "#{a}: #{send(a).inspect}" }.join(', ')}>"
end
load_attributes(attributes) click to toggle source
# File lib/arbor/model/abstract.rb, line 35
def load_attributes(attributes)
  attributes.each do |name, value|
    unless self.respond_to?("#{name}=")
      self.class.instance_eval { define_method(name) { attribute(name) } }
      self.class.send(:attr_writer, name)
    end
    @attribute_names |= [name]
    self.send("#{name}=", value)
  end
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/arbor/model/abstract.rb, line 13
def method_missing(meth, *args, &block)
  attribute_lock ? super : begin
    refresh_data
    send(meth, *args, &block)
  end
end
refresh_data() click to toggle source
# File lib/arbor/model/abstract.rb, line 20
def refresh_data
  raise "No API client configured for this resource" if api_client.nil?
  raise "No entity_type set for this resource" if entity_type.nil?
  raise "No known endpoint for this resource" if href.nil?

  data = api_client.get(href)

  entity_type_lower = entity_type.tr('_', '-').camelize(:lower).tr('-', '_')
  parsed_attributes = Serialiser.parse_attributes(data[entity_type_lower])
  load_attributes(parsed_attributes)
  attach_client(self.api_client)

  @attribute_lock = true
end
to_json() click to toggle source
# File lib/arbor/model/abstract.rb, line 77
def to_json
  Hash[instance_variables.map{ |iv| [iv[1..-1], instance_variable_get(iv)] }]
    .reject{ |n, _v| n == 'api_client' }.to_json
end
unlock_attributes() click to toggle source
# File lib/arbor/model/abstract.rb, line 65
def unlock_attributes
  @attribute_lock = false
end