Class: Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher

Inherits:
ValidationMatcher show all
Defined in:
lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb

Overview

:nodoc:

Constant Summary

ARBITRARY_OUTSIDE_STRING =
'shouldamatchersteststring'

Instance Attribute Summary

Attributes inherited from ValidationMatcher

#failure_message_for_should

Instance Method Summary (collapse)

Methods inherited from ValidationMatcher

#failure_message_for_should_not, #on, #strict

Constructor Details

- (EnsureInclusionOfMatcher) initialize(attribute)

A new instance of EnsureInclusionOfMatcher



27
28
29
30
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 27

def initialize(attribute)
  super(attribute)
  @options = {}
end

Instance Method Details

- (Object) allow_blank(allow_blank = true)



44
45
46
47
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 44

def allow_blank(allow_blank = true)
  @options[:allow_blank] = allow_blank
  self
end

- (Object) allow_nil(allow_nil = true)



49
50
51
52
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 49

def allow_nil(allow_nil = true)
  @options[:allow_nil] = allow_nil
  self
end

- (Object) description



72
73
74
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 72

def description
  "ensure inclusion of #{@attribute} in #{inspect_message}"
end

- (Object) in_array(array)



32
33
34
35
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 32

def in_array(array)
  @array = array
  self
end

- (Object) in_range(range)



37
38
39
40
41
42
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 37

def in_range(range)
  @range = range
  @minimum = range.first
  @maximum = range.max
  self
end

- (Boolean) matches?(subject)

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 76

def matches?(subject)
  super(subject)

  if @range
    @low_message  ||= :inclusion
    @high_message ||= :inclusion

    disallows_lower_value &&
      allows_minimum_value &&
      disallows_higher_value &&
      allows_maximum_value
  elsif @array
    if allows_all_values_in_array? && allows_blank_value? && allows_nil_value? && disallows_value_outside_of_array?
      true
    else
      @failure_message_for_should = "#{@array} doesn't match array in validation"
      false
    end
  end
end

- (Object) with_high_message(message)



67
68
69
70
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 67

def with_high_message(message)
  @high_message = message if message
  self
end

- (Object) with_low_message(message)



62
63
64
65
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 62

def with_low_message(message)
  @low_message = message if message
  self
end

- (Object) with_message(message)



54
55
56
57
58
59
60
# File 'lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb', line 54

def with_message(message)
  if message
    @low_message = message
    @high_message = message
  end
  self
end