Module: HashConverters

Includes:
XMLConverters
Included in:
KalibroGem::Entities::Model
Defined in:
lib/kalibro_gem/helpers/hash_converters.rb

Instance Method Summary collapse

Methods included from XMLConverters

#get_xml, #xml_instance_class_name

Instance Method Details

#convert_to_hash(value) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/kalibro_gem/helpers/hash_converters.rb', line 29

def convert_to_hash(value)
  return value if value.nil?
  return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array)
  return value.to_hash if value.is_a?(KalibroGem::Entities::Model)
  return date_with_milliseconds(value) if value.is_a?(DateTime)
  return 'INF' if value.is_a?(Float) and value.infinite? == 1
  return '-INF' if value.is_a?(Float) and value.infinite? == -1
  value.to_s
end

#date_with_milliseconds(date) ⇒ Object

FIXME: we can think about a better name. This method actually receives an DateTime and converts it to string



24
25
26
27
# File 'lib/kalibro_gem/helpers/hash_converters.rb', line 24

def date_with_milliseconds(date)
  milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s
  date.to_s[0..18] + milliseconds + date.to_s[19..-1]
end

#field_to_hash(field) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/kalibro_gem/helpers/hash_converters.rb', line 39

def field_to_hash(field)
  hash = Hash.new
  field_value = send(field)
  if !field_value.nil?
    hash[field] = convert_to_hash(field_value)
    hash = get_xml(field, field_value).merge(hash)
  end
  hash
end