module Datmachine::Resource::ClassMethods

Public Instance Methods

all(options = {}) click to toggle source
# File lib/datmachine/resources/resource.rb, line 285
def all(options = {})
  pager = paginate(options)
  pager.to_a
end
collection_name() click to toggle source
# File lib/datmachine/resources/resource.rb, line 201
def collection_name
  Utils.pluralize Utils.underscore(resource_name)
end
collection_path() click to toggle source
# File lib/datmachine/resources/resource.rb, line 205
def collection_path
  # this is to just support version1 stuff, but in reality, if we get here
  # we should throw an exception since we do not want to use v1 ever.
  if Datmachine.config[:version] == '1'
    # XXX: we should raise an exception if we get here.
    ["/v#{Datmachine.config[:version]}", collection_name].compact.join '/'
  else
    collection_name
  end
end
construct_from_response(payload) click to toggle source
# File lib/datmachine/resources/resource.rb, line 224
def construct_from_response(payload)
  payload = Datmachine::Utils.indifferent_read_access payload

  links = payload.delete('links') || {}
  meta = payload.delete('meta') || {}

  if payload.length > 1
    raise 'NOT SUPPORTED YET'
  end

  instance = nil
  # the remaining keys here are just hypermedia resources
  payload.each do |key, value|
    # > If the length of an array at a resource key is greater than one,
    # the value represents a list of documents.
    if value.length > 1
      # key is a collection, this should never happen
      resource_body = value
    else
      resource_body = value.first
    end
    # > Singular resources are represented as JSON objects. However,
    # they are still wrapped inside an array:
    #resource_body = value.first
    cls = ("Datmachine::" + Datmachine::Utils.classify(key)).constantize
    instance = cls.new resource_body
    instance.hydrate(links, meta)
  end
  instance
  
end
fetch(*arguments) click to toggle source
# File lib/datmachine/resources/resource.rb, line 256
def fetch(*arguments)
  if arguments.nil? ||
     arguments.empty? ||
     arguments[0].nil? ||
     arguments[0].empty?
    
  end
  scope = arguments.slice!(0)
  options = arguments.slice!(0) || {}
  case scope
    when :all then
      all(options)
    when :first then
      paginate(options).first
    else
      response = Datmachine.get scope, options
      construct_from_response response.body
  end
end
Also aliased as: find
find(*arguments)
Alias for: fetch
href() click to toggle source
# File lib/datmachine/resources/resource.rb, line 216
def href
  collection_path
end
member_name() click to toggle source
# File lib/datmachine/resources/resource.rb, line 220
def member_name
  Utils.underscore resource_name
end
paginate(options = {}) click to toggle source
# File lib/datmachine/resources/resource.rb, line 278
def paginate(options = {})
  Datmachine::Pager.new href, options
end
Also aliased as: scoped, where
resource_name() click to toggle source
# File lib/datmachine/resources/resource.rb, line 197
def resource_name
  Utils.demodulize name
end
scoped(options = {})
Alias for: paginate
where(options = {})
Alias for: paginate