Class: PureMVC::Model

Inherits:
Object
  • Object
show all
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.

See Also:

Constant Summary collapse

MULTITON_MSG =

Message Constants

'Model instance for this Multiton key already constructed!'

Class Method Summary collapse

Instance Method Summary collapse

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) }.

Parameters:

  • key (String)

Raises:

  • (RuntimeError)

    Error if an instance for this Multiton key has already been constructed.



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.

Parameters:

  • key (String)

    the unique key identifying the Multiton instance

  • factory (^(String) -)

    IModel] the unique key passed to the factory block

Returns:

  • (IModel)

    the instance for this Multiton key



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_mapHash{String => IModel}

The Multiton IModel instanceMap.

Returns:

  • (Hash{String => IModel})


34
# File 'src/core/model.rb', line 34

def instance_map = (@instance_map ||= {})

.mutexMutex

Mutex used to synchronize access to the instance map for thread safety.

Returns:

  • (Mutex)


38
# File 'src/core/model.rb', line 38

def mutex = (@mutex ||= Mutex.new)

.remove_model(key) ⇒ Object

Remove an IModel instance

Parameters:

  • key (String)

    the multiton key of the IModel instance to remove



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.

Parameters:

  • proxy_name (String)

    the name of the proxy to check.

Returns:

  • (Boolean)

    whether a Proxy is currently registered with the given proxy_name.



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_modelObject

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.

Parameters:

  • proxy (_IProxy)

    an IProxy to be held by 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.

Parameters:

  • proxy_name (String)

    name of the IProxy instance to be removed.

Returns:

  • (_IProxy, nil)

    the IProxy that was removed from the Model, or nil if none found.



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.

Parameters:

  • proxy_name (String)

    the name of the proxy to retrieve.

Returns:

  • (_IProxy, nil)

    the IProxy instance previously registered with the proxy_name,



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