class Shoulda::Matchers::ActiveRecord::AssociationMatchers::OptionVerifier

@private

Constants

DEFAULT_VALUE_OF_OPTIONS
RELATION_OPTIONS

Attributes

reflector[R]

Public Class Methods

new(reflector) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 16
def initialize(reflector)
  @reflector = reflector
end

Public Instance Methods

actual_value_for(name) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 55
def actual_value_for(name)
  if RELATION_OPTIONS.include?(name)
    actual_value_for_relation_clause(name)
  else
    method_name = "actual_value_for_#{name}"
    if respond_to?(method_name, true)
      __send__(method_name)
    else
      actual_value_for_option(name)
    end
  end
end
correct_for?(*args) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 40
def correct_for?(*args)
  expected_value, name, type = args.reverse

  if expected_value.nil?
    true
  else
    type_cast_expected_value = type_cast(
      type,
      expected_value_for(type, name, expected_value),
    )
    actual_value = type_cast(type, actual_value_for(name))
    type_cast_expected_value == actual_value
  end
end
correct_for_boolean?(name, expected_value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 24
def correct_for_boolean?(name, expected_value)
  correct_for?(:boolean, name, expected_value)
end
correct_for_constant?(name, expected_unresolved_value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 32
def correct_for_constant?(name, expected_unresolved_value)
  correct_for?(:constant, name, expected_unresolved_value)
end
correct_for_hash?(name, expected_value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 28
def correct_for_hash?(name, expected_value)
  correct_for?(:hash, name, expected_value)
end
correct_for_relation_clause?(name, expected_value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 36
def correct_for_relation_clause?(name, expected_value)
  correct_for?(:relation_clause, name, expected_value)
end
correct_for_string?(name, expected_value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 20
def correct_for_string?(name, expected_value)
  correct_for?(:string, name, expected_value)
end

Protected Instance Methods

actual_value_for_class_name() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 121
def actual_value_for_class_name
  reflector.associated_class
end
actual_value_for_option(name) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 129
def actual_value_for_option(name)
  option_value = reflection.options[name]

  if option_value.nil?
    DEFAULT_VALUE_OF_OPTIONS.dig(reflection.macro, name)
  else
    option_value
  end
end
actual_value_for_relation_clause(name) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 114
def actual_value_for_relation_clause(name)
  reflector.extract_relation_clause_from(
    reflector.association_relation,
    name,
  )
end
actual_value_for_strict_loading() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 125
def actual_value_for_strict_loading
  reflection.strict_loading?
end
expected_value_for(type, name, value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 85
def expected_value_for(type, name, value)
  if RELATION_OPTIONS.include?(name)
    expected_value_for_relation_clause(name, value)
  elsif type == :constant
    expected_value_for_constant(value)
  else
    value
  end
end
expected_value_for_constant(name) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 100
def expected_value_for_constant(name)
  namespace = Shoulda::Matchers::Util.deconstantize(
    reflector.model_class.to_s,
  )

  ["#{namespace}::#{name}", name].each do |path|
    constant = Shoulda::Matchers::Util.safe_constantize(path)

    if constant
      return constant
    end
  end
end
expected_value_for_relation_clause(name, value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 95
def expected_value_for_relation_clause(name, value)
  relation = reflector.build_relation_with_clause(name, value)
  reflector.extract_relation_clause_from(relation, name)
end
type_cast(type, value) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb, line 72
def type_cast(type, value)
  case type
  when :string, :relation_clause
    value.to_s
  when :boolean
    !!value
  when :hash
    Hash(value).stringify_keys
  else
    value
  end
end