Sha256: c6caf42b6dee93aa5f29346f3b9aa9dafb0595033193432a460fb8d5e3e2beb7

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module ACH
  # Parses string representation of rule and builds a +Proc+ based on it
  class Formatter::Rule
    # Captures formatting tokens from a rule string.
    RULE_PARSER_REGEX = /^(<-|->)(\d+)(-)?(\|\w+)?$/

    delegate :call, :[], :to => :@lambda

    attr_reader :length

    # 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
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ach_builder-0.2.2 lib/ach/formatter/rule.rb