Module: Validators

Included in:
Changeset
Defined in:
lib/changeset/validators.rb

Instance Method Summary collapse

Instance Method Details

#cast(*casted_keys) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/changeset/validators.rb', line 3

def cast(*casted_keys) 
  new_changes = {}
  @changes.each do |k, v|
    if casted_keys.include?(k)
      new_changes[k] = v
    end
  end
  @changes = new_changes
  @errors = @errors.select { |k, v| casted_keys.include?(k) }
 self
end

#validate_acceptance(key, message: nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/changeset/validators.rb', line 70

def validate_acceptance(key, message: nil)
  unless @changes[key] == true
    generate_error_for(key, :acceptance, message)
  end
  self
end

#validate_change(key, message: nil, &validator) ⇒ Object



111
112
113
114
115
116
# File 'lib/changeset/validators.rb', line 111

def validate_change(key, message: nil, &validator)
  unless validator.call(@changes[key])
    generate_error_for(key, :custom_validator, message)
  end
  self
end

#validate_confirmation(key, message: nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/changeset/validators.rb', line 55

def validate_confirmation(key, message: nil) 
  confirmation_key = "#{key}_confirmation".to_sym
  unless @changes[key] == @changes[confirmation_key]
    generate_error_for(key, :confirmation, message)
  end
  self
end

#validate_exclusion(key, exclusion, message: nil) ⇒ Object



97
98
99
100
101
102
# File 'lib/changeset/validators.rb', line 97

def validate_exclusion(key, exclusion, message: nil)
  if exclusion.include?(@changes[key])
    generate_error_for(key, :exclusion, message)
  end
  self
end

#validate_format(key, regex, message: nil) ⇒ Object



63
64
65
66
67
68
# File 'lib/changeset/validators.rb', line 63

def validate_format(key, regex, message: nil) 
  unless @changes[key].match?(regex)
    generate_error_for(key, :format, message)
  end
  self
end

#validate_inclusion(key, inclusion, message: nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/changeset/validators.rb', line 48

def validate_inclusion(key, inclusion, message: nil) 
  unless inclusion.include?(@changes[key])
    generate_error_for(key, :inclusion, message)
  end
  self
end

#validate_length(key, opts) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/changeset/validators.rb', line 28

def validate_length(key, opts)
  if opts.has_key?(:is)
    cond = @changes[key].length == opts[:is]
  elsif opts.has_key?(:in)
    cond = opts[:in].include? @changes[key].length
  elsif opts.has_key?(:min) && opts.has_key?(:max)
    cond = (opts[:min]..opts[:max]).include? @changes[key].length
  elsif opts.has_key?(:min)
    cond = @changes[key].length >= opts[:min]
  elsif opts.has_key?(:max)
    cond = @changes[key].length <= opts[:max]
  else
    raise "Should define at least one option for validate_length()"
  end
  unless cond
    generate_error_for(key, :length, opts[:message]) 
  end
  self
end

#validate_number(key, opts) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/changeset/validators.rb', line 77

def validate_number(key, opts)
  if opts.has_key?(:less_than)
    cond = @changes[key] < opts[:less_than]
  elsif opts.has_key?(:greater_than)
    cond = @changes[key] > opts[:greater_than]
  elsif opts.has_key?(:less_than_or_equal_to)
    cond = @changes[key] <= opts[:less_than_or_equal_to]
  elsif opts.has_key?(:greater_than_or_equal_to)
    cond = @changes[key] >= opts[:greater_than_or_equal_to]
  elsif opts.has_key?(:equal_to)
    cond = @changes[key] == opts[:equal_to]
  elsif opts.has_key?(:not_equal_to)
    cond = @changes[key] != opts[:not_equal_to]
  end
  unless cond
    generate_error_for(key, :number, opts[:message]) 
  end
  self
end

#validate_required(*required_keys, message: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/changeset/validators.rb', line 15

def validate_required(*required_keys, message: nil) 
  required_keys.each do |key|
    if @changes.has_key?(key)
      if @changes[key].empty? || @changes[key].nil?
        generate_error_for(key, :required, message)
      end
    else
      generate_error_for(key, :required, message)
    end
  end
  self
end

#validate_subset(key, arr, message: nil) ⇒ Object



104
105
106
107
108
109
# File 'lib/changeset/validators.rb', line 104

def validate_subset(key, arr, message: nil)
  unless @changes[key].all?{|k| arr.include?(k)}
    generate_error_for(key, :subset, message)
  end
  self
end