Class: Flyweight
- Inherits:
-
Object
- Object
- Flyweight
- Extended by:
- Forwardable
- Defined in:
- lib/exegesis/flyweight.rb
Overview
class Flyweight
Responsibilities:
Provide a registry for an arbitrary number of instances which need to be
accessed as a single instance.
Notes:
Collaborators:
SourceFile
Directory
Project
Defined Under Namespace
Classes: AlreadyRegisteredError, NoEntryError
Instance Method Summary collapse
-
#[](key_or_instance) ⇒ Object, NilClass
Access the entry under the given key or instance.
-
#clear! ⇒ Object
(also: #reset!)
Clear the Flyweight of all entries.
-
#has_key?(key_or_instance) ⇒ Boolean
Whether the flyweight has the given key or instance registered.
-
#initialize(&key_processor) ⇒ Flyweight
constructor
Create an empty Flyweight with the given key-processing proc.
- #inspect ⇒ Object
-
#register(instance) ⇒ Object
Register an instance in the flyweight.
-
#register!(instance) ⇒ Object
Register an instance in the flyweight.
-
#unregister(key_or_instance) ⇒ Object
Remove an instance from the flyweight.
-
#unregister!(key_or_instance) ⇒ Object
Remove an instance (by key or instance proper) from the flyweight.
Constructor Details
#initialize(&key_processor) ⇒ Flyweight
Create an empty Flyweight with the given key-processing proc.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/exegesis/flyweight.rb', line 19 def initialize(&key_processor) clear! if block_given? @key_processor = key_processor else @key_processor = proc { |id| id } end self end |
Instance Method Details
#[](key_or_instance) ⇒ Object, NilClass
Access the entry under the given key or instance
NB. If, given an instance that would generate a matching key to an already registered instance, but perhaps with different data, you’ll get back a reference to the registered instance.
91 92 93 |
# File 'lib/exegesis/flyweight.rb', line 91 def [](key_or_instance) proxy_across_keytypes(:[], key_or_instance) end |
#clear! ⇒ Object Also known as: reset!
Clear the Flyweight of all entries.
96 97 98 99 |
# File 'lib/exegesis/flyweight.rb', line 96 def clear! @key_registry = {} self end |
#has_key?(key_or_instance) ⇒ Boolean
Whether the flyweight has the given key or instance registered
79 80 81 |
# File 'lib/exegesis/flyweight.rb', line 79 def has_key?(key_or_instance) proxy_across_keytypes(:has_key?, key_or_instance) end |
#inspect ⇒ Object
102 103 104 |
# File 'lib/exegesis/flyweight.rb', line 102 def inspect "Flyweight<#{object_id}, items=#{@key_registry.keys.count}>" end |
#register(instance) ⇒ Object
Register an instance in the flyweight.
46 47 48 49 |
# File 'lib/exegesis/flyweight.rb', line 46 def register(instance) key = build_key(instance) key_registry[key] = instance end |
#register!(instance) ⇒ Object
Register an instance in the flyweight. Throw an error if the key is already used.
37 38 39 40 |
# File 'lib/exegesis/flyweight.rb', line 37 def register!(instance) raise AlreadyRegisteredError if has_key?(instance) register(instance) end |
#unregister(key_or_instance) ⇒ Object
Remove an instance from the flyweight
69 70 71 |
# File 'lib/exegesis/flyweight.rb', line 69 def unregister(key_or_instance) proxy_across_keytypes(:delete, key_or_instance) end |
#unregister!(key_or_instance) ⇒ Object
Remove an instance (by key or instance proper) from the flyweight. Throw an error if no such instance exists
59 60 61 62 |
# File 'lib/exegesis/flyweight.rb', line 59 def unregister!(key_or_instance) raise NoEntryError unless has_key?(key_or_instance) unregister(key_or_instance) end |