Class: Orgill::Products::LegSource

Inherits:
Object
  • Object
show all
Defined in:
lib/orgill/products/leg_extractor.rb

Overview

Kiba source for the “Legacy” or LTS Orgill file format.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data: nil, file: nil, index_map: nil) ⇒ LegSource

Note:

You can't use both data and file

Sets #source, parses it into #products.

Raises:

  • (ArgumentError)

30
31
32
33
34
35
36
# File 'lib/orgill/products/leg_extractor.rb', line 30

def initialize(data: nil, file: nil, index_map: nil)
  raise(ArgumentError) if file.nil? && data.nil? # if no arguments
  raise(ArgumentError) if !file.nil? && !data.nil? # if too many sources

  @source   = data || File.read(file)
  @products = self.class.parse(@source, index_map: index_map)
end

Instance Attribute Details

#productsObject (readonly)

Returns the value of attribute products


5
6
7
# File 'lib/orgill/products/leg_extractor.rb', line 5

def products
  @products
end

#sourceObject (readonly)

Returns the value of attribute source


5
6
7
# File 'lib/orgill/products/leg_extractor.rb', line 5

def source
  @source
end

Class Method Details

.parse(string, index_map: nil) ⇒ Array

Note:

don't use literal strings here. 'rn' isn't processed properly

Returns Array of Arrays, or Array of Hashes if index_map is used. Bottom elements of the Arrays or Hashes are Strings.

Examples:

Orgill::Products::LegSource.parse("foo~   bar ~ baz ~\r\nboo~ ~biz")
  #=> [['foo', 'bar', 'baz'], ['boo', '', 'biz']]

16
17
18
19
20
21
22
23
24
# File 'lib/orgill/products/leg_extractor.rb', line 16

def self.parse(string, index_map: nil)
  detabularize(string).map do |row|
    unless index_map.nil?
      Hash[row.each_with_index.map { |val, ind| [index_map[ind], val] }]
    else
      row
    end
  end
end

Instance Method Details

#eachObject

Note:

Required for usage as a kiba source


39
40
41
# File 'lib/orgill/products/leg_extractor.rb', line 39

def each
  @products.each { |p| yield(p) }
end