class Ey::Core::Collection

Attributes

collection_request[RW]
collection_root[RW]
model_request[RW]
model_root[RW]

Public Instance Methods

==(comparison_object) click to toggle source
# File lib/ey-core/collection.rb, line 63
def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.is_a?(self.class) &&
     comparison_object.map(&:identity) == self.map(&:identity))
end
all(params={}) click to toggle source
# File lib/ey-core/collection.rb, line 69
def all(params={})
  params["url"] ||= self.url

  self.load(
    self.connection.send(self.collection_request, Cistern::Hash.stringify_keys(params))
  )
end
collection_request() click to toggle source
# File lib/ey-core/collection.rb, line 47
def collection_request
  self.class.instance_variable_get(:@collection_request)
end
collection_root() click to toggle source
# File lib/ey-core/collection.rb, line 43
def collection_root
  self.class.instance_variable_get(:@collection_root)
end
create!(*args) click to toggle source
# File lib/ey-core/collection.rb, line 12
def create!(*args)
  model = self.new(*args)
  model.save!
end
each_entry() { |r| ... } click to toggle source
# File lib/ey-core/collection.rb, line 101
def each_entry
  return to_enum(:each_entry) unless block_given?
  page = self
  while page
    page.to_a.each { |r| yield r }
    page = page.next_page
  end
end
each_page() { |page| ... } click to toggle source
# File lib/ey-core/collection.rb, line 92
def each_page
  return to_enum(:each_page) unless block_given?
  page = self
  while page
    yield page
    page = page.next_page
  end
end
first(params={}) click to toggle source
Calls superclass method
# File lib/ey-core/collection.rb, line 77
def first(params={})
  params.empty? ? super() : all(params).to_a.first
end
get(id) click to toggle source
# File lib/ey-core/collection.rb, line 37
def get(id)
  get!(id)
rescue Ey::Core::Response::NotFound
  nil
end
get!(id) click to toggle source
# File lib/ey-core/collection.rb, line 29
def get!(id)
  if data = perform_get("id" => id)
    new(data)
  else
    nil
  end
end
last_page() click to toggle source
# File lib/ey-core/collection.rb, line 59
def last_page
  all("url" => self.last_link)
end
load(data) click to toggle source
Calls superclass method
# File lib/ey-core/collection.rb, line 123
def load(data)
  if data.is_a?(Ey::Core::Response)
    self.merge_attributes(page_parameters(data))
    super(data.body[self.collection_root])
  else
    super
  end
end
model_request() click to toggle source
# File lib/ey-core/collection.rb, line 21
def model_request
  self.class.model_request
end
model_root() click to toggle source
# File lib/ey-core/collection.rb, line 17
def model_root
  self.class.model_root
end
new_page() click to toggle source
# File lib/ey-core/collection.rb, line 119
def new_page
  self.class.new(connection: self.connection)
end
next_page() click to toggle source
# File lib/ey-core/collection.rb, line 51
def next_page
  new_page.all("url" => self.next_link) if self.next_link
end
one() click to toggle source

asserts the results contain only a single record and returns it

# File lib/ey-core/collection.rb, line 82
def one
  if size == 0
    raise "Could not find any records"
  elsif size > 1
    raise "Search returned multiple records when only one was expected"
  else
    first
  end
end
page_parameters(response) click to toggle source
# File lib/ey-core/collection.rb, line 110
def page_parameters(response)
  links = (response.headers['Link'] || "").split(", ").inject({}) do |r, link|
    value, key = link.match(/<(.*)>; rel="(\w+)"/).captures
    r.merge("#{key}_link" => value)
  end

  links.merge(total_count: response.headers['X-Total-Count'])
end
perform_get(params) click to toggle source
# File lib/ey-core/collection.rb, line 25
def perform_get(params)
  connection.send(self.model_request, params).body[self.model_root]
end
previous_page() click to toggle source
# File lib/ey-core/collection.rb, line 55
def previous_page
  new_page.all("url" => self.prev_link) if self.prev_link
end