class Adapter::ActiveModel::Collection

Attributes

options[R]

Public Class Methods

new(options, response, _binding) click to toggle source
# File lib/rspec/api_helpers/adapter/active_model/collection.rb, line 5
def initialize(options, response, _binding)
  @options = HashWithIndifferentAccess.new(options)
  @response = response
  @_binding = _binding
end

Public Instance Methods

find_by(id, new_options = {}) click to toggle source
# File lib/rspec/api_helpers/adapter/active_model/collection.rb, line 21
def find_by(id, new_options = {})
  resource = collection.find{|i| i.send(id).to_s == model.send(id).to_s}
  if resource.nil?
    raise "Resource was not found for id: #{id} (model value: #{model.send(id)})"
  end

  return Adapter::ActiveModel::Resource.new(
    options.merge(new_options), resource.hash, @_binding
  )
end
has_size(size) click to toggle source
# File lib/rspec/api_helpers/adapter/active_model/collection.rb, line 11
def has_size(size)
  expect(collection.size).to(eq size)
end
sample(new_options = {}) click to toggle source
# File lib/rspec/api_helpers/adapter/active_model/collection.rb, line 15
def sample(new_options = {})
  Adapter::ActiveModel::Resource.new(
    options.merge(new_options), collection.sample.hash, @_binding
  )
end

Private Instance Methods

collection() click to toggle source
# File lib/rspec/api_helpers/adapter/active_model/collection.rb, line 41
def collection
  @collection ||= objectize_collection(
    @response, root: options[:resource]
  )
end
model() click to toggle source
# File lib/rspec/api_helpers/adapter/active_model/collection.rb, line 35
def model
  @model ||= parse_model(
    @_binding.instance_exec(&options[:model])
  )
end