Class: Proposal::ArgumentsValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/proposal/arguments_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/validators/proposal/arguments_validator.rb', line 10

def validate record
  if record.expects.is_a? Proc
    unless record.expects.call(record.arguments)
      record.errors.add :arguments, "is invalid"
    end
  elsif record.arguments.is_a? Hash
    case record.expects
    when Symbol
      validate_expected record, record.expects
    when Array
      record.expects.each { |sym| validate_expected record, sym }
    end
  else
    record.errors.add :arguments, "must be a hash"
    case record.expects
    when Symbol
      record.errors.add :arguments, "is missing #{record.expects}"
    when Array
      record.expects.each do |sym|
        record.errors.add :arguments, "is missing #{sym}"
      end
    end
  end
end

#validate_expected(record, sym) ⇒ Object



4
5
6
7
8
# File 'app/validators/proposal/arguments_validator.rb', line 4

def validate_expected record, sym
  unless record.arguments[sym].present?
    record.errors.add :arguments, "is missing #{sym}"
  end
end