class ActiveRecord::ModelSpaces::Registry
Constants
- CONTEXT_STACK_KEY
- MERGED_CONTEXT_KEY
Attributes
enforce_context[R]
model_spaces[R]
model_spaces_by_models[R]
Public Class Methods
new()
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 14 def initialize reset! end
Public Instance Methods
active_key(model_space_name)
click to toggle source
return the key of the active context for the given model_space, or nil if there is no active context
# File lib/active_record/model_spaces/registry.rb, line 121 def active_key(model_space_name) ms = get_model_space(model_space_name) raise "no such model space: #{model_space_name}" if !ms ctx = merged_context[ms.name] ctx.model_space_key if ctx end
base_table_name(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 39 def base_table_name(model) get_model_space_for_model(model).base_table_name(model) end
current_table_name(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 56 def current_table_name(model) get_context_for_model(model).current_table_name(model) end
hoover(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 74 def hoover(model) get_context_for_model(model).hoover end
kill_context(model_space_name, model_space_key)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 115 def kill_context(model_space_name, model_space_key) get_model_space(model_space_name).kill_context(model_space_key) end
new_version(model, &block)
click to toggle source
create a new version of the model
# File lib/active_record/model_spaces/registry.rb, line 65 def new_version(model, &block) get_context_for_model(model).new_version(model, &block) end
register_model(model, model_space_name, opts={})
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 25 def register_model(model, model_space_name, opts={}) old_ms = unchecked_get_model_space_for_model(model) old_ms.deregister_model(model) if old_ms new_ms = register_model_space(model_space_name).register_model(model, opts) register_model_space_for_model(model, new_ms) end
reset!()
click to toggle source
drop all model_space and model registrations. will cause any with_model_space_context to most likely bork horribly
# File lib/active_record/model_spaces/registry.rb, line 20 def reset! @model_spaces = {} @model_spaces_by_models = {} end
set_base_table_name(model, table_name)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 33 def set_base_table_name(model, table_name) ms = unchecked_get_model_space_for_model(model) raise "model #{model} is not (yet) registered to a ModelSpace. do in_model_space before set_table_name or use the :base_table_name option of in_model_space" if !ms ms.set_base_table_name(model, table_name) end
set_enforce_context(v)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 43 def set_enforce_context(v) @enforce_context = !!v end
table_name(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 47 def table_name(model) ctx = enforce_context ? get_context_for_model(model) : unchecked_get_context_for_model(model) if ctx ctx.table_name(model) else get_model_space_for_model(model).base_table_name(model) end end
updated_version(model, &block)
click to toggle source
create an updated version of the model
# File lib/active_record/model_spaces/registry.rb, line 70 def updated_version(model, &block) get_context_for_model(model).updated_version(model, &block) end
with_context(model_space_name, model_space_key, &block)
click to toggle source
execute a block with a ModelSpace
context. only a single context can be active for a given ModelSpace
on any Thread at any time, though different ModelSpaces
may have active contexts concurrently
# File lib/active_record/model_spaces/registry.rb, line 81 def with_context(model_space_name, model_space_key, &block) ms = get_model_space(model_space_name) raise "no such model space: #{model_space_name}" if !ms current_ctx = merged_context[ms.name] if current_ctx && current_ctx.model_space_key==model_space_key.to_sym block.call # same context is already active elsif current_ctx raise "ModelSpace: #{model_space_name}: context with key #{current_ctx.model_space_key} already active" else old_merged_context = self.send(:merged_context) ctx = ms.create_context(model_space_key) context_stack << ctx begin self.merged_context = merge_context_stack r = block.call ctx.commit r ensure context_stack.pop self.merged_context = old_merged_context end end end
working_table_name(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 60 def working_table_name(model) get_context_for_model(model).working_table_name(model) end
Private Instance Methods
context_stack()
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 159 def context_stack Thread.current[CONTEXT_STACK_KEY] ||= [] end
get_context_for_model(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 187 def get_context_for_model(model) ms = get_model_space_for_model(model) ctx = merged_context[ms.name] raise "ModelSpace: '#{ms.name}' has no current context" if !ctx ctx end
get_model_space(model_space_name)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 136 def get_model_space(model_space_name) model_spaces[model_space_name.to_sym] end
get_model_space_for_model(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 151 def get_model_space_for_model(model) ms = unchecked_get_model_space_for_model(model) raise "model: #{model} is not registered to any ModelSpace" if !ms ms end
merge_context_stack()
click to toggle source
merge all entries in the context stack into a map
# File lib/active_record/model_spaces/registry.rb, line 174 def merge_context_stack context_stack.reduce({}) do |m, ctx| raise "ModelSpace: #{ctx.model_space.name}: already has an active context" if m[ctx.model_space.name] m[ctx.model_space.name] = ctx m end end
merged_context()
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 165 def merged_context Thread.current[MERGED_CONTEXT_KEY] || {} end
merged_context=(mc)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 169 def merged_context=(mc) Thread.current[MERGED_CONTEXT_KEY] = mc end
register_model_space(model_space_name)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 132 def register_model_space(model_space_name) model_spaces[model_space_name.to_sym] ||= ModelSpace.new(model_space_name.to_sym) end
register_model_space_for_model(model, model_space)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 140 def register_model_space_for_model(model, model_space) model_spaces_by_models[name_from_model(model)] = model_space end
unchecked_get_context_for_model(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 182 def unchecked_get_context_for_model(model) ms = unchecked_get_model_space_for_model(model) merged_context[ms.name] if ms end
unchecked_get_model_space_for_model(model)
click to toggle source
# File lib/active_record/model_spaces/registry.rb, line 144 def unchecked_get_model_space_for_model(model) mc = all_model_superclasses(model).find do |klass| model_spaces_by_models[name_from_model(klass)] end model_spaces_by_models[name_from_model(mc)] if mc end