class ACH::Record::Base

Base class for all record entities (e.g. ACH::File::Header, ACH::File::Control, ACH::Record::Entry, others). Any record being declared should specify its fields, and optional default values. Except for ACH::Record::Dynamic, any declared field within a record should have corresponding rule defined under ACH::Rule::Formatter.

Example

class Addenda < Record
  fields :record_type,
         :addenda_type_code,
         :payment_related_info,
         :addenda_sequence_num,
         :entry_details_sequence_num

  defaults :record_type => 7
end

addenda = ACH::Addenda.new(
  :addenda_type_code => '05',
  :payment_related_info => 'PAYMENT_RELATED_INFO',
  :addenda_sequence_num => 1,
  :entry_details_sequence_num => 1 )
addenda.to_s! # => "705PAYMENT_RELATED_INFO                                                            00010000001"

Public Class Methods

defaults(default_values = nil) click to toggle source

Set default values for fields. See class description for example.

@param [Hash, nil] default_values @return [Hash]

# File lib/ach/record/base.rb, line 70
def self.defaults(default_values = nil)
  return @defaults if default_values.nil?
  @defaults = default_values.freeze
end
fields(*field_names) click to toggle source

Specify the fields of the record. Order is important. All fields must be declared in ACH::Formatter RULES. See class description for example.

@param [*Symbol] field_names @return [Array<Symbol>]

# File lib/ach/record/base.rb, line 60
def self.fields(*field_names)
  return @fields if field_names.empty?
  @fields = field_names
  @fields.each{ |field| define_field_methods(field) }
end
from_s(string) click to toggle source

Build a new instance of a record from its string representation.

@param [String] string @return [ACH::Record::Base]

# File lib/ach/record/base.rb, line 95
def self.from_s(string)
  field_matcher_regexp = Formatter.matcher_for(fields)
  new Hash[*fields.zip(string.match(field_matcher_regexp)[1..-1]).flatten]
end
new(fields = {}) click to toggle source

Initialize object with field values. If block is given, it will be evaluated in context of isntance.

@param [Hash] fields

# File lib/ach/record/base.rb, line 104
def initialize(fields = {})
  defaults.each do |key, value|
    self.fields[key] = Proc === value ? value.call : value
  end
  self.fields.merge!(fields)
  instance_eval(&Proc.new) if block_given?
end

Private Class Methods

define_field_methods(field) click to toggle source

Define accessor methods for a given field name if it can be found in the keys of {ACH::Formatter::RULES}.

@param [Symbol] field @raise [UnknownFieldError]

# File lib/ach/record/base.rb, line 80
def self.define_field_methods(field)
  raise UnknownFieldError.new(field, name) unless Formatter::RULES.key?(field)
  define_method(field) do |*args|
    args.empty? ? @fields[field] : (@fields[field] = args.first)
  end
  define_method("#{field}=") do |val|
    @fields[field] = val
  end
end

Public Instance Methods

fields() click to toggle source

Return a hash where key is the field’s name and value is the field’s value.

@return [Hash]

# File lib/ach/record/base.rb, line 126
def fields
  @fields ||= {}
end
to_s!() click to toggle source

Build a string from record object.

@return [Stirng] @raise [EmptyFieldError]

# File lib/ach/record/base.rb, line 116
def to_s!
  self.class.fields.map do |name|
    raise EmptyFieldError.new(name, self) if @fields[name].nil?
    Formatter.format name, @fields[name]
  end.join
end

Private Instance Methods

[]=(name, val) click to toggle source

Delegate bracket-assignment to fields.

@param [Symbol] name @param [String] val @return [String]

# File lib/ach/record/base.rb, line 143
def []=(name, val)
  fields[name] = val
end
defaults() click to toggle source

Return field default values defined in class.

@return [Hash]

# File lib/ach/record/base.rb, line 133
def defaults
  self.class.defaults
end