Class: Cmxl::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/cmxl/field.rb

Defined Under Namespace

Classes: LineFormatError, Unknown

Constant Summary collapse

DATE =
/(?<year>\d{0,2})(?<month>\d{2})(?<day>\d{2})/.freeze
@@parsers =

The parser class variable is the registry of all available parser. It is a hash with the tag (MT940 field number/tag) as key and the class as value When parsing a statement line we look for a matching entry or use the Unknown class as default

{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, modifier = nil, tag = nil) ⇒ Field

Returns a new instance of Field.



60
61
62
63
64
65
66
67
68
69
70
71
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *value) ⇒ Object



120
121
122
123
124
125
126
# 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

Class Attribute Details

.parserObject

Returns the value of attribute parser.



26
27
28
# File 'lib/cmxl/field.rb', line 26

def parser
  @parser
end

.tagObject

Returns the value of attribute tag.



37
38
39
# File 'lib/cmxl/field.rb', line 37

def tag
  @tag
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/cmxl/field.rb', line 12

def data
  @data
end

#matchObject

Returns the value of attribute match.



12
13
14
# File 'lib/cmxl/field.rb', line 12

def match
  @match
end

#modifierObject

Returns the value of attribute modifier.



12
13
14
# File 'lib/cmxl/field.rb', line 12

def modifier
  @modifier
end

#sourceObject

Returns the value of attribute source.



12
13
14
# File 'lib/cmxl/field.rb', line 12

def source
  @source
end

#tagObject

Returns the value of attribute tag.



12
13
14
# File 'lib/cmxl/field.rb', line 12

def tag
  @tag
end

Class Method Details

.parse(line) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
# 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

.parsersObject



19
20
21
# File 'lib/cmxl/field.rb', line 19

def self.parsers
  @@parsers
end

Instance Method Details

#add_meta_data(content) ⇒ Object



73
74
75
# File 'lib/cmxl/field.rb', line 73

def (content)
  # Override if the field supports it
end

#to_amount(value) ⇒ Object



116
117
118
# File 'lib/cmxl/field.rb', line 116

def to_amount(value)
  value.tr(',', '.').to_f
end

#to_amount_in_cents(value) ⇒ Object



112
113
114
# 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) ⇒ Object

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.



99
100
101
102
103
104
105
106
107
108
109
110
# 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_hObject



77
78
79
# File 'lib/cmxl/field.rb', line 77

def to_h
  data.merge('tag' => tag)
end

#to_hashObject



81
82
83
# File 'lib/cmxl/field.rb', line 81

def to_hash
  to_h
end

#to_json(*args) ⇒ Object



85
86
87
# File 'lib/cmxl/field.rb', line 85

def to_json(*args)
  to_h.to_json(*args)
end