Class: SimpleHL7::Composite

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_hl7/composite.rb

Overview

Generic building block of a HL7 message. The parts of the message subclass this, and this class does most of the work.

Direct Known Subclasses

Component, ComponentContainer, Field, Segment

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Composite

Constructor

Parameters:

  • value (String) (defaults to: nil)

    a value that is passed down to the subclass constructor. This allows us to set values on top level components that are passed down to the lowest component. Default nil.



10
11
12
13
14
# File 'lib/simple_hl7/composite.rb', line 10

def initialize(value = nil)
  @subcomposites = {}
  cls = self.class
  @subcomposites[cls.start_index] = cls.subcomposite_class.new(value)
end

Class Method Details

.current_separator_char(separator_chars) ⇒ Object

This method is abstract.

The character that is used to separate the subcomposites when

generating a HL7 string.

Parameters:

  • separator_chars (SeparatorChars)

    The separator characters in use during the HL7 string conversion.

Raises:

  • (Exception)


115
116
117
# File 'lib/simple_hl7/composite.rb', line 115

def self.current_separator_char(separator_chars)
  raise Exception.new("Subclass Responsibility")
end

.parse(str, separator_chars) ⇒ Composite

Create a composite from a HL7 string.

Parameters:

  • str (String)

    The string of HL7.

  • separator_chars (SeparatorChars)

    The separator characters used in the HL7 string.

Returns:



130
131
132
133
134
135
136
# File 'lib/simple_hl7/composite.rb', line 130

def self.parse(str, separator_chars)
  composite = new
  parse_subcomposite_hash(str, separator_chars).each do |index, subc|
    composite.set_subcomposite(start_index + index, subc)
  end
  composite
end

.start_indexObject

The index where the first subcomposite is located. This is usually either 1 or 0 depending on the specific Composite.



106
107
108
# File 'lib/simple_hl7/composite.rb', line 106

def self.start_index
  1
end

.subcomposite_classObject

This method is abstract.

The class that is used for subcomposites.

Raises:

  • (Exception)


120
121
122
# File 'lib/simple_hl7/composite.rb', line 120

def self.subcomposite_class
  raise Exception.new("Subclass Responsibility")
end

Instance Method Details

#[](index) ⇒ Subcomposite

Get a specific subcomposite.

Parameters:

  • index (Integer)

    The index of the subcomposite.

Returns:

  • (Subcomposite)

    The subcomposite at index or a new subcomposite if none exists. Note that this returns the Subcomposite object and not the string value. This differs from how []= works, but it seems to make the most sense when dealing with HL7 messages.



34
35
36
# File 'lib/simple_hl7/composite.rb', line 34

def [](index)
  get_subcomposite(index)
end

#[]=(index, value) ⇒ Object

Set the value of the specified subcomposite. The value is actually passed down to the first subcomposite of the specified subcomposite and so on until it reaches a Subcomponent composite.

Parameters:

  • index (Integer)

    the subcomposite index.

  • value (String)

    the value to set.



23
24
25
# File 'lib/simple_hl7/composite.rb', line 23

def []=(index, value)
  set_subcomposite(index, self.class.subcomposite_class.new(value))
end

#eachObject

Calls the specified block once for each index between the start index and the max specified subcomposite index.



64
65
66
67
# File 'lib/simple_hl7/composite.rb', line 64

def each
  start = self.class.start_index
  (start..max_index).each { |i| yield @subcomposites[i] } if max_index
end

#get_subcomposite(index) ⇒ Object

Alias for []



39
40
41
42
43
44
45
46
# File 'lib/simple_hl7/composite.rb', line 39

def get_subcomposite(index)
  subcomposite = @subcomposites[index]
  if subcomposite.nil?
    subcomposite = self.class.subcomposite_class.new
    set_subcomposite(index, subcomposite)
  end
  subcomposite
end

#mapObject

Calls the specified block once for each index between the start index and the max specified subcomposite index and returns an array resulting from the return values of each block.



76
77
78
79
# File 'lib/simple_hl7/composite.rb', line 76

def map
  start = self.class.start_index
  (start..max_index).map { |i| yield @subcomposites[i] } if max_index
end

#set_subcomposite(index, value) ⇒ Object

Sets a specific subcomposite

Parameters:

  • index (Integer)

    the indexs of the subcomposite, if there is an existing compsite at the location it is replaced.

  • value (Subcomposite)

    the new subcomposite object for the specified location.



54
55
56
# File 'lib/simple_hl7/composite.rb', line 54

def set_subcomposite(index, value)
  @subcomposites[index] = value
end

#to_aObject

Get all the subcomposites as an array. Note that this array has the actual subcomposite object and not clones, so any changes will affect this class as well.



98
99
100
101
102
# File 'lib/simple_hl7/composite.rb', line 98

def to_a
  a = []
  each {|subc| a << subc}
  a
end

#to_hl7(separator_chars) ⇒ Object

Get a HL7 string representation of this Composite.



85
86
87
88
# File 'lib/simple_hl7/composite.rb', line 85

def to_hl7(separator_chars)
  sep_char = self.class.current_separator_char(separator_chars)
  map { |subc| subc.to_hl7(separator_chars) if subc }.join(sep_char)
end

#to_sObject

Get the value stored at the first Subcomponent below this Composite



91
92
93
# File 'lib/simple_hl7/composite.rb', line 91

def to_s
  @subcomposites[self.class.start_index].to_s
end