Module: Headache::Formatters

Includes:
Fixy::Formatter::Alphanumeric
Included in:
Record::Overflow
Defined in:
lib/headache/formatters.rb

Constant Summary collapse

BLANK_DATE =
"      "

Instance Method Summary collapse

Instance Method Details

#format_alphanumeric(input, length) ⇒ Object



6
7
8
# File 'lib/headache/formatters.rb', line 6

def format_alphanumeric(input, length)
  super(input, length).upcase
end

#format_date(input, length) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/headache/formatters.rb', line 14

def format_date(input, length)
  if input.respond_to?(:strftime)
    return input.strftime '%y%m%d'
  elsif input.blank?
    return BLANK_DATE
  else
    format_date Date.strptime(input.to_s, '%y%m%d'), length
  end
end

#format_nothing(_input, length) ⇒ Object



24
25
26
# File 'lib/headache/formatters.rb', line 24

def format_nothing(_input, length)
  ' ' * length
end

#format_numeric(input, length) ⇒ Object



28
29
30
31
32
33
# File 'lib/headache/formatters.rb', line 28

def format_numeric(input, length)
  input = input.to_i.abs.to_s
  fail ArgumentError, "Invalid Input: #{input.inspect} (only digits are accepted) from #{self.class.to_s.demodulize}" unless input =~ /^\d+$/
  fail ArgumentError, "Not enough length (input: #{input}, length: #{length})" if input.length > length
  input.rjust(length, '0')
end

#format_numeric_value(input, length) ⇒ Object



10
11
12
# File 'lib/headache/formatters.rb', line 10

def format_numeric_value(input, length)
  format_numeric(input, length)
end

#format_time(input, _length) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/headache/formatters.rb', line 35

def format_time(input, _length)
  if input.is_a?(String) || input.is_a?(Fixnum)
    chars   = input.to_s.chars
    minutes = chars.pop(2).join.to_i
    hours   = chars.first(2).join.to_i
    input   = Time.new 0, 1, 1, hours, minutes
  end
  fail "input #{input.inspect} does not respond to #strftime!" unless input.respond_to?(:strftime)
  input.strftime '%H%M'
end