class Cmxl::Field

Constants

DATE

Attributes

parser[RW]
tag[R]
data[RW]
match[RW]
modifier[RW]
source[RW]
tag[RW]

Public Class Methods

new(source, modifier = nil, tag = nil) click to toggle 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
parse(line) click to toggle source

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

# 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
parsers() click to toggle source
# File lib/cmxl/field.rb, line 19
def self.parsers
  @@parsers
end
tag=(tag) click to toggle source

Class accessor for the tag every defines a MT940 tag it can parse This also adds the class to the parser registry.

# File lib/cmxl/field.rb, line 31
def self.tag=(tag)
  @tag = tag.to_s
  @@parsers[tag.to_s] = self
end

Public Instance Methods

add_meta_data(content) click to toggle source
# File lib/cmxl/field.rb, line 73
def add_meta_data(content)
  # Override if the field supports it
end
method_missing(m, *value) click to toggle 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
to_amount(value) click to toggle source
# File lib/cmxl/field.rb, line 116
def to_amount(value)
  value.tr(',', '.').to_f
end
to_amount_in_cents(value) click to toggle 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
to_date(date, year = nil) click to toggle source

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.

# 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
to_h() click to toggle source
# File lib/cmxl/field.rb, line 77
def to_h
  data.merge('tag' => tag)
end
to_hash() click to toggle source
# File lib/cmxl/field.rb, line 81
def to_hash
  to_h
end
to_json(*args) click to toggle source
# File lib/cmxl/field.rb, line 85
def to_json(*args)
  to_h.to_json(*args)
end