Module: ArtirixDataModels::DAO

Extended by:
ActiveSupport::Concern
Defined in:
lib/artirix_data_models/dao.rb

Defined Under Namespace

Modules: FakeModes

Constant Summary collapse

DELEGATED_METHODS =
[
  :partial_mode_fields,

  :model_name,
  :model_class,

  :paths_factory,
  :fake_mode_factory,

  :gateway,

  :force_fake_enabled,
  :force_fake_disabled,
  :remove_force_fake,
  :fake?,
  :forced_fake_enabled?,
  :forced_fake_disabled?,

  :empty_collection,
  :empty_collection_for,

  :perform_get,
  :perform_post,
  :perform_put,
  :perform_delete,

  :dao_registry,
  :dao_registry=,
  :set_dao_registry,
  :dao_registry_loader,
  :dao_registry_loader=,
  :set_dao_registry_loader,
  :set_default_dao_registry_loader,
  :set_default_dao_registry,
]

Instance Method Summary collapse

Instance Method Details

#create_basic_model_dao(dao_registry:, dao_registry_loader:, fake_mode_factory:, gateway:, gateway_factory:, ignore_default_gateway:, model_class:, model_name:, paths_factory:, **other_basic_model_dao_params) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/artirix_data_models/dao.rb', line 78

def create_basic_model_dao(dao_registry:,
                           dao_registry_loader:,
                           fake_mode_factory:,
                           gateway:,
                           gateway_factory:,
                           ignore_default_gateway:,
                           model_class:,
                           model_name:,
                           paths_factory:,
                           **other_basic_model_dao_params)

  dr = ArtirixDataModels::WithDAORegistry.loader_or_registry_or_default dao_registry_loader: dao_registry_loader,
                                                                        dao_registry: dao_registry

  basic_model_class = dr.get(:basic_class)

  @basic_model_dao = basic_model_class.new model_name: model_name,
                                           model_class: model_class,
                                           fake_mode_factory: fake_mode_factory,
                                           paths_factory: paths_factory,
                                           gateway: gateway,
                                           gateway_factory: gateway_factory,
                                           dao_registry: dao_registry,
                                           dao_registry_loader: dao_registry_loader,
                                           ignore_default_gateway: ignore_default_gateway,
                                           **other_basic_model_dao_params
end

#default_fake_mode_factoryObject



118
119
120
121
122
123
124
# File 'lib/artirix_data_models/dao.rb', line 118

def default_fake_mode_factory
  if defined?(self.class::FakeMode)
    self.class::FakeMode
  else
    FakeModes::Disabled
  end
end

#default_model_classObject



110
111
112
# File 'lib/artirix_data_models/dao.rb', line 110

def default_model_class
  defined?(self.class::MODEL_CLASS) ? self.class::MODEL_CLASS : nil
end

#default_model_nameObject



106
107
108
# File 'lib/artirix_data_models/dao.rb', line 106

def default_model_name
  defined?(self.class::MODEL_NAME) ? self.class::MODEL_NAME : nil
end

#default_path_factoryObject



114
115
116
# File 'lib/artirix_data_models/dao.rb', line 114

def default_path_factory
  self.class::Paths
end

#find(model_pk, cache_adaptor: nil, **extra_options) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'lib/artirix_data_models/dao.rb', line 166

def find(model_pk, cache_adaptor: nil, **extra_options)
  model = dao_registry.get_model model_name, model_pk
  return model if model.present?

  cache_adaptor ||= cache_model_adaptor_for_find(model_pk, **extra_options)

  model = basic_model_dao.find(model_pk, cache_adaptor: cache_adaptor, **extra_options)

  complete_model model
end

#get(model_pk, cache_adaptor: nil, **extra_options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/artirix_data_models/dao.rb', line 155

def get(model_pk, cache_adaptor: nil, **extra_options)
  model = dao_registry.get_model model_name, model_pk
  return model if model.present?

  cache_adaptor ||= cache_model_adaptor_for_get(model_pk, **extra_options)

  model = basic_model_dao.get(model_pk, cache_adaptor: cache_adaptor, **extra_options)

  complete_model model
end

#get_full(model_pk, model_to_reload: nil, cache_adaptor: nil, **extra_options) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/artirix_data_models/dao.rb', line 145

def get_full(model_pk, model_to_reload: nil, cache_adaptor: nil, **extra_options)
  # we do not check in the registry, since it could be a reload
  model_to_reload ||= new_model_with_pk(model_pk)
  cache_adaptor ||= cache_model_adaptor_for_get_full(model_pk, model_to_reload: model_to_reload, **extra_options)

  model = basic_model_dao.get_full(model_pk, model_to_reload: model_to_reload, cache_adaptor: cache_adaptor, **extra_options)

  complete_model model
end

#get_some(model_pks, cache_adaptor: nil, **extra_options) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/artirix_data_models/dao.rb', line 177

def get_some(model_pks, cache_adaptor: nil, **extra_options)
  registered = Array(model_pks).map do |model_pk|
    [
      model_pk,
      dao_registry.get_model(model_name, model_pk)
    ]
  end.to_h

  return registered.values if registered.values.all?(&:present?)

  # load only the missing
  missing_pks = registered.select { |_k, v| v.blank? }.keys

  cache_adaptor ||= cache_model_adaptor_for_get_some(missing_pks, **extra_options)

  models = basic_model_dao.get_some(missing_pks, cache_adaptor: cache_adaptor, **extra_options)

  Array(models).each do |model|
    complete_model model
  end

  list = registered.values.compact.concat(models)

  # reorder with the same order of the model_pks
  # (if for any reason it does not have a primary key or if it is not in the list, then use -1 so they are at the start)
  list.sort_by { |m| pk = m.try :primary_key; model_pks.index(pk) || -1 }
end

#in_fake_modeObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/artirix_data_models/dao.rb', line 126

def in_fake_mode
  return unless block_given?

  begin
    basic_model_dao.force_fake_enabled
    yield
  ensure
    basic_model_dao.remove_force_fake
  end
end

#initialize(dao_registry: nil, dao_registry_loader: nil, gateway: nil, gateway_factory: nil, ignore_default_gateway: false, model_name: nil, model_class: nil, paths_factory: nil, fake_mode_factory: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/artirix_data_models/dao.rb', line 50

def initialize(dao_registry: nil,
               dao_registry_loader: nil,
               gateway: nil,
               gateway_factory: nil,
               ignore_default_gateway: false,
               model_name: nil,
               model_class: nil,
               paths_factory: nil,
               fake_mode_factory: nil)

  model_name ||= default_model_name
  model_class ||= default_model_class
  paths_factory ||= default_path_factory
  fake_mode_factory ||= default_fake_mode_factory

  # temporary dao registry to load basic_class only
  create_basic_model_dao dao_registry: dao_registry,
                         dao_registry_loader: dao_registry_loader,
                         fake_mode_factory: fake_mode_factory,
                         gateway: gateway,
                         gateway_factory: gateway_factory,
                         ignore_default_gateway: ignore_default_gateway,
                         model_class: model_class,
                         model_name: model_name,
                         paths_factory: paths_factory

end

#reload(model) ⇒ Object

DELEGATE TO BASIC_MODEL_DAO #



141
142
143
# File 'lib/artirix_data_models/dao.rb', line 141

def reload(model)
  get_full model.primary_key, model_to_reload: model
end