Class: Datanorm::Lines::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/datanorm/lines/base.rb

Overview

Object that represents one line of a Datanorm file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns:, source_line_number:) ⇒ Base

Returns a new instance of Base.



29
30
31
32
# File 'lib/datanorm/lines/base.rb', line 29

def initialize(columns:, source_line_number:)
  @columns = columns
  @source_line_number = source_line_number
end

Instance Attribute Details

#columnsObject (readonly)

Array that holds all attributes of one line. In the Datanorm file they are typically separated by seimcolons. Header rows may lack semicolons (in V4), in that case, this Array has only one long String.



10
11
12
# File 'lib/datanorm/lines/base.rb', line 10

def columns
  @columns
end

#source_line_numberObject (readonly)

Where in the originating Datanorm file this line is located.



13
14
15
# File 'lib/datanorm/lines/base.rb', line 13

def source_line_number
  @source_line_number
end

Class Method Details

.inherited(subclass) ⇒ Object

This class is subclassed by one type per row. Add convenient predicate methods to query the kind of record class. E.g. ‘Datanorm::Lines::V4::Extra` has `kind_extra?` to be true.



18
19
20
21
22
23
24
25
26
27
# File 'lib/datanorm/lines/base.rb', line 18

def self.inherited(subclass)
  kind_method = "kind_#{subclass.name.split('::').last.downcase}?"

  remove_method(kind_method) if method_defined?(kind_method) # Avoid warnings during tests
  define_method(kind_method) do
    self.class.name.split('::').last.downcase == subclass.name.split('::').last.downcase
  end

  super
end

Instance Method Details

#as_jsonObject



42
43
44
# File 'lib/datanorm/lines/base.rb', line 42

def as_json
  raise "Implement ##{__method__} in #{self.class}"
end

#encodeObject

Convenience Shortcut to convert attributes from CP850 to UTF-8.



58
59
60
# File 'lib/datanorm/lines/base.rb', line 58

def encode(...)
  ::Datanorm::Helpers::Utf8.call(...)
end

#idObject

Every row has a unique identifier. Most often a product number. Text records commonly have their own IDs, which are not equal to the product number. Multiple lines can have the same ID (e.g. one for price and several for description). Also known as “Satzartenkennzeichen”.



38
39
40
# File 'lib/datanorm/lines/base.rb', line 38

def id
  raise "Implement ##{__method__} in #{self.class}"
end

#record_kindObject

The first character in every line always represents the record type. E.g. “T”, “A”



48
49
50
# File 'lib/datanorm/lines/base.rb', line 48

def record_kind
  columns[0]
end

#to_jsonObject



62
63
64
# File 'lib/datanorm/lines/base.rb', line 62

def to_json(...)
  as_json.to_json(...)
end

#to_sObject

Overridden in subclasses.



53
54
55
# File 'lib/datanorm/lines/base.rb', line 53

def to_s
  to_json
end