module SparkApi::Models::Finders

Rails-like finders module

Adds the base set of finder class methods to the models that support them (not all of them do)

Public Instance Methods

find(*arguments) click to toggle source
# File lib/spark_api/models/finders.rb, line 7
def find(*arguments)
  scope = arguments.slice!(0)
  options = arguments.slice!(0) || {}
  case scope
    when nil    then raise ArgumentError, "Argument for find() can't be nil"
    when :all   then find_every(options)
    when :first then find_every(options).first
    when :last  then find_every(options).last
    when :one   then find_every(options.merge(:_limit => 1)).first
    else             find_single(scope, options)
  end
end
find_one(*arguments) click to toggle source
# File lib/spark_api/models/finders.rb, line 20
def find_one(*arguments)
  find(:one, *arguments)
end
first(*arguments) click to toggle source
# File lib/spark_api/models/finders.rb, line 24
def first(*arguments)
  find(:first, *arguments)
end
last(*arguments) click to toggle source
# File lib/spark_api/models/finders.rb, line 28
def last(*arguments)
  find(:last, *arguments)
end

Private Instance Methods

find_every(options) click to toggle source
# File lib/spark_api/models/finders.rb, line 34
def find_every(options)
  collect(connection.get("#{path}", options))
end
find_single(scope, options) click to toggle source
# File lib/spark_api/models/finders.rb, line 38
def find_single(scope, options)
  resp = connection.get("#{path}/#{scope}", options)
  new(resp.first)
end