Class: ACH::Formatter::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/ach/formatter/rule.rb

Overview

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

Constant Summary collapse

RULE_PARSER_REGEX =

Captures formatting tokens from a rule string.

/^(<-|->)(\d+)(-)?(\|\w+)?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ Rule

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.

Parameters:



17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



9
10
11
# File 'lib/ach/formatter/rule.rb', line 9

def length
  @length
end