class Shoulda::Matchers::ActiveRecord::AssociationMatchers::OptionalMatcher

@private

def build_missing_option

def build_missing_option
  String.new('and for the record ').tap do |missing_option_string|
    missing_option_string <<
      if optional
        'not to '
      else
        'to '
      end
    missing_option_string << (
      'fail validation if '\
      ":#{attribute_name} is unset; i.e., either the association "\
      'should have been defined with `optional: '\
      "#{optional.inspect}`, or there "
    )
    missing_option_string <<
      if optional
        'should not '
      else
        'should '
      end
    missing_option_string << "be a presence validation on :#{attribute_name}"
  end
end

def description

def description
  "optional: #{optional}"
end

def initialize(attribute_name, optional)

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

def matches?(subject)

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

def submatcher_passes?(subject)

def submatcher_passes?(subject)
  if optional
    submatcher.matches?(subject)
  else
    submatcher.does_not_match?(subject)
  end
end