Module: HL7::Message::SegmentFields

Included in:
Segment
Defined in:
lib/segment_fields.rb

Overview

SegmentFields

class HL7::Message::Segment::NK1 < HL7::Message::Segment
  weight 100 # segments are sorted ascendingly
  add_field :something_you_want       # assumes :idx=>1
  add_field :something_else, :idx=>6  # :idx=>6 and field count=6

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/segment_fields.rb', line 9

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#[](index) ⇒ Object



93
94
95
# File 'lib/segment_fields.rb', line 93

def [](index)
  @elements[index]
end

#[]=(index, value) ⇒ Object



97
98
99
# File 'lib/segment_fields.rb', line 97

def []=(index, value)
  @elements[index] = value.to_s
end

#field_info(name) ⇒ Object

:nodoc:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/segment_fields.rb', line 81

def field_info(name) # :nodoc:
  field_blk = nil
  idx = name # assume we've gotten a integer
  unless name.is_a?(Integer)
    fld_info = self.class.fields[name]
    idx = fld_info[:idx].to_i
    field_blk = fld_info[:blk]
  end

  [idx, field_blk]
end

#read_field(name) ⇒ Object

:nodoc:



101
102
103
104
105
106
107
108
109
110
# File 'lib/segment_fields.rb', line 101

def read_field(name) # :nodoc:
  idx, field_blk = field_info(name)
  return nil unless idx
  return nil if idx >= @elements.length

  ret = @elements[idx]
  ret = ret.first if ret.is_a?(Array) && ret.length == 1
  ret = field_blk.call(ret) if field_blk
  ret
end

#write_field(name, value) ⇒ Object

:nodoc:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/segment_fields.rb', line 112

def write_field(name, value) # :nodoc:
  idx, field_blk = field_info(name)
  return nil unless idx

  if idx >= @elements.length
    # make some space for the incoming field, missing items are assumed to
    # be empty, so this is valid per the spec -mg
    missing = ("," * (idx - @elements.length)).split(",", -1)
    @elements += missing
  end

  value = value.first if value.is_a?(Array) && value.length == 1
  value = field_blk.call(value) if field_blk
  @elements[idx] = value.to_s
end