Class: RTP::Record

Inherits:
Object
  • Object
show all
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.

Constant Summary collapse

NR_SURPLUS_ATTRIBUTES =

For most record types, there are no surplus (grouped) attributes.

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, min_elements, max_elements) ⇒ Record

Creates a new Record.

Parameters:

  • keyword (String)

    the keyword which identifies this record

  • min_elements (Integer)

    the minimum number of data elements required for this record

  • max_elements (Integer)

    the maximum supported number of data elements for this record



24
25
26
27
28
# File 'lib/rtp-connect/record.rb', line 24

def initialize(keyword, min_elements, max_elements)
  @keyword = keyword
  @min_elements = min_elements
  @max_elements = max_elements
end

Instance Attribute Details

#attributesObject (readonly)

An array of the record’s attributes.



12
13
14
# File 'lib/rtp-connect/record.rb', line 12

def attributes
  @attributes
end

#crcObject

The CRC is used to validate the integrity of the content of the RTP string line.



16
17
18
# File 'lib/rtp-connect/record.rb', line 16

def crc
  @crc
end

#keywordObject

The keyword defines the record type of a particular RTP string line.



14
15
16
# File 'lib/rtp-connect/record.rb', line 14

def keyword
  @keyword
end

Instance Method Details

#encode(options = {}) ⇒ String

Encodes a string from the contents of this instance.

This produces the full record string line, including a computed CRC checksum.

Parameters:

  • options (Hash) (defaults to: {})

    an optional hash parameter

Options Hash (options):

  • :version (Float)

    the Mosaiq compatibility version number (e.g. 2.4) used for the output

Returns:

  • (String)

    a proper RTPConnect type CSV string



47
48
49
50
51
52
53
54
# File 'lib/rtp-connect/record.rb', line 47

def encode(options={})
  encoded_values = values.collect {|v| v && v.encode('ISO8859-1')}
  encoded_values = discard_unsupported_attributes(encoded_values, options) if options[:version]
  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.

Parameters:

  • last_parent (Record)

    the previous parent (the record from the previous line in the RTP file)

  • klass (Record)

    the expected parent record class of this record (e.g. Plan, Field)



61
62
63
64
65
66
67
# File 'lib/rtp-connect/record.rb', line 61

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.

Parameters:

  • string (#to_s)

    the extended treatment field definition record string line

Returns:

  • (Record)

    the updated Record instance

Raises:

  • (ArgumentError)

    if given a string containing an invalid number of elements



86
87
88
89
90
91
92
93
# File 'lib/rtp-connect/record.rb', line 86

def load(string, options={})
  # Extract processed values:
  values = string.to_s.values(options[: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_recordRecord

Returns self.

Returns:



99
100
101
# File 'lib/rtp-connect/record.rb', line 99

def to_record
  self
end

#to_s(options = {}) ⇒ String Also known as: to_str

Encodes the record + any hiearchy of child objects, to a properly formatted RTPConnect ascii string.

Parameters:

  • options (Hash) (defaults to: {})

    an optional hash parameter

Options Hash (options):

  • :version (Float)

    the Mosaiq compatibility version number (e.g. 2.4) used for the output

Returns:

  • (String)

    an RTP string with a single or multiple lines/records



110
111
112
113
114
115
116
117
# File 'lib/rtp-connect/record.rb', line 110

def to_s(options={})
  str = encode(options)
  children.each do |child|
    # Note that the extended plan record was introduced in Mosaiq 2.5.
    str += child.to_s(options) unless child.class == ExtendedPlan && options[:version].to_f < 2.5
  end
  str
end

#valuesArray<String>

Note:

The CRC is not considered part of the actual values and is excluded.

Collects the values (attributes) of this instance.

Returns:

  • (Array<String>)

    an array of attributes (in the same order as they appear in the RTP string)



126
127
128
# File 'lib/rtp-connect/record.rb', line 126

def values
  @attributes.collect {|attribute| self.send(attribute)}
end