Class: Exegesis::Flyweight

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/exegesis/flyweight.rb

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.



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.

Parameters:

  • key_or_instance (Object)

    The key or instance to access.

Returns:

  • (Object, NilClass)

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



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

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



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

#inspectObject



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.

Parameters:

  • instance (Object)

    the instance to register in the flyweight

Returns:

  • (Object)

    the instance given



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.

Parameters:

  • instance (Object)

    the instance to register in the flyweight

Returns:

  • (Object)

    the instance given

Raises:



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

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



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

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



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