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:
-
justification - “<-” or “->”.
-
width - a number of characters for a field.
-
padding - specifying “-” will pad right-justified values with spaces instead of zeros.
-
transformation - allows to call method on a string. Pipe must precedes a method name. Foe example: “|upcase”.
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
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
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
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 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
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
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
# File lib/ach/formatter.rb, line 105 def self.method_missing(meth, *args) self.defined?(meth) ? format(meth, *args) : super end
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