Sha256: 77d840fa24034b45bc63cb9594d614aecdf858673c52cb0e12052e426903b01b

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Zendesk2::PagedCollection
  def self.included(klass)
    klass.send(:attribute, :count)
    klass.send(:attribute, :next_page_link, {:aliases => "next_page"})
    klass.send(:attribute, :previous_page_link, {:aliases => "previous_page"})
    klass.send(:extend, Zendesk2::PagedCollection::Attributes)
  end

  def collection_method; self.class.collection_method; end
  def collection_root; self.class.collection_root; end
  def model_method; self.class.model_method; end
  def model_root; self.class.model_root; end

  def all(params={})
    body = connection.send(collection_method, params).body

    collection = self.clone.load(body[collection_root])
    collection.merge_attributes(Cistern::Hash.slice(body, "count", "next_page", "previous_page"))
    collection
  end

  def get(id)
    if data = self.connection.send(model_method, {"id" => id}).body[self.model_root]
      new(data)
    end
  end

  def next_page
    all("url" => next_page_link) if next_page_link
  end

  def previous_page
    all("url" => previous_page_link) if previous_page_link
  end

  module Attributes
    attr_accessor :collection_method, :collection_root, :model_method, :model_root
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zendesk2-0.0.5 lib/zendesk2/paged_collection.rb