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
Returns a new instance of 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.
119 120 121 |
# File 'lib/lens_protocol/oma/message.rb', line 119 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.
113 114 115 |
# File 'lib/lens_protocol/oma/message.rb', line 113 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.
125 126 127 |
# File 'lib/lens_protocol/oma/message.rb', line 125 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 85 |
# 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| format_number, = value_of('TRCFMT')[side] case format_number.to_i 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
92 93 94 |
# File 'lib/lens_protocol/oma/message.rb', line 92 def radiuses_to_polar radiuses radiuses.map.with_index { |r, i| [i * 2 * Math::PI / radiuses.size, r] } end |
#radiuses_to_rectangular(radiuses) ⇒ Object
101 102 103 |
# File 'lib/lens_protocol/oma/message.rb', line 101 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
129 130 131 |
# File 'lib/lens_protocol/oma/message.rb', line 129 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
133 134 135 |
# File 'lib/lens_protocol/oma/message.rb', line 133 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.
107 108 109 |
# File 'lib/lens_protocol/oma/message.rb', line 107 def to_svg **opts SVG. self, **opts end |
#tracing_in_polar_coordinates ⇒ Object
Converts the “R” record values to polar coordinates.
88 89 90 |
# File 'lib/lens_protocol/oma/message.rb', line 88 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.
97 98 99 |
# File 'lib/lens_protocol/oma/message.rb', line 97 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 |