class PlateApi::PlateObject::Base

Constants

HasManyRelations
HasOneRelations

Attributes

attributes[R]
id[R]
object_handler[RW]
relations[R]

Public Class Methods

new(id, attributes, relations, object_handler=nil) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 9
def initialize(id, attributes, relations, object_handler=nil)
  @id = id
  @object_handler = object_handler
  initialize_state(attributes, relations)
end

Private Class Methods

define_create_method(singular_name, klass) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 111
def self.define_create_method(singular_name, klass)
  define_method("create_#{singular_name}") do |create_attributes|
    @object_handler.api_connector.handler(Object.const_get(klass)).create(self, create_attributes)
  end
end
define_has_many_methods(plural_name, klass) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 97
def self.define_has_many_methods(plural_name, klass)
  define_method(plural_name.to_s) do |params={}|
    @object_handler.api_connector.handler(
      Object.const_get(klass)
    ).index(self.class, @id, params)
  end

  define_method("#{plural_name}_total_count") do
    @object_handler.api_connector.handler(
      Object.const_get(klass)
    ).index_total_count(self)
  end
end
define_has_one_method(name, klass) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 82
def self.define_has_one_method(name, klass)
  define_method(name.to_s) do
    id = self.send("#{name}_id")
    return nil unless id
    @object_handler.api_connector.handler(Object.const_get(klass)).find(id)
  end
end
has_many(plural_name, singular_name, klass, define_create_method=false) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 90
def self.has_many(plural_name, singular_name, klass, define_create_method=false)
  HasManyRelations[self.name] ||= {}
  HasManyRelations[self.name][plural_name.to_s] = klass
  define_has_many_methods(plural_name, klass)
  define_create_method(singular_name, klass) if define_create_method
end
has_one(name, klass) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 75
def self.has_one(name, klass)
  HasOneRelations[self.name] ||= {}
  self.attr_accessor "#{name}_id"
  HasOneRelations[self.name][name.to_s] = klass
  define_has_one_method(name, klass)
end

Public Instance Methods

==(other) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 59
def ==(other)
  return other.id == @id && other.class == self.class
end
api_name() click to toggle source
# File lib/plate_api/plate_object/base.rb, line 15
def api_name
  self.class.api_name
end
delete() click to toggle source
# File lib/plate_api/plate_object/base.rb, line 36
def delete
  raise ArgumentError.new("No object_handler is attached to this object") unless @object_handler
  @object_handler.delete(@id)
end
inspect() click to toggle source
# File lib/plate_api/plate_object/base.rb, line 45
def inspect
  to_s
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/plate_api/plate_object/base.rb, line 49
def method_missing(m, *args, &block)
  if attributes[m.to_s]
    return attributes[m.to_s]
  elsif attributes["content"] && attributes["content"][m.to_s]
    return attributes["content"][m.to_s]["value"]
  else
    super
  end
end
reload() click to toggle source
# File lib/plate_api/plate_object/base.rb, line 19
def reload
  raise ArgumentError.new("No object_handler is set.") unless @object_handler
  reinitialize(@object_handler.find(@id))
  return self
end
to_s() click to toggle source
# File lib/plate_api/plate_object/base.rb, line 41
def to_s
  "<Plate #{self.class.name.split('::').last}, @id=#{@id}, @attributes=#{@attributes}, @object_handler=#{@object_handler}>"
end
update(attributes) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 25
def update(attributes)
  raise ArgumentError.new("Input `attributes` is not a Hash") unless attributes.is_a? Hash
  raise ArgumentError.new("No object_handler is attached to this object") unless @object_handler
  if new_object = @object_handler.update(@id, attributes)
    reinitialize(new_object)
  else
    raise ArgumentError.new("The update was unsuccesful.")
  end
  return self
end

Private Instance Methods

initialize_state(attributes, relations) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 69
def initialize_state(attributes, relations)
  set_relation_ids(relations)
  @attributes = attributes
  @relations = relations
end
reinitialize(new_object) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 65
def reinitialize(new_object)
  initialize_state(new_object.attributes, new_object.relations)
end
set_relation_ids(relations_attributes) click to toggle source
# File lib/plate_api/plate_object/base.rb, line 117
def set_relation_ids(relations_attributes)
  HasOneRelations[self.class.name] ||= {}

  return unless relations_attributes
  self.class::HasOneRelations[self.class.name].keys.each do |relation_name|
    val = relations_attributes["#{relation_name}_id"]
    if val
      send("#{relation_name}_id=", val)
    end
  end
end