Class: LIS::Message::Base
- Inherits:
-
Object
- Object
- LIS::Message::Base
- Extended by:
- ClassMethods
- Defined in:
- lib/lis/messages.rb
Constant Summary
Constants included from ClassMethods
ClassMethods::CONVERSION_WRITER
Class Method Summary collapse
-
.from_string(message) ⇒ Object
instantiate a new Message from a string.
Instance Method Summary collapse
-
#to_message ⇒ Object
serialize a Message object into a String.
- #type_id ⇒ Object
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() type, data = parse() 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_message ⇒ Object
serialize a Message object into a String
= Message.from_string("5L|1|N") #=> <LIS::Message>
. #=> "5L|1|N"
121 122 123 124 125 |
# File 'lib/lis/messages.rb', line 121 def @fields ||= {} arr = Array.new(self.class.default_fields) type_id + @fields.inject(arr) { |a,(k,v)| a[k-1] = v; a }.join("|") end |
#type_id ⇒ Object
112 113 114 |
# File 'lib/lis/messages.rb', line 112 def type_id self.class.type_id end |