Class: Adapter::ActiveModel::Resource

Inherits:
Object
  • Object
show all
Includes:
CommonHelpers, RSpec::Matchers
Defined in:
lib/rspec/api_helpers/adapter/active_model/resource.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) ⇒ Resource

Returns a new instance of Resource.



5
6
7
8
9
# File 'lib/rspec/api_helpers/adapter/active_model/resource.rb', line 5

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

Instance Method Details

#compare_attribute(attribute) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rspec/api_helpers/adapter/active_model/resource.rb', line 17

def compare_attribute(attribute)
  expect(resource.send(attribute)).to(
    eql(
      modifier_for(attribute).call(
        model.send(attribute)
      )
    )
  )
end

#embedded_collection(new_options = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rspec/api_helpers/adapter/active_model/resource.rb', line 27

def embedded_collection(new_options = {})
  raise 'embeds option missing' unless (options[:embeds] || new_options[:embeds])

  Adapter::ActiveModel::Collection.new(
    options.merge(new_options), resource.send(options[:embeds]), @_binding
  )
end

#has_attribute(attribute) ⇒ Object



39
40
41
# File 'lib/rspec/api_helpers/adapter/active_model/resource.rb', line 39

def has_attribute(attribute)
  expect(resource.send(attribute)).to_not(eql(:attribute_not_found))
end

#has_no_attribute(attribute) ⇒ Object



35
36
37
# File 'lib/rspec/api_helpers/adapter/active_model/resource.rb', line 35

def has_no_attribute(attribute)
  expect(resource.send(attribute)).to(eql(:attribute_not_found))
end

#resourceObject



11
12
13
14
15
# File 'lib/rspec/api_helpers/adapter/active_model/resource.rb', line 11

def resource
  @resource ||= objectize_resource(
    @response, root: options[:resource], existing: options[:existing]
  )
end