Class: Eddy::Parse::Interchange
- Inherits:
-
Object
- Object
- Eddy::Parse::Interchange
- Defined in:
- lib/eddy/parse.rb
Overview
Wrapper around an interchange file or string.
Instance Attribute Summary collapse
-
#component_separator ⇒ String
Component Element Separator.
-
#document ⇒ String
Raw content of the interchange.
-
#element_separator ⇒ String
Data Element Separator.
-
#segment_separator ⇒ String
("~").
Class Method Summary collapse
Instance Method Summary collapse
- #initialize(doc, component_separator: ">", segment_separator: "~", element_separator: "*") ⇒ void constructor
- #parse ⇒ Array<Hash>
- #parse_segment(seg) ⇒ Hash
Constructor Details
#initialize(doc, component_separator: ">", segment_separator: "~", element_separator: "*") ⇒ void
23 24 25 26 27 28 |
# File 'lib/eddy/parse.rb', line 23 def initialize(doc, component_separator: ">", segment_separator: "~", element_separator: "*") self.document = doc self.component_separator = component_separator self.segment_separator = segment_separator self.element_separator = element_separator end |
Instance Attribute Details
#component_separator ⇒ String
Component Element Separator
11 12 13 |
# File 'lib/eddy/parse.rb', line 11 def component_separator @component_separator end |
#document ⇒ String
Raw content of the interchange.
8 9 10 |
# File 'lib/eddy/parse.rb', line 8 def document @document end |
#element_separator ⇒ String
Data Element Separator
16 17 18 |
# File 'lib/eddy/parse.rb', line 16 def element_separator @element_separator end |
#segment_separator ⇒ String
Returns ("~").
13 14 15 |
# File 'lib/eddy/parse.rb', line 13 def segment_separator @segment_separator end |
Class Method Details
.from_file(path, **kwargs) ⇒ self
32 33 34 |
# File 'lib/eddy/parse.rb', line 32 def self.from_file(path, **kwargs) return self.new(File.read(path), **kwargs) end |
Instance Method Details
#parse ⇒ Array<Hash>
37 38 39 40 41 |
# File 'lib/eddy/parse.rb', line 37 def parse() doc = self.document.gsub(/\n/, "") segments = doc.split(self.segment_separator) segments.map { |seg| self.parse_segment(seg) } end |
#parse_segment(seg) ⇒ Hash
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/eddy/parse.rb', line 45 def parse_segment(seg) name, *elements = seg.split(self.element_separator) res = { name: name, elements: {} } elements.each_with_index do |value, index| next if value == "" key = sprintf("%s%02d", name, index + 1) res[:elements][key] = value end return res end |