Module: Extensions::HL7::Segment::InstanceMethods

Defined in:
lib/core_ext/segment.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



13
14
15
# File 'lib/core_ext/segment.rb', line 13

def [](key)
  to_hash[key]
end

#handle_element(key) ⇒ Object



32
33
34
# File 'lib/core_ext/segment.rb', line 32

def handle_element(key)
  self.send(key)
end

#segment_nameObject



17
18
19
# File 'lib/core_ext/segment.rb', line 17

def segment_name
  self.class.to_s.split("::").last
end

#to_hashObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/core_ext/segment.rb', line 21

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

#value_for_field(key) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/core_ext/segment.rb', line 36

def value_for_field(key)
  index = key.split(".").first.to_i
  index, subindex = key.split(".").collect {|i|i.to_i}
  field = self.class.field(index)
  if field
    if subindex.blank?
      return self.send(field[0].to_s)
    else
      return self.send(field[0].to_s).split(self.item_delim)[subindex-1]
    end
  end
end