Class: LIS::Message::Base

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/lis/messages.rb

Direct Known Subclasses

Comment, Header, Order, Patient, Query, Result, Terminator

Constant Summary

Constants included from ClassMethods

ClassMethods::CONVERSION_WRITER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

default_fields, field_count, get_field_attributes, get_index_for, has_field, set_field_attributes, set_index_for

Class Method Details

.from_string(message) ⇒ Object

instantiate a new Message from a string

Message.from_string("5L|1|N") #=> <LIS::Message::Terminator>

register subclasses by using type_id



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/lis/messages.rb', line 133

def self.from_string(message)
  type, data = parse(message)
  klass = class_for_type(type)

  obj = klass.allocate

  data = data.to_enum(:each_with_index).inject({}) do |h,(elem,idx)|
    h[idx+2] = elem; h
  end

  obj.instance_variable_set("@fields", data)
  obj
end

Instance Method Details

#to_messageObject

serialize a Message object into a String

message = Message.from_string("5L|1|N") #=> <LIS::Message>
message.to_message #=> "5L|1|N"


121
122
123
124
125
# File 'lib/lis/messages.rb', line 121

def to_message
  @fields ||= {}
  arr = Array.new(self.class.default_fields)
  type_id + @fields.inject(arr) { |a,(k,v)| a[k-1] = v; a }.join("|")
end

#type_idObject



112
113
114
# File 'lib/lis/messages.rb', line 112

def type_id
 self.class.type_id
end