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 :something_you_want       # assumes :idx=>1
  add_field :something_else, :idx=>6  # :idx=>6 and field count=6
  add_field :something_more           # :idx=>7
  add_field :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 = "", delims = [], &blk) ⇒ 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

delims

an optional array of delimiters, where

delims[0] = element delimiter
delims[1] = item delimiter


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/ruby-hl7.rb', line 343

def initialize(raw_segment="", delims=[], &blk)
  @segments_by_name = {}
  @field_total = 0
  @is_child = false
  
  @element_delim = (delims.kind_of?(Array) && delims.length>0) ? delims[0] : "|"
  @item_delim = (delims.kind_of?(Array) && delims.length>1) ? delims[1] : "^"

  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

  if block_given?
    callctx = eval( "self", blk.binding )
    def callctx.__seg__(val=nil)
      @__seg_val__ ||= val
    end
    callctx.__seg__(self)
    # TODO: find out if this pollutes the calling namespace permanently...
    
    to_do = <<-END
    def method_missing( sym, *args, &blk )
      __seg__.send( sym, args, blk )  
    end
    END

    eval( to_do, blk.binding )
    yield self 
    eval( "undef method_missing", blk.binding )
  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



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/ruby-hl7.rb', line 395

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
  end

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

    if args.length > 0
      write_field( base_sym, args.flatten.select { |arg| arg } )
    else
      read_field( base_sym )
    end

  end
end

Instance Attribute Details

#element_delimObject (readonly)

Returns the value of attribute element_delim.



333
334
335
# File 'lib/ruby-hl7.rb', line 333

def element_delim
  @element_delim
end

#item_delimObject (readonly)

Returns the value of attribute item_delim.



334
335
336
# File 'lib/ruby-hl7.rb', line 334

def item_delim
  @item_delim
end

#segment_parentObject

Returns the value of attribute segment_parent.



332
333
334
# File 'lib/ruby-hl7.rb', line 332

def segment_parent
  @segment_parent
end

#segment_weightObject (readonly)

Returns the value of attribute segment_weight.



335
336
337
# File 'lib/ruby-hl7.rb', line 335

def segment_weight
  @segment_weight
end

Instance Method Details

#<=>(other) ⇒ Object

sort-compare two Segments, 0 indicates equality



423
424
425
426
427
428
429
430
431
# File 'lib/ruby-hl7.rb', line 423

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

  # per Comparable docs: http://www.ruby-doc.org/core/classes/Comparable.html
  diff = self.weight - other.weight
  return -1 if diff > 0
  return 1 if diff < 0
  return 0
end

#is_child_segment=(val) ⇒ Object

indicate whether or not the segment has a parent



446
447
448
# File 'lib/ruby-hl7.rb', line 446

def is_child_segment=(val)
  @is_child_segment = val
end

#is_child_segment?Boolean

return true if the segment has a parent

Returns:

  • (Boolean)


441
442
443
# File 'lib/ruby-hl7.rb', line 441

def is_child_segment?
  (@is_child_segment ||= false)
end

#lengthObject

get the length of the segment (number of fields it contains)



451
452
453
454
# File 'lib/ruby-hl7.rb', line 451

def length
  0 unless @elements
  @elements.length
end

#to_infoObject



381
382
383
# File 'lib/ruby-hl7.rb', line 381

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



386
387
388
# File 'lib/ruby-hl7.rb', line 386

def to_s
  @elements.join( @element_delim )
end

#weightObject

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



435
436
437
# File 'lib/ruby-hl7.rb', line 435

def weight
  self.class.weight
end