Class: JSONAPI::Resources::Matchers::HaveMany

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/resources/matchers/have_many.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ HaveMany

Returns a new instance of HaveMany.



8
9
10
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 8

def initialize(name)
  self.name = name
end

Instance Attribute Details

#expected_class_nameObject

Returns the value of attribute expected_class_name.



6
7
8
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 6

def expected_class_name
  @expected_class_name
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 6

def name
  @name
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 6

def resource
  @resource
end

Instance Method Details

#descriptionObject



12
13
14
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 12

def description
  "have many `#{name}`"
end

#failure_messageObject



36
37
38
39
40
41
42
43
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 36

def failure_message
  resource_name = resource.class.name.demodulize
  message = ["expected `#{resource_name}` to have many `#{name}`"]
  if self.expected_class_name
    message << "with class name `#{self.expected_class_name}`"
  end
  message.join(" ")
end

#has_key_in_relationships?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 22

def has_key_in_relationships?
  serialized_hash = JSONAPI::ResourceSerializer.new(resource.class).
    serialize_to_hash(resource).with_indifferent_access
  expected_key = name.to_s.dasherize
  relationships = serialized_hash["data"]["relationships"]
  return false if relationships.nil?
  relationships.has_key?(expected_key)
end

#matches?(resource) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 16

def matches?(resource)
  self.resource = resource

  has_key_in_relationships? && matches_class_name?
end

#matches_class_name?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 45

def matches_class_name?
  return true if self.expected_class_name.nil?
  association = resource.class._relationships[name]
  actual_class_name = association.class_name
  self.expected_class_name == actual_class_name
end

#with_class_name(name) ⇒ Object



31
32
33
34
# File 'lib/jsonapi/resources/matchers/have_many.rb', line 31

def with_class_name(name)
  self.expected_class_name = name
  self
end