class MagLoft::RemoteResource

Attributes

destroyed[R]
id[RW]

Public Class Methods

all() click to toggle source
# File lib/magloft/remote_resource.rb, line 91
def all
  api.get(endpoint).transform_to_many(self)
end
create(attributes = {}) click to toggle source
# File lib/magloft/remote_resource.rb, line 95
def create(attributes = {})
  entity = new(attributes)
  entity.save
  entity
end
endpoint(path = nil) click to toggle source
# File lib/magloft/remote_resource.rb, line 71
def endpoint(path = nil)
  if path.nil?
    @endpoint
  else
    @endpoint = path
  end
end
find(id) click to toggle source
# File lib/magloft/remote_resource.rb, line 79
def find(id)
  api.get("#{endpoint}/#{id}").transform_to_one(self)
end
find_one(params) click to toggle source
# File lib/magloft/remote_resource.rb, line 83
def find_one(params)
  where(params).first
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/magloft/remote_resource.rb, line 101
def method_missing(name, *args, &block)
  if name[0..7] == "find_by_" and args.length == 1
    attribute = name[8..-1].to_sym
    if remote_attributes.include?(attribute)
      params = {}
      params[attribute] = args.first
      return find_one(params)
    end
  end
  super
end
new(attributes = {}) click to toggle source
# File lib/magloft/remote_resource.rb, line 6
def initialize(attributes = {})
  allowed_attributes = attributes.slice(*self.class.remote_attributes)
  Dialers::AssignAttributes.call(self, allowed_attributes.except(:id))
end
remote_attribute(*args) click to toggle source
# File lib/magloft/remote_resource.rb, line 63
def remote_attribute(*args)
  args.each do |arg|
    remote_attributes.push(arg)
    class_eval("attr_accessor :#{arg}", __FILE__, __LINE__)
    class_eval("def #{arg}=(val);update_data(:#{arg}, val);end", __FILE__, __LINE__)
  end
end
remote_attributes() click to toggle source
# File lib/magloft/remote_resource.rb, line 59
def remote_attributes
  @remote_attributes ||= []
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/magloft/remote_resource.rb, line 113
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('find_by_') || super
end
where(params) click to toggle source
# File lib/magloft/remote_resource.rb, line 87
def where(params)
  api.get(endpoint, params).transform_to_many(self)
end

Private Class Methods

api() click to toggle source
# File lib/magloft/remote_resource.rb, line 119
def api
  Api.client.api_caller
end

Public Instance Methods

changed?() click to toggle source
# File lib/magloft/remote_resource.rb, line 11
def changed?
  changed_data.keys.count > 0
end
changed_data() click to toggle source
# File lib/magloft/remote_resource.rb, line 42
def changed_data
  @changed_data ||= {}
end
clear_changed_data!() click to toggle source
# File lib/magloft/remote_resource.rb, line 46
def clear_changed_data!
  @changed_data = {}
  self
end
destroy() click to toggle source
# File lib/magloft/remote_resource.rb, line 33
def destroy
  return false if id.nil? or destroyed?
  transformable = Api.client.api_caller.delete("#{self.class.endpoint}/#{id}")
  transformable.transform_to_existing(self)
  @destroyed = true
  clear_changed_data!
  self
end
destroyed?() click to toggle source
# File lib/magloft/remote_resource.rb, line 15
def destroyed?
  destroyed == true
end
save() click to toggle source
# File lib/magloft/remote_resource.rb, line 19
def save
  return false if destroyed? or !changed?
  if changed_data.keys.count > 0
    if id.nil?
      transformable = Api.client.api_caller.post(self.class.endpoint, changed_data)
    else
      transformable = Api.client.api_caller.put("#{self.class.endpoint}/#{id}", changed_data.except(:id))
    end
    transformable.transform_to_existing(self)
    clear_changed_data!
  end
  self
end
update_data(key, value) click to toggle source
# File lib/magloft/remote_resource.rb, line 51
def update_data(key, value)
  if send(key) != value
    instance_variable_set("@#{key}", value)
    changed_data[key] = value
  end
end