Class: ActiveRecord::ModelSpaces::Registry
- Inherits:
-
Object
- Object
- ActiveRecord::ModelSpaces::Registry
- Includes:
- Util
- Defined in:
- lib/active_record/model_spaces/registry.rb
Instance Attribute Summary collapse
-
#enforce_context ⇒ Object
readonly
Returns the value of attribute enforce_context.
-
#model_spaces ⇒ Object
readonly
Returns the value of attribute model_spaces.
-
#model_spaces_by_models ⇒ Object
readonly
Returns the value of attribute model_spaces_by_models.
Instance Method Summary collapse
-
#active_key(model_space_name) ⇒ Object
return the key of the active context for the given model_space, or nil if there is no active context.
- #base_table_name(model) ⇒ Object
- #current_table_name(model) ⇒ Object
- #hoover(model) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #kill_context(model_space_name, model_space_key) ⇒ Object
-
#new_version(model, &block) ⇒ Object
create a new version of the model.
- #register_model(model, model_space_name, opts = {}) ⇒ Object
-
#reset! ⇒ Object
drop all model_space and model registrations.
- #set_base_table_name(model, table_name) ⇒ Object
- #set_enforce_context(v) ⇒ Object
- #table_name(model) ⇒ Object
-
#updated_version(model, &block) ⇒ Object
create an updated version of the model.
-
#with_context(model_space_name, model_space_key, &block) ⇒ Object
execute a block with a ModelSpace context.
- #working_table_name(model) ⇒ Object
Methods included from Util
all_model_superclasses, class_for_classname, class_from_classname, is_active_record_model?, model_from_name, name_from_model, require_for_classname
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
14 15 16 |
# File 'lib/active_record/model_spaces/registry.rb', line 14 def initialize reset! end |
Instance Attribute Details
#enforce_context ⇒ Object (readonly)
Returns the value of attribute enforce_context.
12 13 14 |
# File 'lib/active_record/model_spaces/registry.rb', line 12 def enforce_context @enforce_context end |
#model_spaces ⇒ Object (readonly)
Returns the value of attribute model_spaces.
10 11 12 |
# File 'lib/active_record/model_spaces/registry.rb', line 10 def model_spaces @model_spaces end |
#model_spaces_by_models ⇒ Object (readonly)
Returns the value of attribute model_spaces_by_models.
11 12 13 |
# File 'lib/active_record/model_spaces/registry.rb', line 11 def model_spaces_by_models @model_spaces_by_models end |
Instance Method Details
#active_key(model_space_name) ⇒ Object
return the key of the active context for the given model_space, or nil if there is no active context
121 122 123 124 125 126 127 128 |
# 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) ⇒ Object
39 40 41 |
# 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) ⇒ Object
56 57 58 |
# 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) ⇒ Object
74 75 76 |
# 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) ⇒ Object
115 116 117 |
# 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) ⇒ Object
create a new version of the model
65 66 67 |
# 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 = {}) ⇒ Object
25 26 27 28 29 30 31 |
# 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! ⇒ Object
drop all model_space and model registrations. will cause any with_model_space_context to most likely bork horribly
20 21 22 23 |
# 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) ⇒ Object
33 34 35 36 37 |
# 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) ⇒ Object
43 44 45 |
# File 'lib/active_record/model_spaces/registry.rb', line 43 def set_enforce_context(v) @enforce_context = !!v end |
#table_name(model) ⇒ Object
47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
create an updated version of the model
70 71 72 |
# 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) ⇒ Object
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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
60 61 62 |
# 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 |