class Cmxl::Field
Constants
- DATE
Attributes
Public Class Methods
Source
# File lib/cmxl/field.rb, line 60 def initialize(source, modifier = nil, tag = nil) self.tag = tag self.modifier = modifier self.source = source self.data = {} if self.match = self.source.match(self.class.parser) match.names.each do |name| data[name] = match[name] end end end
Source
# File lib/cmxl/field.rb, line 49 def self.parse(line) if line =~ /\A:(\w{2,2})(\w)?:(.*)\z/m tag = Regexp.last_match(1) modifier = Regexp.last_match(2) content = Regexp.last_match(3).delete("\r").gsub(/\n\z/, '') # remove trailing line break to prevent empty field parsing Field.parsers[tag.to_s].new(content, modifier, tag) else raise LineFormatError, "Wrong line format: #{line.dump}" if Cmxl.config[:raise_line_format_errors] end end
Public: Parses a statement line and initiates a matching Field
class
Returns an instance of the special field class for the matched line.
Raises and LineFormatError if the line is not well formatted
Example:
Cmxl::Field.parse
(‘:60F:C031002PLN40000,00’) #=> returns an AccountBalance instance
Source
# File lib/cmxl/field.rb, line 31 def self.tag=(tag) @tag = tag.to_s @@parsers[tag.to_s] = self end
Class accessor for the tag every defines a MT940 tag it can parse This also adds the class to the parser registry.
Public Instance Methods
Source
# File lib/cmxl/field.rb, line 73 def add_meta_data(content) # Override if the field supports it end
Source
# File lib/cmxl/field.rb, line 120 def method_missing(m, *value) if m =~ /=\z/ data[m] = value.first else data[m.to_s] end end
Source
# File lib/cmxl/field.rb, line 116 def to_amount(value) value.tr(',', '.').to_f end
Source
# File lib/cmxl/field.rb, line 112 def to_amount_in_cents(value) value.gsub(/[,|\.](\d*)/) { Regexp.last_match(1).ljust(2, '0') }.to_i end
Source
# File lib/cmxl/field.rb, line 99 def to_date(date, year = nil) if match = date.to_s.match(DATE) year ||= "20#{match['year'] || Date.today.strftime('%y')}" month = match['month'] day = match['day'] Date.new(year.to_i, month.to_i, day.to_i) else date end rescue ArgumentError # let's simply ignore invalid dates date end
Internal: Converts a provided string into a date object
In MT940 documents the date is provided as a 6 char string (YYMMDD) or as a 4 char string (MMDD) If a 4 char string is provided a second parameter with the year should be provided. If no year is present the current year is assumed.
Example:
to_date
(‘140909’) to_date
(‘0909’, 2014)
Retuns a date object or the provided date value if it is not parseable.