Class: NoBrainer::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.



7
8
9
# File 'lib/matchers/validations/format_of.rb', line 7

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

Instance Method Details

#descriptionObject



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

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)


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
56
57
# File 'lib/matchers/validations/format_of.rb', line 26

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



21
22
23
24
# File 'lib/matchers/validations/format_of.rb', line 21

def not_to_allow(invalid_value)
  @invalid_value = invalid_value
  self
end

#to_allow(valid_value) ⇒ Object



16
17
18
19
# File 'lib/matchers/validations/format_of.rb', line 16

def to_allow(valid_value)
  @valid_value = valid_value
  self
end

#with_format(format) ⇒ Object



11
12
13
14
# File 'lib/matchers/validations/format_of.rb', line 11

def with_format(format)
  @format = format
  self
end