Class: Datanorm::Lines::Base
- Inherits:
-
Object
- Object
- Datanorm::Lines::Base
- Defined in:
- lib/datanorm/lines/base.rb
Overview
Object that represents one line of a Datanorm file.
Direct Known Subclasses
V4::Dimension, V4::Extra, V4::Priceset, V4::Product, V4::Text, V5::Dimension, V5::Product, V5::Text
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Array that holds all attributes of one line.
-
#source_line_number ⇒ Object
readonly
Where in the originating Datanorm file this line is located.
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
This class is subclassed by one type per row.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#encode ⇒ Object
Convenience Shortcut to convert attributes from CP850 to UTF-8.
-
#id ⇒ Object
Every row has a unique identifier.
-
#initialize(columns:, source_line_number:) ⇒ Base
constructor
A new instance of Base.
-
#record_kind ⇒ Object
The first character in every line always represents the record type.
- #to_json ⇒ Object
-
#to_s ⇒ Object
Overridden in subclasses.
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
#columns ⇒ Object (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_number ⇒ Object (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_json ⇒ Object
42 43 44 |
# File 'lib/datanorm/lines/base.rb', line 42 def as_json raise "Implement ##{__method__} in #{self.class}" end |
#encode ⇒ Object
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 |
#id ⇒ Object
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_kind ⇒ Object
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_json ⇒ Object
62 63 64 |
# File 'lib/datanorm/lines/base.rb', line 62 def to_json(...) as_json.to_json(...) end |
#to_s ⇒ Object
Overridden in subclasses.
53 54 55 |
# File 'lib/datanorm/lines/base.rb', line 53 def to_s to_json end |