module Rackconnect::Model::ClassMethods

Public Instance Methods

all(*args) click to toggle source
# File lib/rackconnect/lib/model.rb, line 37
def all(*args)
  apply(args)
  resp = Rackconnect::Request.get(@_endpoint)
  resp.body.map{ |obj| self.new(json: obj) }
end
attributes(*args) click to toggle source
# File lib/rackconnect/lib/model.rb, line 33
def attributes(*args)
  attr_accessor *args
end
create(options={}) click to toggle source
# File lib/rackconnect/lib/model.rb, line 49
def create(options={})
  resp = Rackconnect::Request.post(@_endpoint, body: options.to_json)
  self.new(json: resp.body)
end
endpoint(str=nil, options={}) { |block| ... } click to toggle source
# File lib/rackconnect/lib/model.rb, line 24
def endpoint(str=nil, options={}, &block)
  @_endpoint = block_given? ? yield(block) : str
end
endpoint_vars(*args) click to toggle source
# File lib/rackconnect/lib/model.rb, line 28
def endpoint_vars(*args)
  # Inject into class level of descendant class
  self.class.module_eval{ attr_accessor *args }
end
find(*args) click to toggle source
# File lib/rackconnect/lib/model.rb, line 43
def find(*args)
  id = apply(args)
  resp = Rackconnect::Request.get("#{@_endpoint}/#{id}")
  self.new(json: resp.body)
end

Private Instance Methods

apply(args) click to toggle source
# File lib/rackconnect/lib/model.rb, line 56
def apply(args)
  first = args.first

  # Plain resource ID string
  return first if first.is_a?(String)

  # Parent IDs, etc.
  if first.is_a?(Array)
    args.each do |(arg)|
      arg.each do |(k,v)|
        self.send("#{k}=", v)
      end
    end
  end
end