Class: Adapter::ActiveModel::Collection

Inherits:
Object
  • Object
show all
Includes:
CommonHelpers, RSpec::Matchers
Defined in:
lib/rspec/api_helpers/adapter/active_model/collection.rb

Instance Method Summary collapse

Methods included from CommonHelpers

#modifier_for, #object_hash, #objectize_collection, #objectize_resource, #parse_model, #superset_mismatch_error

Constructor Details

#initialize(options, response, _binding) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
# 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

Instance Method Details

#find_by(id, new_options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



11
12
13
# 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 = {}) ⇒ Object



15
16
17
18
19
# 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