Class: ActiveModel::Serializers::Matchers::AssociationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers/matchers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ AssociationMatcher

Returns a new instance of AssociationMatcher.



128
129
130
# File 'lib/active_model_serializers/matchers.rb', line 128

def initialize(name)
  @name = name
end

Instance Attribute Details

#actualObject

Returns the value of attribute actual.



126
127
128
# File 'lib/active_model_serializers/matchers.rb', line 126

def actual
  @actual
end

#keyObject

Returns the value of attribute key.



126
127
128
# File 'lib/active_model_serializers/matchers.rb', line 126

def key
  @key
end

#nameObject

Returns the value of attribute name.



126
127
128
# File 'lib/active_model_serializers/matchers.rb', line 126

def name
  @name
end

Instance Method Details

#as(value) ⇒ Object



148
149
150
151
# File 'lib/active_model_serializers/matchers.rb', line 148

def as(value)
  self.key = value
  self
end

#descriptionObject



153
154
155
# File 'lib/active_model_serializers/matchers.rb', line 153

def description
  "have attribute #{name}"
end

#failure_message_for_shouldObject



157
158
159
# File 'lib/active_model_serializers/matchers.rb', line 157

def failure_message_for_should
  %Q{expected #{actual.inspect} to include a "#{name}" association, but it did not}
end

#failure_message_for_should_notObject



161
162
163
# File 'lib/active_model_serializers/matchers.rb', line 161

def failure_message_for_should_not
  %Q{expected #{actual.inspect} to not include a "#{name}" association, but it did}
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/active_model_serializers/matchers.rb', line 132

def matches?(actual)
  @actual = actual

  matched_association = associations.select do |assc|
    assc.name == name
  end.first

  return false unless matched_association

  if key
    return false if matched_association.key != key
  end

  true
end