Class: ArtirixDataModels::DAORegistry::IdentityMap

Inherits:
Object
  • Object
show all
Includes:
WithDAORegistry
Defined in:
lib/artirix_data_models/dao_registry.rb

Constant Summary

Constants included from WithDAORegistry

WithDAORegistry::DEFAULT_DAO_REGISTRY_LOADER

Instance Attribute Summary

Attributes included from WithDAORegistry

#dao_registry_loader

Instance Method Summary collapse

Methods included from WithDAORegistry

#dao_registry, #dao_registry=, #default_dao_registry, loader_or_registry_or_default, #set_dao_registry, #set_dao_registry_and_loader, #set_dao_registry_loader, #set_default_dao_registry_loader

Constructor Details

#initialize(dao_registry_loader: nil, dao_registry: nil) ⇒ IdentityMap

Returns a new instance of IdentityMap.



168
169
170
171
# File 'lib/artirix_data_models/dao_registry.rb', line 168

def initialize(dao_registry_loader: nil, dao_registry: nil)
  set_dao_registry_and_loader dao_registry_loader, dao_registry
  @identity_map = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



183
184
185
# File 'lib/artirix_data_models/dao_registry.rb', line 183

def method_missing(method_name, *arguments, &block)
  dao_registry.send method_name, *arguments, &block
end

Instance Method Details

#get(key, override_dao_registry: nil) ⇒ Object



187
188
189
190
# File 'lib/artirix_data_models/dao_registry.rb', line 187

def get(key, override_dao_registry: nil)
  override_dao_registry ||= self
  dao_registry.get key, override_dao_registry: override_dao_registry
end

#get_model(model_dao_name, primary_key) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/artirix_data_models/dao_registry.rb', line 220

def get_model(model_dao_name, primary_key)
  model_dao_name = model_dao_name.to_s
  primary_key = primary_key.to_s
  return nil unless model_dao_name.present? && primary_key.present?

  @identity_map[model_dao_name] ||= {}
  val = @identity_map[model_dao_name][primary_key]

  if val.nil?
    log "get model #{model_dao_name}:#{primary_key} NOT PRESENT"
  else
    log "get model #{model_dao_name}:#{primary_key} PRESENT!!!!!"
  end

  val
end

#register_model(model) ⇒ Object

IDENTITY MAP FOR MODELS #



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/artirix_data_models/dao_registry.rb', line 196

def register_model(model)
  model_dao_name, primary_key = keys_from_model(model, action: :register)
  return self unless model_dao_name.present? && primary_key.present?

  log "register model #{model_dao_name}:#{primary_key}"

  @identity_map[model_dao_name] ||= {}
  @identity_map[model_dao_name][primary_key] = model

  self
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/artirix_data_models/dao_registry.rb', line 179

def respond_to_missing?(method_name, include_private = false)
  dao_registry.respond_to? method_name, include_private
end

#unload_model(model) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/artirix_data_models/dao_registry.rb', line 208

def unload_model(model)
  model_dao_name, primary_key = keys_from_model(model, action: :unload)
  return self unless model_dao_name.present? && primary_key.present?

  log "unload model #{model_dao_name}:#{primary_key}"

  @identity_map[model_dao_name] ||= {}
  @identity_map[model_dao_name].delete primary_key

  self
end