Class: PureMVC::Model
- Inherits:
-
Object
- Object
- PureMVC::Model
- Defined in:
- src/core/model.rb
Overview
A Multiton IModel
implementation.
In PureMVC, the Model
class provides access to model objects (Proxies) by named lookup.
The Model
assumes these responsibilities:
-
Maintains a cache of
IProxy
instances. -
Provides methods for registering, retrieving, and removing
IProxy
instances.
Your application must register IProxy
instances with the Model
. Typically, an ICommand
is used to create and register IProxy
instances after the <code>Facade<code> has initialized the Core actors.
Constant Summary collapse
- MULTITON_MSG =
Message Constants
'Model instance for this Multiton key already constructed!'
Class Method Summary collapse
-
.get_instance(key, &factory) ⇒ IModel
Model
Multiton Factory method. -
.instance_map ⇒ Hash{String => IModel}
The Multiton IModel instanceMap.
-
.mutex ⇒ Mutex
Mutex used to synchronize access to the instance map for thread safety.
-
.remove_model(key) ⇒ Object
Remove an IModel instance.
Instance Method Summary collapse
-
#has_proxy?(proxy_name) ⇒ Boolean
Check if a Proxy is registered.
-
#initialize(key) ⇒ Model
constructor
Constructor.
-
#initialize_model ⇒ Object
Initialize the
Model
instance. -
#register_proxy(proxy) ⇒ Object
Register an
IProxy
with theModel
. -
#remove_proxy(proxy_name) ⇒ _IProxy?
Remove an
IProxy
from theModel
. -
#retrieve_proxy(proxy_name) ⇒ _IProxy?
Retrieve an
IProxy
from theModel
.
Constructor Details
#initialize(key) ⇒ Model
Constructor.
This IModel
implementation is a Multiton, so you should not call the constructor directly, but instead call the static Multiton Factory method PureMVC::Model.get_instance(key) { |key| PureMVC::Model.new(key) }
.
69 70 71 72 73 74 75 76 77 |
# File 'src/core/model.rb', line 69 def initialize(key) raise MULTITON_MSG if self.class.instance_map[key] self.class.instance_map[key] = self @multiton_key = key @proxy_map = {} @proxy_mutex = Mutex.new initialize_model end |
Class Method Details
.get_instance(key, &factory) ⇒ IModel
Model
Multiton Factory method.
45 46 47 48 49 |
# File 'src/core/model.rb', line 45 def get_instance(key, &factory) mutex.synchronize do instance_map[key] ||= factory.call(key) end end |
.instance_map ⇒ Hash{String => IModel}
The Multiton IModel instanceMap.
34 |
# File 'src/core/model.rb', line 34 def instance_map = (@instance_map ||= {}) |
.mutex ⇒ Mutex
Mutex used to synchronize access to the instance map for thread safety.
38 |
# File 'src/core/model.rb', line 38 def mutex = (@mutex ||= Mutex.new) |
.remove_model(key) ⇒ Object
Remove an IModel instance
54 55 56 57 58 |
# File 'src/core/model.rb', line 54 def remove_model(key) mutex.synchronize do instance_map.delete(key) end end |
Instance Method Details
#has_proxy?(proxy_name) ⇒ Boolean
Check if a Proxy is registered.
113 114 115 116 117 |
# File 'src/core/model.rb', line 113 def has_proxy?(proxy_name) @proxy_mutex.synchronize do @proxy_map.key?(proxy_name) end end |
#initialize_model ⇒ Object
Initialize the Model
instance.
Called automatically by the constructor, this is your opportunity to initialize the Multiton instance in your subclass without overriding the constructor.
85 |
# File 'src/core/model.rb', line 85 def initialize_model; end |
#register_proxy(proxy) ⇒ Object
Register an IProxy
with the Model
.
90 91 92 93 94 95 96 |
# File 'src/core/model.rb', line 90 def register_proxy(proxy) proxy.initialize_notifier(@multiton_key) @proxy_mutex.synchronize do @proxy_map[proxy.name] = proxy end proxy.on_register end |
#remove_proxy(proxy_name) ⇒ _IProxy?
Remove an IProxy
from the Model
.
123 124 125 126 127 128 129 130 131 |
# File 'src/core/model.rb', line 123 def remove_proxy(proxy_name) # @type var proxy: _IProxy? proxy = nil @proxy_mutex.synchronize do proxy = @proxy_map.delete(proxy_name) end proxy&.on_remove proxy end |
#retrieve_proxy(proxy_name) ⇒ _IProxy?
Retrieve an IProxy
from the Model
.
or nil if none found.
103 104 105 106 107 |
# File 'src/core/model.rb', line 103 def retrieve_proxy(proxy_name) @proxy_mutex.synchronize do @proxy_map[proxy_name] end end |