module LightSide::Resources

Public Class Methods

included(base) click to toggle source
# File lib/lightside/resources.rb, line 63
def self.included(base)
  base.extend(ClassMethods)
  base.singleton_class.class_eval { attr_accessor :resource_base, :readonly_attributes, :writable_attributes }
end
new(hash={}) click to toggle source
# File lib/lightside/resources.rb, line 4
def initialize(hash={})
  set_attributes_from_hash(hash)
end

Public Instance Methods

attributes() click to toggle source
# File lib/lightside/resources.rb, line 8
def attributes
  self.class.attributes.inject({}) do |memo, attr|
    memo.merge(attr => self.__send__(attr))
  end
end
delete() click to toggle source
# File lib/lightside/resources.rb, line 14
def delete
  self.class.delete_resource(id)
end
id() click to toggle source
# File lib/lightside/resources.rb, line 18
def id
  url && url.split("/").last
end
reload() click to toggle source
# File lib/lightside/resources.rb, line 22
def reload
  self.class.resource(id) do |hash|
    set_attributes_from_hash(hash)
  end
  self
end
save() click to toggle source
# File lib/lightside/resources.rb, line 33
def save
  json = JSON.generate(writable_attributes)
  if id
    self.class.update_resource(id, json) { |hash| set_attributes_from_hash(hash) }
  else
    self.class.create_resource(json) { |hash| set_attributes_from_hash(hash) }
  end
  self
end
set_attributes_from_hash(hash) click to toggle source
# File lib/lightside/resources.rb, line 43
def set_attributes_from_hash(hash)
  self.class.attributes.each do |attr|
    self.__send__("#{attr.to_s}=", hash.fetch(attr, nil))
  end
end
to_s() click to toggle source
# File lib/lightside/resources.rb, line 49
def to_s
  %w(id url errors).map(&:to_sym).inject("#{self.class.name}") do |memo, attr|
    val = self.__send__(attr)
    memo << " #{attr}:#{val}" if val
    memo
  end
end
update() click to toggle source
# File lib/lightside/resources.rb, line 29
def update
  save
end
writable_attributes() click to toggle source
# File lib/lightside/resources.rb, line 57
def writable_attributes
  self.class.writable_attributes.inject({}) do |memo, attr|
    memo.merge(attr => self.__send__(attr))
  end
end