class ACH::Formatter::Rule

Parses string representation of rule and builds a Proc based on it

Constants

RULE_PARSER_REGEX

Captures formatting tokens from a rule string.

Attributes

length[R]

Public Class Methods

new(rule) click to toggle source

Initialize the instance with the formatting data. Parses the given string for formatting values, such as width, justification, etc. With the result, it builds a Proc object to be used to format the given string according to the formatting rule.

@param [ACH::Formatter::Rule] rule

# File lib/ach/formatter/rule.rb, line 17
def initialize(rule)
  just, width, pad, transf = rule.match(RULE_PARSER_REGEX)[1..-1]
  @length    = width.to_i
  @padmethod = just == '<-' ? :ljust : :rjust
  @padstr    = @padmethod == :ljust ? ' ' : pad == '-' ? ' ' : '0'
  @transform = transf[1..-1] if transf

  @lambda = Proc.new do |val|
    val = val.to_s
    (@transform ? val.send(@transform) : val).send(@padmethod, @length, @padstr)[-@length..-1]
  end
end