module ACH::Formatter

Ensures that every field is formatted with its own rule. Rules are defined in ACH::RULES constant.

Rule Format

Every rule can contain the next items:

For real examples see RULES constants.

Usage Example

ACH::Formatter.format(:customer_name, "LINUS TORVALDS")  # => "LINUS TORVALDS        "
ACH::Formatter.format(:amount, 52)                       # => "0000000052"
ACH::Formatter.customer_acct('1234567890')               # => "1234567890     "

Constants

RULES

Rules for formatting each field. See module documentation for examples.

Public Class Methods

compiled_rules() click to toggle source

Return a hash of all rules that have been built at the moment of method call.

@return [Hash]

# File lib/ach/formatter.rb, line 130
def self.compiled_rules
  @compiled_rules ||= {}
end
define(field, format) click to toggle source

Add a field with corresponding format to RULES.

@param [Symbol] field @param [String] format @return [Hash]

# File lib/ach/formatter.rb, line 96
def self.define(field, format)
  RULES[field] = format
end
defined?(field_name) click to toggle source

Return true if field_name is one of the keys of RULES.

@param [Symbol] field_name @return [Boolean]

# File lib/ach/formatter.rb, line 87
def self.defined?(field_name)
  RULES.key?(field_name)
end
format(field_name, value) click to toggle source

Format the value using the rule defined by field_name format.

@param [Symbol] field_name @param [String] value @return [String]

# File lib/ach/formatter.rb, line 114
def self.format(field_name, value)
  rule_for_field(field_name).call(value)
end
matcher_for(fields) click to toggle source

Return a regular expression that can be used to split string into matched parts, that will correspond to passed fields parameter. Used for ACH reading purposes.

@param [Array<Symbol>] fields @return [Regexp]

# File lib/ach/formatter.rb, line 139
def self.matcher_for(fields)
  /^#{fields.map{ |f| "(.{#{rule_for_field(f).length}})" }.join}$/
end
method_missing(meth, *args) click to toggle source

If missing method name is one of the defined rules, pass its name and the rest of arguments to the format method.

@param [Symbol] meth @param [*Object] args

Calls superclass method
# File lib/ach/formatter.rb, line 105
def self.method_missing(meth, *args)
  self.defined?(meth) ? format(meth, *args) : super
end
rule_for_field(field) click to toggle source

Return ACH::Formatter::Rule rule, built from the corresponding format definition of a field.

@param [Symbol] field @return [ACH::Formatter::Rule]

# File lib/ach/formatter.rb, line 123
def self.rule_for_field(field)
  compiled_rules[field] ||= Rule.new(RULES[field])
end