Module: FixedWidthFileValidator::StringHelper

Included in:
String
Defined in:
lib/fixed_width_file_validator/string_helper.rb

Instance Method Summary collapse

Instance Method Details

#anyObject



6
7
8
# File 'lib/fixed_width_file_validator/string_helper.rb', line 6

def any
  true
end

#blankObject



10
11
12
# File 'lib/fixed_width_file_validator/string_helper.rb', line 10

def blank
  strip.empty?
end

#date(format = '%Y%m%d') ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fixed_width_file_validator/string_helper.rb', line 28

def date(format = '%Y%m%d')
  # since exception is slow, optimize for known format here
  if format == '%Y%m%d'
    return false unless length == 8
    y = slice(0..3).to_i
    m = slice(4..5).to_i
    d = slice(6..7).to_i
    Date.valid_date?(y, m, d)
  else
    Date.strptime(self, format)
  end
rescue ArgumentError
  false
end

#date_or_blank(format = '%Y%m%d') ⇒ Object



55
56
57
# File 'lib/fixed_width_file_validator/string_helper.rb', line 55

def date_or_blank(format = '%Y%m%d')
  blank || date(format)
end

#date_time(format = '%Y%m%d%H%M%S') ⇒ Object



22
23
24
25
26
# File 'lib/fixed_width_file_validator/string_helper.rb', line 22

def date_time(format = '%Y%m%d%H%M%S')
  Time.strptime(self, format)
rescue ArgumentError
  false
end

#not_blankObject



14
15
16
# File 'lib/fixed_width_file_validator/string_helper.rb', line 14

def not_blank
  !blank
end

#numeric(max = 32, precision = 0, min = 1) ⇒ Object



63
64
65
66
# File 'lib/fixed_width_file_validator/string_helper.rb', line 63

def numeric(max = 32, precision = 0, min = 1)
  m = /^(\d*)\.?(\d*)$/.match(self)
  m && m[1] && (min..max).cover?(m[1].size) && m[2].size == precision
end

#numeric_or_blank(max = 32, precision = 0, min = 1) ⇒ Object



68
69
70
# File 'lib/fixed_width_file_validator/string_helper.rb', line 68

def numeric_or_blank(max = 32, precision = 0, min = 1)
  blank || numeric(max, precision, min)
end

#positiveObject



59
60
61
# File 'lib/fixed_width_file_validator/string_helper.rb', line 59

def positive
  to_i.positive?
end

#timeObject



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

def time
  return false unless length == 6
  h = self[0..1].to_i
  m = self[2..3].to_i
  s = self[4..5].to_i
  h >= 0 && h < 24 && m >= 0 && m < 60 && s >= 0 && s < 60
end

#time_or_blankObject



51
52
53
# File 'lib/fixed_width_file_validator/string_helper.rb', line 51

def time_or_blank
  blank || time
end

#width(num) ⇒ Object



18
19
20
# File 'lib/fixed_width_file_validator/string_helper.rb', line 18

def width(num)
  size == num
end