Module: TxtData::FormatHelpers

Included in:
TxtDataRowDSL::Field
Defined in:
lib/txt_data/format_helpers.rb

Overview

A helpful module for formatting fields for fixed length txt data files.

Instance Method Summary collapse

Instance Method Details

#filter_illegal_characters(value) ⇒ Object

Filters invalid characters specified in CPF eSubmission spec file.

Parameters:

  • value (String)


34
35
36
# File 'lib/txt_data/format_helpers.rb', line 34

def filter_illegal_characters(value)
  value.tr("-_+$<>:;?!=[]`^|\\'\"`~", "")
end

#money_format(value, length:, just: :right, pad: '0') ⇒ Object

Right justified, “0” padded, 2 decimal places, with no separator.

Parameters:

  • value (Fixnum)
  • length (Fixnum)


7
8
9
10
# File 'lib/txt_data/format_helpers.rb', line 7

def money_format(value, length:, just: :right, pad: '0')
  txt = ("%.2f" % value.round(2)).to_s.sub(".", "").truncate(length, omission: "")
  justify(txt, length, just, pad)
end

#number_format(value, length:, just: :right, pad: '0') ⇒ Object

Right justified, “0” padded, no decimal places, with no separator.

Parameters:

  • value (Fixnum)
  • length (Fixnum)
  • just (Symbol) (defaults to: :right)
  • pad (String) (defaults to: '0')


17
18
19
20
# File 'lib/txt_data/format_helpers.rb', line 17

def number_format(value, length:, just: :right, pad: '0')
  txt = value.to_i.to_s.truncate(length, omission: "")
  justify(txt, length, just, pad)
end

#text_format(value, length:, just: :left, sanitize:, pad: ' ') ⇒ Object

Left justified, “ ” padded, text.

Parameters:

  • value (String)
  • length (Fixnum)
  • just (Symbol) (defaults to: :left)
  • pad (String) (defaults to: ' ')


27
28
29
30
# File 'lib/txt_data/format_helpers.rb', line 27

def text_format(value, length:, just: :left, sanitize:, pad: ' ')
  txt = sanitize ? filter_illegal_characters(value.to_s).truncate(length, omission: "") : value
  justify(txt, length, just, pad)
end