Class: RspecSequel::Validation

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

Instance Method Summary collapse

Methods inherited from Base

#failure_message, #failure_message_when_negated, #hash_to_nice_string, #matches?

Constructor Details

#initialize(*args) ⇒ Validation

Returns a new instance of Validation.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rspec_sequel/validation.rb', line 5

def initialize(*args)
  # initialize Base
  options = args.last.is_a?(Hash) ? args.pop : {}
  super(args.pop, options)

  # check additionnal param
  if additionnal_param_required?
    if args.size>1
      raise ArgumentError, "too many params for matcher"
    else
      @additionnal = args.pop
      raise ArgumentError, "expected matcher first parameter to be #{additionnal_param_type.inspect} but received #{@additionnal.class.inspect} instead" unless @additionnal.kind_of?(additionnal_param_type)
    end
  else
    raise ArgumentError, "too many params for matcher" unless args.empty?
  end
end

Instance Method Details

#additionnal_param_required?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rspec_sequel/validation.rb', line 27

def additionnal_param_required?
  additionnal_param_type!=NilClass
end

#additionnal_param_typeObject



23
24
25
# File 'lib/rspec_sequel/validation.rb', line 23

def additionnal_param_type
  NilClass
end

#args_to_called_attributes(args) ⇒ Object



71
72
73
# File 'lib/rspec_sequel/validation.rb', line 71

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

#valid?(db, i, c, attribute, options) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rspec_sequel/validation.rb', line 35

def valid?(db, i, c, attribute, options)
  # check options
  invalid_options = options.keys.reject{|o| valid_options.include?(o)}
  invalid_options.each{|o|
    @suffix << "but option #{o.inspect} is not valid"
  }
  return false unless invalid_options.empty?

  # check validation itself
  called_count = 0
  i = i.dup
  i.class.columns # ensure colums are read again after .dup
  allow(i).to receive(validation_type) 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_count += 1
      end
    end
  end
  i.valid?
  if called_count>1
    @suffix << "but validation is called too many times"
    return false
  end
  called_count==1
end

#valid_optionsObject



31
32
33
# File 'lib/rspec_sequel/validation.rb', line 31

def valid_options
  [:allow_blank, :allow_missing, :allow_nil, :message]
end