Class: Nacha::Record::Base
- Inherits:
-
Object
- Object
- Nacha::Record::Base
- Includes:
- Validations::FieldValidations
- Defined in:
- lib/nacha/record/base.rb
Overview
Base class for all Nacha records.
Direct Known Subclasses
AckEntryDetail, AdvBatchControl, AdvEntryDetail, AdvFileControl, AdvFileHeader, ArcEntryDetail, BatchControl, BatchHeader, BocEntryDetail, CcdAddenda, CcdEntryDetail, CieAddenda, CieEntryDetail, CtxAddenda, CtxCorporateEntryDetail, DneAddenda, DneEntryDetail, EnrAddenda, EnrEntryDetail, FifthIatAddenda, FileControl, FileHeader, Filler, FirstIatAddenda, FourthIatAddenda, IatBatchHeader, IatEntryDetail, IatForeignCoorespondentBankInformationAddenda, IatRemittanceInformationAddenda, MteAddenda, MteEntryDetail, PopEntryDetail, PosAddenda, PosEntryDetail, PpdAddenda, PpdEntryDetail, RckEntryDetail, SecondIatAddenda, SeventhIatAddenda, ShrAddenda, ShrEntryDetail, SixthIatAddenda, TelEntryDetail, ThirdIatAddenda, TrcEntryDetail, TrxAddenda, TrxEntryDetail, WebAddenda, WebEntryDetail, XckEntryDetail
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#line_number ⇒ Object
:reek:Attribute.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#original_input_line ⇒ Object
Returns the value of attribute original_input_line.
-
#parent ⇒ Object
:reek:Attribute.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Class Method Summary collapse
- .definition ⇒ Object
-
.matcher ⇒ Object
:reek:TooManyStatements rubocop:disable Layout/BlockAlignment, rubocop:disable Style/StringConcatenation:.
-
.nacha_field(name, inclusion:, contents:, position:) ⇒ Object
:reek:LongParameterList, :reek:ManualDispatch.
-
.parse(ach_str) ⇒ Object
rubocop:enable Layout/BlockAlignment, rubocop:enable Style/StringConcatenation:.
- .record_type ⇒ Object
-
.to_h ⇒ Object
:reek:ManualDispatch, :reek:TooManyStatements.
- .to_json(*_args) ⇒ Object
- .unpack_str ⇒ Object
- .validations ⇒ Object
Instance Method Summary collapse
- #add_error(err_string) ⇒ Object
- #create_fields_from_definition ⇒ Object
-
#credit? ⇒ Boolean
Checks if the current transaction code represents a credit transaction.
-
#debit? ⇒ Boolean
Checks if the current transaction code represents a debit transaction.
- #definition ⇒ Object
- #errors ⇒ Object
- #human_name ⇒ Object
-
#initialize(opts = {}) ⇒ Base
constructor
:reek:ManualDispatch.
- #inspect ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
:reek:TooManyStatements.
- #record_type ⇒ Object
- #respond_to_missing?(method_name) ⇒ Boolean
- #to_ach ⇒ Object
- #to_h ⇒ Object
- #to_html(_opts = {}) ⇒ Object
- #to_json(*_args) ⇒ Object
-
#valid? ⇒ Boolean
look for invalid fields, if none, then return true.
- #validate ⇒ Object
Methods included from Validations::FieldValidations
Constructor Details
#initialize(opts = {}) ⇒ Base
:reek:ManualDispatch
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nacha/record/base.rb', line 19 def initialize(opts = {}) @children = [] @parent = nil @errors = [] @original_input_line = nil @line_number = nil @dirty = false @fields = {} create_fields_from_definition opts.each do |key, value| setter = "#{key}=" # rubocop:disable GitlabSecurity/PublicSend public_send(setter, value) if value && respond_to?(setter) # rubocop:enable GitlabSecurity/PublicSend end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
:reek:TooManyStatements
303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/nacha/record/base.rb', line 303 def method_missing(method_name, *args, &block) method = method_name.to_s field_name = method.gsub(/=$/, '').to_sym field = @fields[field_name] return super unless field if method.end_with?('=') assign_field_data(field, args) else field end end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
14 15 16 |
# File 'lib/nacha/record/base.rb', line 14 def children @children end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
14 15 16 |
# File 'lib/nacha/record/base.rb', line 14 def fields @fields end |
#line_number ⇒ Object
:reek:Attribute
16 17 18 |
# File 'lib/nacha/record/base.rb', line 16 def line_number @line_number end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/nacha/record/base.rb', line 14 def name @name end |
#original_input_line ⇒ Object
Returns the value of attribute original_input_line.
14 15 16 |
# File 'lib/nacha/record/base.rb', line 14 def original_input_line @original_input_line end |
#parent ⇒ Object
:reek:Attribute
16 17 18 |
# File 'lib/nacha/record/base.rb', line 16 def parent @parent end |
#validations ⇒ Object (readonly)
Returns the value of attribute validations.
14 15 16 |
# File 'lib/nacha/record/base.rb', line 14 def validations @validations end |
Class Method Details
.definition ⇒ Object
50 51 52 |
# File 'lib/nacha/record/base.rb', line 50 def definition @definition ||= {} end |
.matcher ⇒ Object
:reek:TooManyStatements rubocop:disable Layout/BlockAlignment, rubocop:disable Style/StringConcatenation:
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/nacha/record/base.rb', line 67 def matcher @matcher ||= begin output_started = false skipped_output = false Regexp.new('\A' + definition.values.reverse.collect do |field| contents = field[:contents] position = field[:position] size = position.size if contents =~ /\AC(.+)\z/ last_match = Regexp.last_match(1) if last_match.match?(/ */) && !output_started skipped_output = true '' else output_started = true last_match end elsif contents.match?(/\ANumeric\z/) || contents.match?(/\AYYMMDD\z/) output_started = true '[0-9 ]' + "{#{size}}" elsif output_started '.' + "{#{size}}" else skipped_output = true '' end end.reverse.join + (skipped_output ? '.*' : '') + '\z') end end |
.nacha_field(name, inclusion:, contents:, position:) ⇒ Object
:reek:LongParameterList, :reek:ManualDispatch
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/nacha/record/base.rb', line 38 def nacha_field(name, inclusion:, contents:, position:) Nacha.add_ach_record_type(self) definition[name] = { inclusion: inclusion, contents: contents, position: position, name: name } validation_method = :"valid_#{name}" return unless respond_to?(validation_method) (validations[name] ||= []) << validation_method end |
.parse(ach_str) ⇒ Object
rubocop:enable Layout/BlockAlignment, rubocop:enable Style/StringConcatenation:
99 100 101 102 103 104 105 106 |
# File 'lib/nacha/record/base.rb', line 99 def parse(ach_str) rec = new(original_input_line: ach_str) ach_str.unpack(unpack_str).zip(rec.fields.values) do |input_data, field| field.data = input_data end rec.validate rec end |
.record_type ⇒ Object
108 109 110 |
# File 'lib/nacha/record/base.rb', line 108 def record_type Nacha.record_name(self) end |
.to_h ⇒ Object
:reek:ManualDispatch, :reek:TooManyStatements
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/nacha/record/base.rb', line 113 def to_h fields = definition.transform_values do |field_def| { inclusion: field_def[:inclusion], contents: field_def[:contents], position: field_def[:position].to_s } end fields[:child_record_types] = child_record_types.to_a fields[:child_record_types] ||= [] fields[:klass] = name.to_s { record_type.to_sym => fields } end |
.to_json(*_args) ⇒ Object
129 130 131 |
# File 'lib/nacha/record/base.rb', line 129 def to_json(*_args) JSON.pretty_generate(to_h) end |
.unpack_str ⇒ Object
58 59 60 61 62 |
# File 'lib/nacha/record/base.rb', line 58 def unpack_str @unpack_str ||= definition.values.collect do |field_def| Nacha::Field.unpack_str(field_def) end.join.freeze end |
.validations ⇒ Object
54 55 56 |
# File 'lib/nacha/record/base.rb', line 54 def validations @validations ||= {} end |
Instance Method Details
#add_error(err_string) ⇒ Object
298 299 300 |
# File 'lib/nacha/record/base.rb', line 298 def add_error(err_string) @errors << err_string end |
#create_fields_from_definition ⇒ Object
139 140 141 142 143 |
# File 'lib/nacha/record/base.rb', line 139 def create_fields_from_definition definition.each_pair do |field_name, field_def| @fields[field_name.to_sym] = Nacha::Field.new(field_def) end end |
#credit? ⇒ Boolean
This method includes robust error handling. If a ‘NoMethodError` occurs (e.g., if `transaction_code` is undefinable or does not respond to `to_s` in an unexpected way) or a `NameError` occurs (e.g., if `transaction_code` or `CREDIT_TRANSACTION_CODES` is not defined in the current scope), the method gracefully rescues these exceptions and returns `false`. This default behavior ensures that an inability to determine the transaction type results in it being considered “not a credit”.
Checks if the current transaction code represents a credit transaction.
This method evaluates the ‘transaction_code` (which is expected to be an attribute or method available in the current context) against a predefined set of credit transaction codes.
287 288 289 290 291 292 |
# File 'lib/nacha/record/base.rb', line 287 def credit? transaction_code && CREDIT_TRANSACTION_CODES.include?(transaction_code.to_s) rescue NoMethodError, NameError false end |
#debit? ⇒ Boolean
This method includes robust error handling. If a ‘NoMethodError` occurs (e.g., if `transaction_code` is undefinable or does not respond to `to_s` in an unexpected way) or a `NameError` occurs (e.g., if `transaction_code` or `DEBIT_TRANSACTION_CODES` is not defined in the current scope), the method gracefully rescues these exceptions and returns `false`. This default behavior ensures that an inability to determine the transaction type results in it being considered “not a debit”.
Checks if the current transaction code represents a debit transaction.
This method evaluates the ‘transaction_code` (which is expected to be an attribute or method available in the current context) against a predefined set of debit transaction codes.
247 248 249 250 251 252 |
# File 'lib/nacha/record/base.rb', line 247 def debit? transaction_code && DEBIT_TRANSACTION_CODES.include?(transaction_code.to_s) rescue NoMethodError, NameError false end |
#definition ⇒ Object
195 196 197 |
# File 'lib/nacha/record/base.rb', line 195 def definition self.class.definition end |
#errors ⇒ Object
294 295 296 |
# File 'lib/nacha/record/base.rb', line 294 def errors (@errors + @fields.values.map(&:errors)).flatten end |
#human_name ⇒ Object
149 150 151 |
# File 'lib/nacha/record/base.rb', line 149 def human_name @human_name ||= record_type.to_s.split('_').map(&:capitalize).join(' ') end |
#inspect ⇒ Object
191 192 193 |
# File 'lib/nacha/record/base.rb', line 191 def inspect "#<#{self.class.name}> #{to_h}" end |
#record_type ⇒ Object
145 146 147 |
# File 'lib/nacha/record/base.rb', line 145 def record_type self.class.record_type end |
#respond_to_missing?(method_name) ⇒ Boolean
316 317 318 319 |
# File 'lib/nacha/record/base.rb', line 316 def respond_to_missing?(method_name, *) field_name = method_name.to_s.gsub(/=$/, '').to_sym definition[field_name] || super end |
#to_ach ⇒ Object
170 171 172 173 174 |
# File 'lib/nacha/record/base.rb', line 170 def to_ach @fields.keys.collect do |key| @fields[key].to_ach end.join end |
#to_h ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/nacha/record/base.rb', line 153 def to_h { nacha_record_type: record_type, metadata: { klass: self.class.name, errors: errors, line_number: @line_number, original_input_line: original_input_line } }.merge( @fields.keys.to_h do |key| [key, @fields[key].to_json_output] end) end |
#to_html(_opts = {}) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/nacha/record/base.rb', line 176 def to_html(_opts = {}) record_error_class = nil field_html = @fields.values.collect do |field| record_error_class ||= 'error' if field.errors.any? field.to_html end.join "<div class=\"nacha-record tooltip #{record_type} #{record_error_class}\">" \ "<span class=\"nacha-field\" data-name=\"record-number\">#{format('%05d', line_number)} | </span>" \ "#{field_html}" \ "<span class=\"record-type\" data-name=\"record-type\">#{human_name}</span>" \ "</div>" end |
#to_json(*_args) ⇒ Object
166 167 168 |
# File 'lib/nacha/record/base.rb', line 166 def to_json(*_args) JSON.pretty_generate(to_h) end |
#valid? ⇒ Boolean
look for invalid fields, if none, then return true
209 210 211 212 |
# File 'lib/nacha/record/base.rb', line 209 def valid? validate errors.empty? end |
#validate ⇒ Object
199 200 201 202 203 204 205 206 |
# File 'lib/nacha/record/base.rb', line 199 def validate # Run field-level validations first @fields.each_value(&:validate) # Then run record-level validations that might depend on multiple fields self.class.definition.each_key do |field| run_record_level_validations_for(field) end end |