Class: Rulix::ActiveRecordValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rulix/active_record_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ActiveRecordValidator

Returns a new instance of ActiveRecordValidator.



5
6
7
8
9
# File 'lib/rulix/active_record_validator.rb', line 5

def initialize *args
  ruleset = args.first[:ruleset]

  self.ruleset = ruleset
end

Instance Attribute Details

#rulesetObject

Returns the value of attribute ruleset.



3
4
5
# File 'lib/rulix/active_record_validator.rb', line 3

def ruleset
  @ruleset
end

Instance Method Details

#reduce_errors_into(record, error_hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rulix/active_record_validator.rb', line 23

def reduce_errors_into record, error_hash
  error_hash.reduce(record) do |subject, error|
    key, val = error

    if val.is_a? Hash
      reduce_errors_into record.send(key), val
    else
      val.flatten.map do |err|
        subject.errors.add key, err
      end
    end

    subject
  end
end

#validate(record) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rulix/active_record_validator.rb', line 11

def validate record
  result = Rulix::Validator.run record, ruleset

  if result.valid?
    true
  else
    reduce_errors_into record, result

    false
  end
end