Class: SimpleHL7::Composite
- Inherits:
-
Object
- Object
- SimpleHL7::Composite
- 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
Class Method Summary collapse
-
.current_separator_char(separator_chars) ⇒ Object
abstract
generating a HL7 string.
-
.parse(str, separator_chars) ⇒ Composite
Create a composite from a HL7 string.
-
.start_index ⇒ Object
The index where the first subcomposite is located.
- .subcomposite_class ⇒ Object abstract
Instance Method Summary collapse
-
#[](index) ⇒ Subcomposite
Get a specific subcomposite.
-
#[]=(index, value) ⇒ Object
Set the value of the specified subcomposite.
-
#each ⇒ Object
Calls the specified block once for each index between the start index and the max specified subcomposite index.
-
#get_subcomposite(index) ⇒ Object
Alias for [].
-
#initialize(value = nil) ⇒ Composite
constructor
Constructor.
-
#map ⇒ Object
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.
-
#set_subcomposite(index, value) ⇒ Object
Sets a specific subcomposite.
-
#to_a ⇒ Object
Get all the subcomposites as an array.
-
#to_hl7(separator_chars) ⇒ Object
Get a HL7 string representation of this Composite.
-
#to_s ⇒ Object
Get the value stored at the first Subcomponent below this Composite.
Constructor Details
#initialize(value = nil) ⇒ Composite
Constructor
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
The character that is used to separate the subcomposites when
generating a HL7 string.
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.
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_index ⇒ Object
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_class ⇒ Object
The class that is used for subcomposites.
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.
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.
23 24 25 |
# File 'lib/simple_hl7/composite.rb', line 23 def []=(index, value) set_subcomposite(index, self.class.subcomposite_class.new(value)) end |
#each ⇒ Object
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 |
#map ⇒ Object
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
54 55 56 |
# File 'lib/simple_hl7/composite.rb', line 54 def set_subcomposite(index, value) @subcomposites[index] = value end |
#to_a ⇒ Object
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_s ⇒ Object
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 |