class Easy::Matchers::Validations::HaveValidationMatcher

Public Class Methods

new(field, type) click to toggle source
# File lib/matchers/validations.rb, line 5
def initialize(field, type)
  @field = field
  @type = type
  @options = {}
end

Public Instance Methods

check_on() click to toggle source
# File lib/matchers/validations.rb, line 49
def check_on
  validator_on_methods = [@validator.options[:on]].flatten

  if validator_on_methods.any?
    message = " on methods: #{ validator_on_methods }"

    if on_options_covered_by?(@validator)
      @positive_result_message << message
    else
      @negative_result_message << message
      @result = false
    end
  end
end
description() click to toggle source
# File lib/matchers/validations.rb, line 38
def description
  desc = "have #{ @type } validator on #{ @field }"
  desc << " on #{ @options[:on] }" if @options[:on]
  desc
end
failure_message() click to toggle source
# File lib/matchers/validations.rb, line 30
def failure_message
  "Expected #{ @klass.inspect } to #{ description }; instead got #{ @negative_result_message }"
end
failure_message_when_negated() click to toggle source
# File lib/matchers/validations.rb, line 34
def failure_message_when_negated
  "Expected #{ @klass.inspect } to not #{ description }; instead got #{ @positive_result_message }"
end
matches?(actual) click to toggle source
# File lib/matchers/validations.rb, line 11
def matches?(actual)
  @klass = actual.is_a?(Class) ? actual : actual.class

  @validator = @klass.validators_on(@field).detect{ |v|
    v.kind == @type && (!v.options[:on] || on_options_matches?(v))
  }

  if @validator
    @negative_result_message = "#{ @type.inspect } validator on #{ @field.inspect }"
    @positive_result_message = "#{ @type.inspect } validator on #{ @field.inspect }"
  else
    @negative_result_message = "no #{ @type.inspect } validator on #{ @field.inspect }"
    return false
  end
  @result = true
  check_on if @options[:on]
  @result
end
on(*on_method) click to toggle source
# File lib/matchers/validations.rb, line 44
def on(*on_method)
  @options[:on] = on_method.flatten
  self
end

Private Instance Methods

on_options_covered_by?(validator) click to toggle source
# File lib/matchers/validations.rb, line 70
def on_options_covered_by?(validator)
  ([@options[:on]].flatten - [validator.options[:on]].flatten).empty?
end
on_options_matches?(validator) click to toggle source
# File lib/matchers/validations.rb, line 66
def on_options_matches?(validator)
  @options[:on] && validator.options[:on] && on_options_covered_by?(validator)
end