Class: LensProtocol::OMA::Message
- Inherits:
-
Object
- Object
- LensProtocol::OMA::Message
- Defined in:
- lib/lens_protocol/oma/message.rb
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Class Method Summary collapse
-
.from_hash(*args) ⇒ Object
Builds a message from a hash of record labels to record value.
Instance Method Summary collapse
- #add_record(label, value) ⇒ Object
- #empty? ⇒ Boolean
-
#except(labels) ⇒ Object
Returns a new message without the records of the given labels.
-
#include?(label) ⇒ Boolean
Returns
true
if the message contains a record with the given label. -
#initialize(records: {}) ⇒ Message
constructor
A new instance of Message.
-
#merge(other) ⇒ Object
Similarly to Hash#merge returns a new message containing the records of
this
and the records ofother
keeping the ones inother
if the labels colides. -
#only(labels) ⇒ Object
Returns a new message with only the records of the given labels.
- #remove_empty_records ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
-
#to_svg(**opts) ⇒ Object
Returns an array of SVG strings, one for each side.
-
#tracing_in_polar_coordinates ⇒ Object
Converts the “R” record values to polar coordinates.
-
#tracing_in_rectangular_coordinates ⇒ Object
Converts the “R” record values to rectangular coordinates.
- #value_of(label, default = nil) ⇒ Object
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
#records ⇒ Object (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
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
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.
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
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.
71 72 73 |
# File 'lib/lens_protocol/oma/message.rb', line 71 def only labels Message.new records: @records.slice(*labels) end |
#remove_empty_records ⇒ Object
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_hash ⇒ Object
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_s ⇒ Object
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. self, **opts end |
#tracing_in_polar_coordinates ⇒ Object
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_coordinates ⇒ Object
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 |