Class: SupplierPayments::PaymentFile::AbstractRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/supplier_payments/payment_file/records.rb

Constant Summary collapse

LayoutError =
Class.new(Exception)
FormatError =
Class.new(Exception)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ AbstractRecord

Returns a new instance of AbstractRecord.



52
53
54
55
56
# File 'lib/supplier_payments/payment_file/records.rb', line 52

def initialize(attrs = {})
  attrs.each do |key, value|
    self.send("#{key}=", value)
  end
end

Class Attribute Details

.layoutObject

Returns the value of attribute layout.



9
10
11
# File 'lib/supplier_payments/payment_file/records.rb', line 9

def layout
  @layout
end

.transaction_codeObject

Returns the value of attribute transaction_code.



8
9
10
# File 'lib/supplier_payments/payment_file/records.rb', line 8

def transaction_code
  @transaction_code
end

Class Method Details

.parse(line) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/supplier_payments/payment_file/records.rb', line 37

def self.parse(line)
  io = StringIO.new(line)
  record = new

  layout.each do |field, length, format, *opts|
    if field[-1] == "!"
      io.seek(length, IO::SEEK_CUR)
    else
      record.send("#{field}=", io.read(length))
    end
  end

  record
end

Instance Method Details

#inspectObject



64
65
66
67
68
69
70
71
# File 'lib/supplier_payments/payment_file/records.rb', line 64

def inspect
  str = self.class.layout.map { |field, length, format, *opts|
    next if field[-1] == "!"

    ":#{field} => #{send(field).inspect}"
  }.compact.join(", ")
  "<#{self.class.name}(#{transaction_code}) #{str}>"
end

#to_sObject



58
59
60
61
62
# File 'lib/supplier_payments/payment_file/records.rb', line 58

def to_s
  self.class.layout.map { |field, length, format, *opts|
    format_field(field, length, format, *opts)
  }.join
end

#transaction_codeObject



73
74
75
# File 'lib/supplier_payments/payment_file/records.rb', line 73

def transaction_code
  self.class.transaction_code
end