class Cubits::ResourceCollection

Attributes

path[R]
resource[R]

Public Class Methods

new(params = {}) click to toggle source
# File lib/cubits/resource_collection.rb, line 7
def initialize(params = {})
  @path = params[:path]
  @resource = params[:resource]
  @exposed_methods = params[:expose_methods]
end

Public Instance Methods

all() click to toggle source

Loads collection of resources

@return [Array<Resource>]

# File lib/cubits/resource_collection.rb, line 36
def all
  fail NoMethodError, "Resource #{name} does not expose .all" unless exposed_method?(:all)
  list = []
  page_count.times do |i|
    list += page(i + 1)
  end
  list
rescue NotFound
  []
end
collection_name() click to toggle source

By convention collection name for the resource is the last part of the path

# File lib/cubits/resource_collection.rb, line 15
def collection_name
  path.split('/').last
end
count() click to toggle source

Returns number of elements in the collection

# File lib/cubits/resource_collection.rb, line 27
def count
  pagination.total_count
end
Also aliased as: size
each() { |r| ... } click to toggle source

Iterates through the collection, yielding the giving block for each resource.

# File lib/cubits/resource_collection.rb, line 84
def each(&_block)
  return to_enum unless block_given?
  page_count.times do |i|
    page(i + 1).each do |r|
      yield r
    end
  end
end
exposed_method?(method_name) click to toggle source

@return true if the method is exposed by this resource

# File lib/cubits/resource_collection.rb, line 21
def exposed_method?(method_name)
  (@exposed_methods || []).include?(method_name)
end
find(id) click to toggle source

Loads resource

@param id [String]

@return nil if resource is not found

# File lib/cubits/resource_collection.rb, line 65
def find(id)
  fail NoMethodError, "Resource #{name} does not expose .find" unless exposed_method?(:find)
  resource.new Cubits.connection.get(path_to(id))
rescue NotFound
  nil
end
first() click to toggle source

Returns first element of the collection

# File lib/cubits/resource_collection.rb, line 49
def first
  first_page.first
end
last() click to toggle source

Returns last element of the collection

# File lib/cubits/resource_collection.rb, line 55
def last
  last_page.last
end
name() click to toggle source
# File lib/cubits/resource_collection.rb, line 106
def name
  "Collection of #{resource.name}"
end
path_to(resource_or_id = nil) click to toggle source

Returns API path to resource

# File lib/cubits/resource_collection.rb, line 95
def path_to(resource_or_id = nil)
  fail ArgumentError, "Resource path is not set for #{self}" unless path
  if resource_or_id.is_a?(Resource)
    "#{path}/#{resource_or_id.id}"
  elsif resource_or_id
    "#{path}/#{resource_or_id}"
  else
    path
  end
end
reload() click to toggle source

Reloads collection

@return [self]

# File lib/cubits/resource_collection.rb, line 76
def reload
  @pagination = nil
  @pages = nil
  self
end
size()
Alias for: count
to_s() click to toggle source
# File lib/cubits/resource_collection.rb, line 110
def to_s
  "<#{self.class.name} of #{resource.name}, #{path}>"
end

Private Instance Methods

first_page() click to toggle source

Returns first page of the collection

# File lib/cubits/resource_collection.rb, line 158
def first_page
  page(1)
end
last_page() click to toggle source

Returns last page of the collection

# File lib/cubits/resource_collection.rb, line 164
def last_page
  page(page_count)
end
load_page(i) click to toggle source

Loads i-th page

# File lib/cubits/resource_collection.rb, line 150
def load_page(i)
  response = Cubits.connection.get(path_to, page: i)
  @pagination = Hashie::Mash.new(response['pagination'])
  response[collection_name].map { |r| resource.new r }
end
page(i) click to toggle source

Returns i-th page

# File lib/cubits/resource_collection.rb, line 144
def page(i)
  pages[i] ||= load_page(i)
end
page_count() click to toggle source

Returns number of pages

# File lib/cubits/resource_collection.rb, line 127
def page_count
  pagination.page_count
end
pages() click to toggle source

Returns collection of pages as a Hash: {

<page_number> => [<resource>, ...],
<page_number> => [<resource>, ...],
...

}

# File lib/cubits/resource_collection.rb, line 138
def pages
  @pages ||= {}
end
pagination() click to toggle source

Returns current pagination object. If pagination was not requested yet, does a one page request

# File lib/cubits/resource_collection.rb, line 119
def pagination
  return @pagination if @pagination
  first_page
  @pagination
end