Class: Redox::Models::AbstractModel

Inherits:
Hashie::Trash
  • Object
show all
Includes:
Hashie::Extensions::IgnoreUndeclared, Hashie::Extensions::IndifferentAccess
Defined in:
lib/redox/models/model.rb

Constant Summary collapse

HIGH_LEVEL_KEYS =
%w[Meta Patient Visit PotentialMatches].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_helpers(model, data) ⇒ Object

rubocop:disable Metrics/AbcSize



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/redox/models/model.rb', line 78

def self.add_helpers(model, data)
  model_class = get_inflected_class(model.meta&.data_model)

  data.each_key do |key|
    next if HIGH_LEVEL_KEYS.include?(key.to_s)

    helper_name = key.to_s.downcase.to_sym

    if model_class.nil?
      model.define_singleton_method(helper_name) { data[key] }
    elsif data[key].is_a?(Array)
      model.define_singleton_method(helper_name) { data[key].map { |obj| model_class.new(obj) } }
    else
      model.define_singleton_method(helper_name) { model_class.new(data[key]) }
    end
  end

  model
end

.from_response(response) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/redox/models/model.rb', line 39

def self.from_response(response)
  model = Model.new
  model.response = response

  # rubocop:disable Lint/SuppressedException
  HIGH_LEVEL_KEYS.each do |k|
    model.send("#{k}=", Module.const_get("Redox::Models::#{k}").new(response[k])) if response[k]
  rescue StandardError
  end
  # rubocop:enable Lint/SuppressedException

  model
end

.from_response_inflected(response) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/redox/models/model.rb', line 53

def self.from_response_inflected(response)
  model = from_response(response)

  return model unless model.response.ok?

  data = model.response.parsed_response

  return model unless data.respond_to?(:keys)

  add_helpers(model, data)
end

.get_inflected_class(data_model) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redox/models/model.rb', line 65

def self.get_inflected_class(data_model)
  return if data_model.nil?

  model_class = "Redox::Models::#{data_model}"

  begin
    Object.const_get(model_class)
  rescue NameError
    nil
  end
end

Instance Method Details

#insurancesObject



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

def insurances
  (patient&.insurances || []) + (visit&.insurances || [])
end

#to_json(_args = {}) ⇒ Object



31
32
33
# File 'lib/redox/models/model.rb', line 31

def to_json(_args = {})
  to_h.to_json
end