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(hash) ⇒ Object
Builds a message from a hash of record labels to record value.
Instance Method Summary collapse
- #add_record(label, value) ⇒ Object
- #add_record_or_concat_values(label, values) ⇒ Object
- #add_record_or_insert_values(label, values) ⇒ Object
- #add_record_side_values(label, side, values) ⇒ Object
- #context(key) ⇒ Object
- #empty? ⇒ Boolean
-
#except(labels) ⇒ Object
Returns a new message without the records of the given labels.
-
#include?(label) ⇒ Boolean
Returns
trueif the message contains a record with the given label. -
#initialize(records: {}, context: {}) ⇒ Message
constructor
A new instance of Message.
-
#merge(other) ⇒ Object
Similarly to Hash#merge returns a new message containing the records of
thisand the records ofotherkeeping the ones inotherif the labels colides. -
#only(labels) ⇒ Object
Returns a new message with only the records of the given labels.
-
#radius_data ⇒ Object
Returns the “R” reconds decoded radiuses according to the tracing format.
- #radiuses_to_polar(radiuses) ⇒ Object
- #radiuses_to_rectangular(radiuses) ⇒ Object
- #remove_empty_records ⇒ Object
- #set_context(key, value) ⇒ 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: {}, context: {}) ⇒ Message
13 14 15 16 |
# File 'lib/lens_protocol/oma/message.rb', line 13 def initialize records: {}, context: {} @records = records @context = context 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
.from_hash(hash) ⇒ Object
Builds a message from a hash of record labels to record value.
7 8 9 10 11 |
# File 'lib/lens_protocol/oma/message.rb', line 7 def self.from_hash hash hash.reduce new do |, (label, value)| .add_record(label, value) end end |
Instance Method Details
#add_record(label, value) ⇒ Object
18 19 20 21 |
# File 'lib/lens_protocol/oma/message.rb', line 18 def add_record label, value @records[label] ||= Record.new(label: label, value: value) self end |
#add_record_or_concat_values(label, values) ⇒ Object
29 30 31 32 33 |
# File 'lib/lens_protocol/oma/message.rb', line 29 def add_record_or_concat_values label, values @records[label] ||= Record.new(label: label, value: []) @records[label].value.concat values self end |
#add_record_or_insert_values(label, values) ⇒ Object
23 24 25 26 27 |
# File 'lib/lens_protocol/oma/message.rb', line 23 def add_record_or_insert_values label, values @records[label] ||= Record.new(label: label, value: []) @records[label].value << values self end |
#add_record_side_values(label, side, values) ⇒ Object
35 36 37 38 39 |
# File 'lib/lens_protocol/oma/message.rb', line 35 def add_record_side_values label, side, values @records[label] ||= Record.new(label: label, value: [[], []]) @records[label].value[side].concat values self end |
#context(key) ⇒ Object
63 64 65 |
# File 'lib/lens_protocol/oma/message.rb', line 63 def context key @context[key] end |
#empty? ⇒ Boolean
59 60 61 |
# File 'lib/lens_protocol/oma/message.rb', line 59 def empty? @records.empty? end |
#except(labels) ⇒ Object
Returns a new message without the records of the given labels.
118 119 120 |
# File 'lib/lens_protocol/oma/message.rb', line 118 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
55 56 57 |
# File 'lib/lens_protocol/oma/message.rb', line 55 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.
112 113 114 |
# File 'lib/lens_protocol/oma/message.rb', line 112 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.
124 125 126 |
# File 'lib/lens_protocol/oma/message.rb', line 124 def only labels Message.new records: @records.slice(*labels) end |
#radius_data ⇒ Object
Returns the “R” reconds decoded radiuses according to the tracing format.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/lens_protocol/oma/message.rb', line 72 def radius_data return [] unless value_of('TRCFMT') && value_of('R') [0, 1].map do |side| case Type::Trcfmt.number(value_of('TRCFMT')[side]) when 0 # side not present [] when 1 # ASCII format value_of('R')[side] else # unknown format return [] end end end |
#radiuses_to_polar(radiuses) ⇒ Object
91 92 93 |
# File 'lib/lens_protocol/oma/message.rb', line 91 def radiuses_to_polar radiuses radiuses.map.with_index { |r, i| [i * 2 * Math::PI / radiuses.size, r] } end |
#radiuses_to_rectangular(radiuses) ⇒ Object
100 101 102 |
# File 'lib/lens_protocol/oma/message.rb', line 100 def radiuses_to_rectangular radiuses radiuses_to_polar(radiuses).map { |(a, r)| [r * Math.cos(a), r * Math.sin(a)].map { |v| v.round 2 } } end |
#remove_empty_records ⇒ Object
128 129 130 |
# File 'lib/lens_protocol/oma/message.rb', line 128 def remove_empty_records Message.new records: @records.reject { |_, r| r.empty? } end |
#set_context(key, value) ⇒ Object
41 42 43 44 |
# File 'lib/lens_protocol/oma/message.rb', line 41 def set_context key, value @context[key] = value self end |
#to_hash ⇒ Object
67 68 69 |
# File 'lib/lens_protocol/oma/message.rb', line 67 def to_hash Hash[*@records.flat_map { |label, record| [label, record.value] }] end |
#to_s ⇒ Object
132 133 134 |
# File 'lib/lens_protocol/oma/message.rb', line 132 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.
106 107 108 |
# File 'lib/lens_protocol/oma/message.rb', line 106 def to_svg **opts SVG. self, **opts end |
#tracing_in_polar_coordinates ⇒ Object
Converts the “R” record values to polar coordinates.
87 88 89 |
# File 'lib/lens_protocol/oma/message.rb', line 87 def tracing_in_polar_coordinates radius_data.map { |radiuses| radiuses_to_polar radiuses } end |
#tracing_in_rectangular_coordinates ⇒ Object
Converts the “R” record values to rectangular coordinates.
96 97 98 |
# File 'lib/lens_protocol/oma/message.rb', line 96 def tracing_in_rectangular_coordinates radius_data.map { |radiuses| radiuses_to_rectangular radiuses } end |
#value_of(label, default = nil) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/lens_protocol/oma/message.rb', line 46 def value_of label, default = nil if include? label @records[label].value else default end end |