Class: LensProtocol::OMA::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records: {}) ⇒ Message

Returns a new instance of Message.



11
12
13
# File 'lib/lens_protocol/oma/message.rb', line 11

def initialize records: {}
  @records = records
end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



4
5
6
# File 'lib/lens_protocol/oma/message.rb', line 4

def records
  @records
end

Class Method Details

.from_hash(*args) ⇒ Object

Builds a message from a hash of record labels to record value.



7
8
9
# File 'lib/lens_protocol/oma/message.rb', line 7

def self.from_hash *args
  OMA.generate *args
end

Instance Method Details

#add_record(label, value) ⇒ Object



15
16
17
18
# File 'lib/lens_protocol/oma/message.rb', line 15

def add_record label, value
  @records[label] ||= Record.new(label: label, value: value)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/lens_protocol/oma/message.rb', line 33

def empty?
  @records.empty?
end

#except(labels) ⇒ Object

Returns a new message without the records of the given labels.

Parameters:

  • labels (Array)


65
66
67
# File 'lib/lens_protocol/oma/message.rb', line 65

def except labels
  Message.new records: @records.reject { |label, _| labels.include? label }
end

#include?(label) ⇒ Boolean

Returns true if the message contains a record with the given label

Returns:

  • (Boolean)


29
30
31
# File 'lib/lens_protocol/oma/message.rb', line 29

def include? label
  @records.key? label
end

#merge(other) ⇒ Object

Similarly to Hash#merge returns a new message containing the records of this and the records of other keeping the ones in other if the labels colides.



59
60
61
# File 'lib/lens_protocol/oma/message.rb', line 59

def merge other
  Message.new records: @records.merge(other.records)
end

#only(labels) ⇒ Object

Returns a new message with only the records of the given labels.

Parameters:

  • labels (Array)


71
72
73
# File 'lib/lens_protocol/oma/message.rb', line 71

def only labels
  Message.new records: @records.slice(*labels)
end

#remove_empty_recordsObject



75
76
77
# File 'lib/lens_protocol/oma/message.rb', line 75

def remove_empty_records
  Message.new records: @records.reject { |_, r| r.empty? }
end

#to_hashObject



37
38
39
# File 'lib/lens_protocol/oma/message.rb', line 37

def to_hash
  Hash[*@records.flat_map { |label, record| [label, record.value] }]
end

#to_sObject



79
80
81
# File 'lib/lens_protocol/oma/message.rb', line 79

def to_s
  OMA.format self
end

#to_svg(**opts) ⇒ Object

Returns an array of SVG strings, one for each side. If the tracing format is not recognized or there is no tracing data, returns an empty array.



53
54
55
# File 'lib/lens_protocol/oma/message.rb', line 53

def to_svg **opts
  SVG.from_message self, **opts
end

#tracing_in_polar_coordinatesObject

Converts the “R” record values to polar coordinates.



42
43
44
# File 'lib/lens_protocol/oma/message.rb', line 42

def tracing_in_polar_coordinates
  value_of('TRCFMT', []).map { |tracing_dataset| tracing_dataset&.in_polar_coordinates || [] }
end

#tracing_in_rectangular_coordinatesObject

Converts the “R” record values to rectangular coordinates.



47
48
49
# File 'lib/lens_protocol/oma/message.rb', line 47

def tracing_in_rectangular_coordinates
  value_of('TRCFMT', []).map { |tracing_dataset| tracing_dataset&.in_rectangular_coordinates || [] }
end

#value_of(label, default = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/lens_protocol/oma/message.rb', line 20

def value_of label, default = nil
  if include? label
    @records[label].value
  else
    default
  end
end