Module: ComposableValidations::Utils

Included in:
ComposableValidations
Defined in:
lib/composable_validations/utils.rb

Instance Method Summary collapse

Instance Method Details

#default_errors(validator) ⇒ Object



2
3
4
5
6
7
# File 'lib/composable_validations/utils.rb', line 2

def default_errors(validator)
  lambda do |object, errors_hash|
    errors = ComposableValidations::Errors.new(errors_hash)
    validator.call(object, errors, nil)
  end
end

#error(errors, msg, object, *segments) ⇒ Object



25
26
27
28
# File 'lib/composable_validations/utils.rb', line 25

def error(errors, msg, object, *segments)
  errors.add(msg, join(*segments), object)
  false
end

#join(*segments) ⇒ Object



9
10
11
12
13
# File 'lib/composable_validations/utils.rb', line 9

def join(*segments)
  segments.inject([]) do |acc, seg|
    acc + Array(seg)
  end
end

#validate(msg, key = nil, &blk) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/composable_validations/utils.rb', line 15

def validate(msg, key = nil, &blk)
  lambda do |o, errors, prefix|
    if yield(o)
      true
    else
      error(errors, msg, o, prefix, key)
    end
  end
end