Class: OpenC3::XmlAccessor
- Defined in:
- lib/openc3/accessors/xml_accessor.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Accessor
Class Method Summary collapse
- .buffer_to_doc(buffer) ⇒ Object
- .doc_to_buffer(doc) ⇒ Object
- .read_item(item, buffer) ⇒ Object
- .read_items(items, buffer) ⇒ Object
- .write_item(item, value, buffer) ⇒ Object
- .write_items(items, values, buffer) ⇒ Object
Instance Method Summary collapse
-
#enforce_derived_write_conversion(_item) ⇒ Object
If this is true it will enforce that COSMOS DERIVED items must have a write_conversion to be written.
-
#enforce_encoding ⇒ Object
If this is set it will enforce that buffer data is encoded in a specific encoding.
-
#enforce_length ⇒ Object
This affects whether the Packet class enforces the buffer length at all.
-
#enforce_short_buffer_allowed ⇒ Object
This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size.
Methods inherited from Accessor
#args, convert_to_type, #initialize, #read_item, #read_items, #write_item, #write_items
Constructor Details
This class inherits a constructor from OpenC3::Accessor
Class Method Details
.buffer_to_doc(buffer) ⇒ Object
70 71 72 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 70 def self.buffer_to_doc(buffer) Nokogiri.XML(buffer) end |
.doc_to_buffer(doc) ⇒ Object
74 75 76 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 74 def self.doc_to_buffer(doc) doc.to_xml end |
.read_item(item, buffer) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 19 def self.read_item(item, buffer) return nil if item.data_type == :DERIVED doc = buffer_to_doc(buffer) doc_value = doc.xpath(item.key) # Nokogiri returns a Nokogiri::XML::Text which responds to first # unless they've applied some XPath functions to the result like normalize-space if doc_value.respond_to?(:first) doc_value = doc_value.first end return convert_to_type(doc_value.to_s, item) end |
.read_items(items, buffer) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 40 def self.read_items(items, buffer) doc = buffer_to_doc(buffer) result = {} items.each do |item| if item.data_type == :DERIVED result[item.name] = nil else doc_value = doc.xpath(item.key) # Nokogiri returns a Nokogiri::XML::Text which responds to first # unless they've applied some XPath functions to the result like normalize-space if doc_value.respond_to?(:first) doc_value = doc_value.first end result[item.name] = convert_to_type(doc_value.to_s, item) end end return result end |
.write_item(item, value, buffer) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 31 def self.write_item(item, value, buffer) return nil if item.data_type == :DERIVED doc = buffer_to_doc(buffer) node = doc.xpath(item.key).first node.content = value.to_s buffer.replace(doc_to_buffer(doc)) return value end |
.write_items(items, values, buffer) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 59 def self.write_items(items, values, buffer) doc = buffer_to_doc(buffer) items.each_with_index do |item, index| next if item.data_type == :DERIVED node = doc.xpath(item.key).first node.content = values[index].to_s end buffer.replace(doc_to_buffer(doc)) return values end |
Instance Method Details
#enforce_derived_write_conversion(_item) ⇒ Object
If this is true it will enforce that COSMOS DERIVED items must have a write_conversion to be written
100 101 102 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 100 def enforce_derived_write_conversion(_item) return true end |
#enforce_encoding ⇒ Object
If this is set it will enforce that buffer data is encoded in a specific encoding
80 81 82 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 80 def enforce_encoding return nil end |
#enforce_length ⇒ Object
This affects whether the Packet class enforces the buffer length at all. Set to false to remove any correlation between buffer length and defined sizes of items in COSMOS
87 88 89 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 87 def enforce_length return false end |
#enforce_short_buffer_allowed ⇒ Object
This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size. Items outside the buffer bounds will return nil when read.
94 95 96 |
# File 'lib/openc3/accessors/xml_accessor.rb', line 94 def enforce_short_buffer_allowed return true end |