Class: LSVplus::File

Inherits:
BaseContainer show all
Defined in:
lib/lsv_plus/file.rb

Defined Under Namespace

Classes: InvalidCreatorIdentification, InvalidLSVIdentification

Constant Summary collapse

ATTRIBUTES =
%i(processing_type creation_date creator_identification currency lsv_identification)
FIVE_CHARS_UPPERCASE =
/\A[A-Z0-9]{5}\z/

Instance Method Summary collapse

Methods inherited from BaseContainer

#initialize, #set_attributes, #validate_presence_of_required_attributes

Constructor Details

This class inherits a constructor from LSVplus::BaseContainer

Instance Method Details

#add_record(record) ⇒ Object



20
21
22
# File 'lib/lsv_plus/file.rb', line 20

def add_record(record)
  records << record
end

#recordsObject



24
25
26
# File 'lib/lsv_plus/file.rb', line 24

def records
  @records ||= []
end

#records_as_stringObject



32
33
34
35
36
37
38
39
40
# File 'lib/lsv_plus/file.rb', line 32

def records_as_string
  output = StringIO.new
  records.each_with_index do |record, index|
    index += 1
    output.write RecordFormatter.call(self, record, index)
  end
  output.rewind
  output.read
end

#to_sObject



28
29
30
# File 'lib/lsv_plus/file.rb', line 28

def to_s
  records_as_string + total_record
end

#totalObject



46
47
48
# File 'lib/lsv_plus/file.rb', line 46

def total
  records.inject(BigDecimal.new(0)) { |sum, record| sum + record.amount }
end

#total_recordObject



42
43
44
# File 'lib/lsv_plus/file.rb', line 42

def total_record
  TotalRecordFormatter.call(self)
end

#validate(attributes) ⇒ Object



50
51
52
53
54
# File 'lib/lsv_plus/file.rb', line 50

def validate(attributes)
  super(attributes)
  validate_creator_identification(attributes)
  validate_lsv_identification(attributes)
end

#validate_creator_identification(attributes) ⇒ Object



56
57
58
59
60
# File 'lib/lsv_plus/file.rb', line 56

def validate_creator_identification(attributes)
  unless attributes[:creator_identification] =~ FIVE_CHARS_UPPERCASE
    raise InvalidCreatorIdentification, "Does not match #{FIVE_CHARS_UPPERCASE}"
  end
end

#validate_lsv_identification(attributes) ⇒ Object



62
63
64
65
66
# File 'lib/lsv_plus/file.rb', line 62

def validate_lsv_identification(attributes)
  unless attributes[:lsv_identification] =~ FIVE_CHARS_UPPERCASE
    raise InvalidLSVIdentification, "Does not match #{FIVE_CHARS_UPPERCASE}"
  end
end