Module: RubyOpenFormValidators::ValidationsHandler

Extended by:
ValidationsHandler
Included in:
ValidationsHandler
Defined in:
lib/ruby_open_form_validators/validations_handler.rb

Instance Method Summary collapse

Instance Method Details

#create_response(valid, error_message) ⇒ Object



72
73
74
75
76
77
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 72

def create_response(valid, error_message)
  {
    valid: valid,
    message: valid ? nil : error_message
  }
end

#earliest_today(value, _) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 59

def earliest_today(value, _)
  handler do
    today = DateTime.now.beginning_of_day.strftime(Constants::DATE_FORMAT)
    valid = Parser.to_date!(value) >= Parser.to_date!(today)
    create_response(valid, "Date must be after today's date")
  end
end

#email(value, _) ⇒ Object



67
68
69
70
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 67

def email(value, _)
  valid = Constants::EMAIL_REGEX.match?(value)
  create_response(valid, 'Wrong email format')
end

#handlerObject



79
80
81
82
83
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 79

def handler
  yield
rescue StandardError => e
  create_response(false, e.message)
end

#max_date(value, validator) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 51

def max_date(value, validator)
  handler do
    expected_value = Parser.remove_non_digits(validator)
    valid = Parser.to_date!(value) <= Parser.to_date!(expected_value)
    create_response(valid, "Date must be before #{expected_value}")
  end
end

#max_length(value, validator) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 35

def max_length(value, validator)
  handler do
    expected_length = Parser.remove_non_digits(validator)
    valid = value.length <= Parser.to_number!(expected_length)
    create_response(valid, "Length must be shorter than #{expected_length} characters")
  end
end

#max_value(value, validator) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 19

def max_value(value, validator)
  handler do
    expected_value = Parser.remove_non_digits(validator)
    valid = Parser.to_number!(value) <= Parser.to_number!(expected_value)
    create_response(valid, "Value must be less than #{expected_value}")
  end
end

#min_date(value, validator) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 43

def min_date(value, validator)
  handler do
    expected_value = Parser.remove_non_digits(validator)
    valid = Parser.to_date!(value) >= Parser.to_date!(expected_value)
    create_response(valid, "Date must be after #{expected_value}")
  end
end

#min_length(value, validator) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 27

def min_length(value, validator)
  handler do
    expected_length = Parser.remove_non_digits(validator)
    valid = value.to_s.length >= Parser.to_number!(expected_length)
    create_response(valid, "Length must be longer than #{expected_length} characters")
  end
end

#min_value(value, validator) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ruby_open_form_validators/validations_handler.rb', line 11

def min_value(value, validator)
  handler do
    expected_value = Parser.remove_non_digits(validator)
    valid = Parser.to_number!(value) >= Parser.to_number!(expected_value)
    create_response(valid, "Value must be greater than #{expected_value}")
  end
end