Class: Exegesis::Flyweight
- Inherits:
-
Object
- Object
- Exegesis::Flyweight
- Extended by:
- Forwardable
- Defined in:
- lib/exegesis/flyweight.rb
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.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/exegesis/flyweight.rb', line 20 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.
92 93 94 |
# File 'lib/exegesis/flyweight.rb', line 92 def [](key_or_instance) proxy_across_keytypes(:[], key_or_instance) end |
#clear! ⇒ Object Also known as: reset!
Clear the Flyweight of all entries.
97 98 99 100 |
# File 'lib/exegesis/flyweight.rb', line 97 def clear! @key_registry = {} self end |
#has_key?(key_or_instance) ⇒ Boolean
Whether the flyweight has the given key or instance registered
80 81 82 |
# File 'lib/exegesis/flyweight.rb', line 80 def has_key?(key_or_instance) proxy_across_keytypes(:has_key?, key_or_instance) end |
#inspect ⇒ Object
103 104 105 |
# File 'lib/exegesis/flyweight.rb', line 103 def inspect "Flyweight<#{object_id}, items=#{@key_registry.keys.count}>" end |
#register(instance) ⇒ Object
Register an instance in the flyweight.
47 48 49 50 |
# File 'lib/exegesis/flyweight.rb', line 47 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.
38 39 40 41 |
# File 'lib/exegesis/flyweight.rb', line 38 def register!(instance) raise AlreadyRegisteredError if has_key?(instance) register(instance) end |
#unregister(key_or_instance) ⇒ Object
Remove an instance from the flyweight
70 71 72 |
# File 'lib/exegesis/flyweight.rb', line 70 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
60 61 62 63 |
# File 'lib/exegesis/flyweight.rb', line 60 def unregister!(key_or_instance) raise NoEntryError unless has_key?(key_or_instance) unregister(key_or_instance) end |