Class: Shoulda::Matchers::ActiveRecord::AssociationMatchers::OptionVerifier

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

Constant Summary collapse

DEFAULT_VALUE_OF_OPTIONS =
{
  has_many: {
    validate: true,
  },
}.freeze
RELATION_OPTIONS =
[:conditions, :order].freeze

Instance Method Summary collapse

Constructor Details

#initialize(reflector) ⇒ OptionVerifier

Returns a new instance of OptionVerifier.



16
17
18
# File 'lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb', line 16

def initialize(reflector)
  @reflector = reflector
end

Instance Method Details

#actual_value_for(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# 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) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# 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) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# 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) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# 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) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# 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