Class: SequelSpec::Matchers::Validation::ValidateMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/sequel_spec/validation/validate_matcher.rb

Instance Method Summary collapse

Methods inherited from Base

#failure_message, #hash_to_nice_string, #initialize, #matches?, #negative_failure_message, #with_options

Constructor Details

This class inherits a constructor from SequelSpec::Matchers::Base

Instance Method Details

#additionnal_param_required?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/sequel_spec/validation/validate_matcher.rb', line 57

def additionnal_param_required?
  false
end

#allowing_blankObject



45
46
47
# File 'lib/sequel_spec/validation/validate_matcher.rb', line 45

def allowing_blank
  with_options :allow_blank => true
end

#allowing_missingObject



49
50
51
# File 'lib/sequel_spec/validation/validate_matcher.rb', line 49

def allowing_missing
  with_options :allow_missing => true
end

#allowing_nilObject



41
42
43
# File 'lib/sequel_spec/validation/validate_matcher.rb', line 41

def allowing_nil
  with_options :allow_nil => true
end

#args_to_called_attributes(args) ⇒ Object



61
62
63
# File 'lib/sequel_spec/validation/validate_matcher.rb', line 61

def args_to_called_attributes(args)
  [args.pop].flatten
end

#valid?(db, instance, klass, attribute, options) ⇒ Boolean

Returns:

  • (Boolean)


5
6
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
# File 'lib/sequel_spec/validation/validate_matcher.rb', line 5

def valid?(db, instance, klass, attribute, options)
  additionnal_param_check if self.respond_to?(:additionnal_param_check)

  instance = instance.dup
  instance.class.columns

  called = false

  proxy = Proc.new do |args|
    called_options = args.last.is_a?(Hash) ? args.pop : {}
    called_attributes = args_to_called_attributes(args)
    called_additionnal = args.shift if additionnal_param_required?

    if !args.empty?
      @suffix << "but called with too many params"
    elsif called_attributes.include?(attribute)
      if additionnal_param_required? && @additionnal != called_additionnal
        @suffix << "but called with #{called_additionnal} instead"
      elsif !options.empty? && called_options != options
        @suffix << "but called with option(s) #{hash_to_nice_string called_options} instead"
      else
        called = true
      end
    end
  end

  instance.singleton_class.send(:define_method, validation_type,
    Proc.new { |*args| proxy.call(args) }
  )

  # Run validations
  instance.valid?

  called
end

#with_message(message) ⇒ Object



53
54
55
# File 'lib/sequel_spec/validation/validate_matcher.rb', line 53

def with_message(message)
  with_options :message => message
end