Class: ACH::Formatter::Rule
- Inherits:
-
Object
- Object
- ACH::Formatter::Rule
- 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
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
-
#initialize(rule) ⇒ Rule
constructor
Initializes instance with formatting data.
Constructor Details
#initialize(rule) ⇒ Rule
Initializes instance with formatting data. Parses passed string for formatting values, such as width, justification, etc. As the result, builds a Proc object that will be used to format passed string according to formatting rule.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ach/formatter/rule.rb', line 14 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
#length ⇒ Object (readonly)
Returns the value of attribute length.
9 10 11 |
# File 'lib/ach/formatter/rule.rb', line 9 def length @length end |