Class: Flyweight

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

Constructor Details

#initialize(&key_processor) ⇒ Flyweight

Create an empty Flyweight with the given key-processing proc.

Parameters:

  • key_processor (Proc)

    a proc which turns an instance into it’s key.



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.

Parameters:

  • key_or_instance (Object)

    The key or instance to access.

Returns:

  • (Object, NilClass)

    the instance desired, or nil if it doesn’t exist



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

Parameters:

  • key_or_instance (Object)

    Either the key under which an instance is registered, or the instance itself.

Returns:

  • (Boolean)

    True if the Flyweight has the key or instance, false otherwise



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

#inspectObject



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.

Parameters:

  • instance (Object)

    the instance to register in the flyweight

Returns:

  • (Object)

    the instance given



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.

Parameters:

  • instance (Object)

    the instance to register in the flyweight

Returns:

  • (Object)

    the instance given

Raises:



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

Parameters:

  • key_or_instance (Object)

    Either the key under which an instance is registered, or the instance itself.

Returns:

  • (Object)

    the instance deleted 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

Parameters:

  • key_or_instance (Object)

    Either the key under which an instance is registered, or the instance itself.

Returns:

  • (Object)

    the instance deleted from the flyweight

Raises:

  • (NoFlyweightEntryError)

    when trying to delete a key that isn’t present in the flyweight



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