Class: HL7::Message::Segment
- Inherits:
-
Object
- Object
- HL7::Message::Segment
- 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, ERR, EVN, MSA, MSH, NK1, NTE, OBR, OBX, ORC, ORU, PID, PV1, PV2, QRD, QRF, SFT, SPM
Defined Under Namespace
Classes: Default, ERR, EVN, MSA, MSH, NK1, NTE, OBR, OBX, ORC, ORU, PID, PV1, PV2, QRD, QRF, SFT, SPM
Instance Attribute Summary collapse
-
#element_delim ⇒ Object
readonly
Returns the value of attribute element_delim.
-
#item_delim ⇒ Object
readonly
Returns the value of attribute item_delim.
-
#segment_parent ⇒ Object
Returns the value of attribute segment_parent.
-
#segment_weight ⇒ Object
readonly
Returns the value of attribute segment_weight.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
sort-compare two Segments, 0 indicates equality.
- #initialize(raw_segment = "", delims = [], &blk) ⇒ Segment constructor
-
#is_child_segment=(val) ⇒ Object
indicate whether or not the segment has a parent.
-
#is_child_segment? ⇒ Boolean
return true if the segment has a parent.
-
#length ⇒ Object
get the length of the segment (number of fields it contains).
-
#method_missing(sym, *args, &blk) ⇒ Object
handle the e<number> field accessor and any aliases that didn’t get added to the system automatically.
- #to_info ⇒ Object
-
#to_s ⇒ Object
(also: #to_hl7)
output the HL7 spec version of the segment.
-
#weight ⇒ Object
get the defined sort-weight of this segment class an alias for self.weight.
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
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/ruby-hl7.rb', line 373 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 = " def method_missing( sym, *args, &blk )\n __seg__.send( sym, args, blk )\n end\n END\n\n eval( to_do, blk.binding )\n yield self\n eval( \"undef method_missing\", blk.binding )\n end\nend\n" |
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
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/ruby-hl7.rb', line 433 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_delim ⇒ Object (readonly)
Returns the value of attribute element_delim.
363 364 365 |
# File 'lib/ruby-hl7.rb', line 363 def element_delim @element_delim end |
#item_delim ⇒ Object (readonly)
Returns the value of attribute item_delim.
364 365 366 |
# File 'lib/ruby-hl7.rb', line 364 def item_delim @item_delim end |
#segment_parent ⇒ Object
Returns the value of attribute segment_parent.
362 363 364 |
# File 'lib/ruby-hl7.rb', line 362 def segment_parent @segment_parent end |
#segment_weight ⇒ Object (readonly)
Returns the value of attribute segment_weight.
365 366 367 |
# File 'lib/ruby-hl7.rb', line 365 def segment_weight @segment_weight end |
Class Method Details
.add_child_type(child_type) ⇒ Object
411 412 413 414 415 416 417 |
# File 'lib/ruby-hl7.rb', line 411 def self.add_child_type(child_type) if @child_types @child_types << child_type.to_sym else has_children [ child_type.to_sym ] end end |
Instance Method Details
#<=>(other) ⇒ Object
sort-compare two Segments, 0 indicates equality
461 462 463 464 465 466 467 468 469 |
# File 'lib/ruby-hl7.rb', line 461 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
484 485 486 |
# File 'lib/ruby-hl7.rb', line 484 def is_child_segment=(val) @is_child_segment = val end |
#is_child_segment? ⇒ Boolean
return true if the segment has a parent
479 480 481 |
# File 'lib/ruby-hl7.rb', line 479 def is_child_segment? (@is_child_segment ||= false) end |
#length ⇒ Object
get the length of the segment (number of fields it contains)
489 490 491 492 |
# File 'lib/ruby-hl7.rb', line 489 def length 0 unless @elements @elements.length end |
#to_info ⇒ Object
419 420 421 |
# File 'lib/ruby-hl7.rb', line 419 def to_info "%s: empty segment >> %s" % [ self.class.to_s, @elements.inspect ] end |
#to_s ⇒ Object Also known as: to_hl7
output the HL7 spec version of the segment
424 425 426 |
# File 'lib/ruby-hl7.rb', line 424 def to_s @elements.join( @element_delim ) end |
#weight ⇒ Object
get the defined sort-weight of this segment class an alias for self.weight
473 474 475 |
# File 'lib/ruby-hl7.rb', line 473 def weight self.class.weight end |