Module: Validator::Matchers::ModelSpecHelper

Defined in:
lib/validator/matchers/model_spec_helper.rb

Overview

FIXME: This is really brittle, and poorly written in general. Will replace shortly.

Constant Summary collapse

VALIDATOR_REGEX =
%r{^(?:validate_)(.+)_of}

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/validator/matchers/model_spec_helper.rb', line 7

def self.included(base)
  base.class_eval do
    def respond_to_missing?(method_name, *)
      (method_name =~ VALIDATOR_REGEX) || super
    end

    def method_missing(method_name, *args, &block)
      if regex = method_name.to_s.match(VALIDATOR_REGEX)
        validator_name = regex[1]
        validator_class_name = validator_name.camelcase + 'Validator'

        RSpec::Matchers.define method_name do |attribute|
          match do |model|
            attribute = attribute.first if attribute.is_a?(Array)
            model_validators =
              model._validators[attribute].map{ |v| v.class.to_s }
            model_validators.include?(validator_class_name)
          end

          failure_message do |model|
            attribute = attribute.first if attribute.is_a?(Array)
            model.class.to_s + ' does not ' +
            method_name.to_s.humanize.downcase + ' :' + attribute.to_s
          end

          failure_message_when_negated do |model|
            attribute = attribute.first if attribute.is_a?(Array)
            model.class.to_s + ' does ' +
            method_name.to_s.humanize.downcase + ' :' + attribute.to_s
          end

          description do |model|
            attribute = attribute.first if attribute.is_a?(Array)
            method_name.to_s.humanize.downcase + ' :' + attribute.to_s
          end
        end

        self.__send__(method_name, args, block)
      else
        super
      end
    end
  end
end