Class: Vldt::Join

Inherits:
Validation show all
Defined in:
lib/vldt/join.rb

Overview

Join multiple validations together and merge all of their errors.

Instance Method Summary collapse

Constructor Details

#initialize(*validations) ⇒ Join

Returns a new instance of Join.



4
5
6
# File 'lib/vldt/join.rb', line 4

def initialize (*validations)
  @validations = validations
end

Instance Method Details

#call(object) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vldt/join.rb', line 8

def call (object)
  errors = @validations.map { |v| v.call(object) }

  if errors.any?
    errors.compact.inject(Hash.new([])) do |es, e|
      e.each do |k, v|
        es[k] += v
      end

      es
    end
  end
end