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

#format_date(attr, format = "%B %d, %Y") ⇒ Object



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

def format_date(attr, format = "%B %d, %Y")
  date = self.send(attr)
  if date.blank?
    return nil
  else
    Date.parse(date).strftime(format)
  end
end

#format_datetime(attr, format = "%m/%d/%Y %k:%M") ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/core_ext/segment.rb', line 30

def format_datetime(attr, format = "%m/%d/%Y %k:%M")
  datetime = self.send(attr)
  if datetime.blank?
    return nil
  else 
    DateTime.parse(datetime).strftime(format)
  end
end

#handle_element(key) ⇒ Object



50
51
52
# File 'lib/core_ext/segment.rb', line 50

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

#provider_hash(provider_type, provider_code) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/core_ext/segment.rb', line 54

def provider_hash(provider_type, provider_code)
  if to_hash["#{provider_type}Provider"] and !to_hash["#{provider_type}Provider"].empty?
    to_hash["#{provider_type}Provider"].merge("providerType" => provider_code)
  else
    {}
  end
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



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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/core_ext/segment.rb', line 62

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