Class: HL7::Message::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-hl7.rb

Overview

Ruby Object representation of an hl7 2.x message segment The segments can be setup to provide aliases to specific fields with optional validation code that is run when the field is modified The segment field data is also accessible via the e<number> method.

Defining a New Segment

class HL7::Message::Segment::NK1 < HL7::Message::Segment
  wieght 100 # segments are sorted ascendingly
  add_field :name=>:something_you_want       # assumes :idx=>1
  add_field :name=>:something_else, :idx=>6  # :idx=>6 and field count=6
  add_field :name=>:something_more           # :idx=>7
  add_field :name=>:block_example do |value|
    raise HL7::InvalidDataError.new unless value.to_i < 100 && value.to_i > 10
    return value
  end 
  # this block will be executed when seg.block_example= is called
  # and when seg.block_example is called

Direct Known Subclasses

Default, EVN, MSA, MSH, NTE, OBR, OBX, ORU, PID, PV1, PV2, QRD, QRF

Defined Under Namespace

Classes: Default, EVN, MSA, MSH, NTE, OBR, OBX, ORU, PID, PV1, PV2, QRD, QRF

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_segment = "") ⇒ Segment

setup a new HL7::Message::Segment

raw_segment

is an optional String or Array which will be used as the segment’s field data



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/ruby-hl7.rb', line 286

def initialize(raw_segment="")
  @segments_by_name = {}
  @element_delim = '|'
  @field_total = 0

  if (raw_segment.kind_of? Array)
    @elements = raw_segment
  else
    @elements = raw_segment.split( element_delim, -1 )
    if raw_segment == ""
      @elements[0] = self.class.to_s.split( "::" ).last 
      @elements << ""
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object

handle the e<number> field accessor and any aliases that didn’t get added to the system automatically



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/ruby-hl7.rb', line 316

def method_missing( sym, *args, &blk )
  base_str = sym.to_s.gsub( "=", "" )
  base_sym = base_str.to_sym

  if self.class.fields.include?( base_sym )
    # base_sym is ok, let's move on
  elsif /e([0-9]+)/.match( base_str )
    # base_sym should actually be $1, since we're going by
    # element id number
    base_sym = $1.to_i
  else
    super.method_missing( sym, args, blk )  
  end

  if sym.to_s.include?( "=" )
    write_field( base_sym, args )
  else
    read_field( base_sym )
  end
end

Instance Attribute Details

#element_delimObject (readonly)

Returns the value of attribute element_delim.



279
280
281
# File 'lib/ruby-hl7.rb', line 279

def element_delim
  @element_delim
end

#item_delimObject (readonly)

Returns the value of attribute item_delim.



280
281
282
# File 'lib/ruby-hl7.rb', line 280

def item_delim
  @item_delim
end

#segment_weightObject (readonly)

Returns the value of attribute segment_weight.



281
282
283
# File 'lib/ruby-hl7.rb', line 281

def segment_weight
  @segment_weight
end

Instance Method Details

#<=>(other) ⇒ Object



337
338
339
340
341
342
343
344
# File 'lib/ruby-hl7.rb', line 337

def <=>( other ) 
  return nil unless other.kind_of?(HL7::Message::Segment)

  diff = self.weight - other.weight
  return -1 if diff > 0
  return 1 if diff < 0
  return 0
end

#to_infoObject



302
303
304
# File 'lib/ruby-hl7.rb', line 302

def to_info
  "%s: empty segment >> %s" % [ self.class.to_s, @elements.inspect ] 
end

#to_sObject Also known as: to_hl7

output the HL7 spec version of the segment



307
308
309
# File 'lib/ruby-hl7.rb', line 307

def to_s
  @elements.join( @element_delim )
end

#weightObject

get the defined sort-weight of this segment class an alias for self.weight



348
349
350
# File 'lib/ruby-hl7.rb', line 348

def weight
  self.class.weight
end