Class: Shoulda::Matchers::ActiveRecord::AssociationMatchers::OptionalMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name, optional) ⇒ OptionalMatcher

Returns a new instance of OptionalMatcher.



9
10
11
12
13
14
15
# File 'lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb', line 9

def initialize(attribute_name, optional)
  @attribute_name = attribute_name
  @optional = optional
  @submatcher = ActiveModel::AllowValueMatcher.new(nil).
    for(attribute_name)
  @missing_option = ''
end

Instance Attribute Details

#missing_optionObject (readonly)

Returns the value of attribute missing_option.



7
8
9
# File 'lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb', line 7

def missing_option
  @missing_option
end

Instance Method Details

#descriptionObject



17
18
19
# File 'lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb', line 17

def description
  "optional: #{optional}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb', line 21

def matches?(subject)
  if submatcher_passes?(subject)
    true
  else
    @missing_option = build_missing_option

    false
  end
end