Class: RTP::Record
- Inherits:
-
Object
- Object
- RTP::Record
- Defined in:
- lib/rtp-connect/record.rb
Overview
The Record class contains attributes and methods that are common for the various record types defined in the RTPConnect standard.
Direct Known Subclasses
ControlPoint, DoseTracking, ExtendedField, ExtendedPlan, Field, Plan, Prescription, SimulationField, SiteSetup
Instance Attribute Summary collapse
-
#crc ⇒ Object
The CRC is used to validate the integrity of the content of the RTP string line.
-
#keyword ⇒ Object
The keyword defines the record type of a particular RTP string line.
Instance Method Summary collapse
-
#encode ⇒ String
Encodes a string from the contents of this instance.
-
#get_parent(last_parent, klass) ⇒ Object
Follows the tree of parents until the appropriate parent of the requesting record is found.
-
#initialize(keyword, min_elements, max_elements) ⇒ Record
constructor
Creates a new Record.
-
#load(string, options = {}) ⇒ Record
Sets up a record by parsing a RTPConnect string line.
-
#to_record ⇒ Record
Returns self.
-
#values ⇒ Array<String>
Collects the values (attributes) of this instance.
Constructor Details
#initialize(keyword, min_elements, max_elements) ⇒ Record
Creates a new Record.
19 20 21 22 23 |
# File 'lib/rtp-connect/record.rb', line 19 def initialize(keyword, min_elements, max_elements) @keyword = keyword @min_elements = min_elements @max_elements = max_elements end |
Instance Attribute Details
#crc ⇒ Object
The CRC is used to validate the integrity of the content of the RTP string line.
11 12 13 |
# File 'lib/rtp-connect/record.rb', line 11 def crc @crc end |
#keyword ⇒ Object
The keyword defines the record type of a particular RTP string line.
9 10 11 |
# File 'lib/rtp-connect/record.rb', line 9 def keyword @keyword end |
Instance Method Details
#encode ⇒ String
Encodes a string from the contents of this instance.
This produces the full record string line, including a computed CRC checksum.
40 41 42 43 44 45 46 |
# File 'lib/rtp-connect/record.rb', line 40 def encode encoded_values = values.collect {|v| v && v.encode('ISO8859-1')} content = CSV.generate_line(encoded_values, force_quotes: true, row_sep: '') + "," checksum = content.checksum # Complete string is content + checksum (in double quotes) + carriage return + line feed return (content + checksum.to_s.wrap + "\r\n").encode('ISO8859-1') end |
#get_parent(last_parent, klass) ⇒ Object
Follows the tree of parents until the appropriate parent of the requesting record is found.
53 54 55 56 57 58 59 |
# File 'lib/rtp-connect/record.rb', line 53 def get_parent(last_parent, klass) if last_parent.is_a?(klass) return last_parent else return last_parent.get_parent(last_parent.parent, klass) end end |
#load(string, options = {}) ⇒ Record
Sets up a record by parsing a RTPConnect string line.
78 79 80 81 82 83 84 85 |
# File 'lib/rtp-connect/record.rb', line 78 def load(string, ={}) # Extract processed values: values = string.to_s.values([:repair]) raise ArgumentError, "Invalid argument 'string': Expected at least #{@min_elements} elements for #{@keyword}, got #{values.length}." if values.length < @min_elements RTP.logger.warn "The number of given elements (#{values.length}) exceeds the known number of data elements for this record (#{@max_elements}). This may indicate an invalid string record or that the RTP format has recently been expanded with new elements." if values.length > @max_elements self.send(:set_attributes, values) self end |
#to_record ⇒ Record
Returns self.
91 92 93 |
# File 'lib/rtp-connect/record.rb', line 91 def to_record self end |