Module: RubyOpenFormValidators

Defined in:
lib/ruby_open_form_validators.rb,
lib/ruby_open_form_validators/parser.rb,
lib/ruby_open_form_validators/version.rb,
lib/ruby_open_form_validators/constants.rb,
lib/ruby_open_form_validators/validations_handler.rb

Defined Under Namespace

Modules: Constants, Parser, ValidationsHandler

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.validate(value, validators) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby_open_form_validators.rb', line 9

def self.validate(value, validators)
  empty_accum = { text: value, valid: true, messages: [] }
  validators.split(',').each_with_object(empty_accum) do |validator, accum|
    validator_key = Parser.remove_digits(validator).underscore
    result = if ValidationsHandler.method_defined?(validator_key)
               ValidationsHandler.public_send(validator_key, value, validator)
             else
               { valid: true, message: nil }
             end

    accum[:valid] &&= result[:valid]
    message = result[:message]
    accum[:messages] << message unless message.nil?
  end
end