Module: RubyOpenFormValidators::Parser

Defined in:
lib/ruby_open_form_validators/parser.rb

Class Method Summary collapse

Class Method Details

.numeric?(string_number) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/ruby_open_form_validators/parser.rb', line 28

def numeric?(string_number)
  !Float(string_number).nil?
rescue StandardError
  false
end

.remove_digits(string) ⇒ Object



13
14
15
# File 'lib/ruby_open_form_validators/parser.rb', line 13

def remove_digits(string)
  string.gsub(/\d+(.\d+(e[+-]\d+)?)?/, '')
end

.remove_non_digits(string) ⇒ Object



9
10
11
# File 'lib/ruby_open_form_validators/parser.rb', line 9

def remove_non_digits(string)
  string.gsub(/^[a-zA-Z]+/, '')
end

.to_date!(attribute) ⇒ Object



17
18
19
# File 'lib/ruby_open_form_validators/parser.rb', line 17

def to_date!(attribute)
  Date.parse(attribute, Constants::DATE_FORMAT)
end

.to_number!(attribute) ⇒ Object



21
22
23
24
25
26
# File 'lib/ruby_open_form_validators/parser.rb', line 21

def to_number!(attribute)
  return attribute if attribute.is_a?(Numeric)
  return attribute.to_f if attribute.is_a?(String) && numeric?(attribute)

  raise 'invalid numeric value'
end