Class: ACH::Record::Base
- Inherits:
-
Object
- Object
- ACH::Record::Base
- Includes:
- Constants, Validations
- Defined in:
- lib/ach/record/base.rb
Overview
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"
Direct Known Subclasses
Batch::Control, Batch::Header, File::Control, File::Header, Addenda, Dynamic, Entry, Tail
Defined Under Namespace
Classes: EmptyFieldError, UnknownFieldError
Constant Summary
Constants included from Constants
Constants::BATCH_ADDENDA_RECORD_TYPE, Constants::BATCH_CONTROL_RECORD_TYPE, Constants::BATCH_ENTRY_RECORD_TYPE, Constants::BATCH_HEADER_RECORD_TYPE, Constants::BLOCKING_FACTOR, Constants::FILE_CONTROL_RECORD_TYPE, Constants::FILE_HEADER_RECORD_TYPE, Constants::FORMAT_CODE, Constants::RECORD_SIZE, Constants::ROWS_DELIMITER
Class Method Summary collapse
-
.defaults(default_values = nil) ⇒ Hash
Set default values for fields.
-
.fields(*field_names) ⇒ Array<Symbol>
Specify the fields of the record.
-
.from_s(string) ⇒ ACH::Record::Base
Build a new instance of a record from its string representation.
Instance Method Summary collapse
-
#[]=(name, val) ⇒ String
Delegate bracket-assignment to
fields
. -
#fields ⇒ Hash
Return a hash where key is the field’s name and value is the field’s value.
-
#initialize(fields = {}) ⇒ Base
constructor
Initialize object with field values.
-
#to_s! ⇒ Stirng
Build a string from record object.
Methods included from Validations
Constructor Details
#initialize(fields = {}) ⇒ Base
Initialize object with field values. If block is given, it will be evaluated in context of isntance.
104 105 106 107 108 109 110 |
# 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 |
Class Method Details
.defaults(default_values = nil) ⇒ Hash
Set default values for fields. See class description for example.
70 71 72 73 |
# 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) ⇒ Array<Symbol>
Specify the fields of the record. Order is important. All fields must be declared in ACH::Formatter RULES
. See class description for example.
60 61 62 63 64 |
# 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) ⇒ ACH::Record::Base
Build a new instance of a record from its string representation.
95 96 97 98 |
# 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 |
Instance Method Details
#[]=(name, val) ⇒ String
Delegate bracket-assignment to fields
.
143 144 145 |
# File 'lib/ach/record/base.rb', line 143 def []=(name, val) fields[name] = val end |
#fields ⇒ Hash
Return a hash where key is the field’s name and value is the field’s value.
126 127 128 |
# File 'lib/ach/record/base.rb', line 126 def fields @fields ||= {} end |
#to_s! ⇒ Stirng
Build a string from record object.
116 117 118 119 120 121 |
# 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 |