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



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/change_health/models/model.rb', line 79

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

  return ChangeHealth::Models.date_hour_formatter(value) if key.to_s.downcase.include?('dateandhour')

  return ChangeHealth::Models.hour_formatter(value) if key.to_s.downcase.include?('hour')

  return ChangeHealth::Models.date_formatter(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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/change_health/models/model.rb', line 59

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



93
94
95
# File 'lib/change_health/models/model.rb', line 93

def as_json(_args = {})
  to_h
end

#to_hObject



55
56
57
# File 'lib/change_health/models/model.rb', line 55

def to_h
  self.class.hashify(self)
end

#to_json(*_args) ⇒ Object



97
98
99
# File 'lib/change_health/models/model.rb', line 97

def to_json(*_args)
  to_h.to_json
end