Class: Mongoid::Matchers::Validations::ValidateFormatOfMatcher

Inherits:
HaveValidationMatcher show all
Defined in:
lib/matchers/validations/format_of.rb

Instance Method Summary collapse

Methods inherited from HaveValidationMatcher

#failure_message_for_should, #failure_message_for_should_not, #on, #with_message

Constructor Details

#initialize(field) ⇒ ValidateFormatOfMatcher

Returns a new instance of ValidateFormatOfMatcher.



5
6
7
# File 'lib/matchers/validations/format_of.rb', line 5

def initialize(field)
  super(field, :format)
end

Instance Method Details

#descriptionObject



57
58
59
60
61
62
63
# File 'lib/matchers/validations/format_of.rb', line 57

def description
  options_desc = []
  options_desc << " with format #{@format.inspect}" if @format
  options_desc << " allowing the value #{@valid_value.inspect}" if @valid_value
  options_desc << " not allowing the value #{@invalid_value.inspect}" if @invalid_value
  super << options_desc.to_sentence
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/matchers/validations/format_of.rb', line 24

def matches?(actual)
  return false unless result = super(actual)

  if @format
    if @validator.options[:with] == @format
      @positive_result_message = @positive_result_message << " with format #{@validator.options[:format].inspect}"
    else
      @negative_result_message = @negative_result_message << " with format #{@validator.options[:format].inspect}"
      result = false
    end
  end

  if @valid_value
    if @validator.options[:with] =~ @valid_value
      @positive_result_message = @positive_result_message << " with #{@valid_value.inspect} as a valid value"
    else
      @negative_result_message = @negative_result_message << " with #{@valid_value.inspect} as an invalid value"
      result = false
    end
  end

  if @invalid_value
    if @invalid_value !~ @validator.options[:with]
      @positive_result_message = @positive_result_message << " with #{@invalid_value.inspect} as an invalid value"
    else
      @negative_result_message = @negative_result_message << " with #{@invalid_value.inspect} as a valid value"
      result = false
    end
  end

  result
end

#not_to_allow(invalid_value) ⇒ Object



19
20
21
22
# File 'lib/matchers/validations/format_of.rb', line 19

def not_to_allow(invalid_value)
  @invalid_value = invalid_value
  self
end

#to_allow(valid_value) ⇒ Object



14
15
16
17
# File 'lib/matchers/validations/format_of.rb', line 14

def to_allow(valid_value)
  @valid_value = valid_value
  self
end

#with_format(format) ⇒ Object



9
10
11
12
# File 'lib/matchers/validations/format_of.rb', line 9

def with_format(format)
  @format = format
  self
end