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

#default_fake_mode_factoryObject



91
92
93
94
95
96
97
# File 'lib/artirix_data_models/dao.rb', line 91

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

#default_model_classObject



83
84
85
# File 'lib/artirix_data_models/dao.rb', line 83

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

#default_model_nameObject



79
80
81
# File 'lib/artirix_data_models/dao.rb', line 79

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

#default_path_factoryObject



87
88
89
# File 'lib/artirix_data_models/dao.rb', line 87

def default_path_factory
  self.class::Paths
end

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



139
140
141
142
143
144
145
146
147
148
# File 'lib/artirix_data_models/dao.rb', line 139

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



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

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



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

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/artirix_data_models/dao.rb', line 150

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



99
100
101
102
103
104
105
106
107
108
# File 'lib/artirix_data_models/dao.rb', line 99

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
77
# 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
  dr                = ArtirixDataModels::WithDAORegistry.loader_or_registry_or_default(dao_registry_loader: dao_registry_loader, dao_registry: dao_registry)
  @basic_model_dao  = dr.get(:basic_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

end

#reload(model) ⇒ Object

DELEGATE TO BASIC_MODEL_DAO #



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

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