Sha256: fc0d4caf603585d5b560d7c96f5c4187beb54028f1f68adb9a1ca64aab7e619c

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Extensions
  module HL7
    module Segment
      attr_accessor :hash
      
      def self.included base
        base.send :include, InstanceMethods
        base.extend ClassMethods
      end
      
      module InstanceMethods
        
        def segment_name
          self.class.to_s.split("::").last
        end
  
        def to_hash
          @hash ||= {}
          fields = self.class.fields
          if fields.is_a?Hash
            self.class.fields.keys.each do |key|
              @hash[key.to_s.camelize(:lower)] = self.handle_element(key)
            end
          end
          @hash
        end
  
        def handle_element(key)
          self.send(key)
        end
      end
      
      module ClassMethods
        def from_hash(type, hash)
          clazz = eval("::HL7::Message::Segment::#{type}")
          instance = clazz.new
          instance.hash = hash

          fields = clazz.fields
          fields.keys.each do |field|
            instance.send("#{field}=",hash[field.to_s.camelize(:lower)])
          end  
          instance
        end
      end
            
    end
  end
end

class HL7::Message::Segment
  include Extensions::HL7::Segment
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-hl7-extensions-0.0.4 lib/core_ext/segment.rb
ruby-hl7-extensions-0.0.3 lib/core_ext/segment.rb
ruby-hl7-extensions-0.0.1 lib/core_ext/segment.rb