Class: LensProtocol::OMA::Type::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lens_protocol/oma/type/base.rb

Direct Known Subclasses

Integer, Numeric, R, Text, Trcfmt

Instance Method Summary collapse

Constructor Details

#initialize(mode: :single_value) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/lens_protocol/oma/type/base.rb', line 5

def initialize mode: :single_value
  @mode = mode
end

Instance Method Details

#format(record, _message) ⇒ Array of lines or a single one

Returns:

  • (Array of lines or a single one)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lens_protocol/oma/type/base.rb', line 28

def format record, _message
  case @mode
  when :single_value
    format_line record.label, [format_value(record.value)]
  when :array_of_values
    format_line record.label, format_values(record.value)
  when :chiral
    format_line record.label, format_chiral(record.value)
  when :matrix_of_values
    record.value.map do |value|
      format_line record.label, format_values(value)
    end
  else
    raise ArgumentError, "Mode #{@mode} not supported"
  end
end

#parse(line, message) ⇒ Message

Given a line and a message produces a new message with the record(s) corresponding to that line added to the message

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lens_protocol/oma/type/base.rb', line 11

def parse line, message
  label, data = label_and_data line
  case @mode
  when :single_value
    message.add_record label, parse_value(data)
  when :array_of_values
    message.add_record_or_concat_values label, parse_values(data)
  when :chiral
    message.add_record label, parse_chiral(data)
  when :matrix_of_values
    message.add_record_or_insert_values label, parse_values(data)
  else
    raise ArgumentError, "Mode #{@mode} not supported"
  end
end