Sha256: 517ac49558da82e64b81a64f9b6dec799a272d4a747827c63337bf18230cf95c

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

class Adapter::ActiveModel::Collection
  include Adapter::ActiveModel::CommonHelpers
  include RSpec::Matchers

  def initialize(options, response, _binding)
    @options = HashWithIndifferentAccess.new(options)
    @response = response
    @_binding = _binding
  end

  def has_size(size)
    expect(collection.size).to(eq size)
  end

  def sample(new_options = {})
    Adapter::ActiveModel::Resource.new(
      options.merge(new_options), collection.sample.hash, @_binding
    )
  end

  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

  private
    attr_reader :options

    def model
      @model ||= parse_model(
        @_binding.instance_exec(&options[:model])
      )
    end

    def collection
      @collection ||= objectize_collection(
        @response, root: options[:resource]
      )
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-api_helpers-1.0.4 lib/rspec/api_helpers/adapter/active_model/collection.rb
rspec-api_helpers-1.0.3 lib/rspec/api_helpers/adapter/active_model/collection.rb
rspec-api_helpers-1.0.2 lib/rspec/api_helpers/adapter/active_model/collection.rb