Class: ChangeHealth::Models::Model

Inherits:
Hashie::Trash
  • Object
show all
Defined in:
lib/change_health/models/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format_value(key, value) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/change_health/models/model.rb', line 59

def self.format_value(key, value)
  return nil if value == ''

  return ChangeHealth::Models::DATE_FORMATTER.call(value) if key.to_s.downcase.include?('date')
  return ChangeHealth::Models::POSTAL_CODE_FORMATTER.call(value) if key.to_s.downcase.include?('postalcode')

  value
end

.hashify(model) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/change_health/models/model.rb', line 39

def self.hashify(model)
  model.map do |key, value|
    formatted_value = case value
                      when Hash
                        hashify(model[key])
                      when Array
                        value.map do |element|
                          if element.is_a?(Hash)
                            hashify(element)
                          else # if it's an array of arrays, can't handle it
                            format_value(key, element)
                          end
                        end
                      else
                        format_value(key, value)
                      end
    [key, formatted_value]
  end.to_h
end

Instance Method Details

#as_json(_args = {}) ⇒ Object



68
69
70
# File 'lib/change_health/models/model.rb', line 68

def as_json(_args = {})
  to_h
end

#to_hObject



35
36
37
# File 'lib/change_health/models/model.rb', line 35

def to_h
  self.class.hashify(self)
end

#to_json(*_args) ⇒ Object



72
73
74
# File 'lib/change_health/models/model.rb', line 72

def to_json(*_args)
  to_h.to_json
end