module Plaza::RestfulModel

Constants

ErrorResponse

Public Class Methods

included(base) click to toggle source
# File lib/plaza/models/restful_model.rb, line 9
def self.included(base)
  base.class_eval do
    include Virtus.model
    include Plaza::BaseModel
    attribute :id,     Integer
    attribute :errors, Hash

    def serialize
      attrs = attributes.delete_if{|k, v| k.to_sym == :id && v.nil?}
      attrs = attrs.delete_if{|k, v| [:updated_at, :created_at].include?(k.to_sym)}
      attrs.delete(:errors)
      {singular_name => attrs}
    end
  end

  base.extend ClassMethods
end

Public Instance Methods

delete() click to toggle source
# File lib/plaza/models/restful_model.rb, line 96
def delete
  self.class.adapter.delete(self.id)
end
error_messages() click to toggle source
# File lib/plaza/models/restful_model.rb, line 100
def error_messages
  if errors.empty?
    []
  else
    errors.collect{|k, v| v.collect{|val| "#{k} #{val}"}}.flatten
  end
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/plaza/models/restful_model.rb, line 142
def method_missing(method_name, *args, &block)
  method_name = method_name.to_s
  if self.respond_to?(method_name + '_id')
    obj_id = self.send(method_name + '_id')
    class_name = Plaza::Inflector.classify(method_name)
    klass = (self.class.name.split('::')[0..-2] + [class_name]).reduce(Module, :const_get)
    return klass.find(obj_id)
  else
    raise NoMethodError.new "undefined method '#{method_name}' for #{self.class}"
  end
end
new_record?() click to toggle source
# File lib/plaza/models/restful_model.rb, line 108
def new_record?
  !persisted?
end
persisted?() click to toggle source
# File lib/plaza/models/restful_model.rb, line 112
def persisted?
  self.id.present?
end
plaza_config() click to toggle source
# File lib/plaza/models/restful_model.rb, line 92
def plaza_config
  self.class.plaza_config
end
save() click to toggle source
# File lib/plaza/models/restful_model.rb, line 116
def save
  begin
    if self.id
      self.attributes = self.class.adapter.update(self.id, self.serialize)
    else
      self.attributes = self.class.adapter.create(self.serialize)
    end
  rescue Plaza::ResourceInvalid => e
    self.errors.merge!(e.errors)
  end
  self.errors.empty?
end
serialize() click to toggle source
# File lib/plaza/models/restful_model.rb, line 16
def serialize
  attrs = attributes.delete_if{|k, v| k.to_sym == :id && v.nil?}
  attrs = attrs.delete_if{|k, v| [:updated_at, :created_at].include?(k.to_sym)}
  attrs.delete(:errors)
  {singular_name => attrs}
end
symbolize_keys(hash) click to toggle source
# File lib/plaza/models/restful_model.rb, line 129
def symbolize_keys(hash)
  hash.inject({}){|sym_hash,(k,v)| sym_hash[k.to_sym] = v; sym_hash}
end
to_param() click to toggle source
# File lib/plaza/models/restful_model.rb, line 133
def to_param
  self.id
end
update_attributes(attributes_hash) click to toggle source
# File lib/plaza/models/restful_model.rb, line 137
def update_attributes(attributes_hash)
  self.attributes = self.attributes.merge(self.symbolize_keys(attributes_hash))
  self.save
end

Protected Instance Methods

class_for(identifier) click to toggle source
# File lib/plaza/models/restful_model.rb, line 156
def class_for(identifier)
  if identifier.kind_of?(Symbol)
    klass = Plaza.const_get(Plaza::Inflector.classify(identifier.to_s))
  elsif identifier.kind_of?(String)
    klass = Object.const_get(identifier)
  elsif identifier.kind_of?(Class)
    klass = identifier
  else
    raise TypeError.new("Can't convert to has_many relation")
  end
end