Module: DataMapper::OrderedSet::Cache::API Private
- Included in:
- DataMapper::OrderedSet::Cache, SubjectSet::NameCache
- Defined in:
- lib/dm-core/support/ordered_set.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
The default implementation of the API that DataMapper::OrderedSet expects from the cache object that it uses to
1. keep track of insertion order
2. enforce set semantics.
Classes including API must customize the behavior of the cache in 2 ways:
They must determine the value to use as cache key and thus set discriminator, by implementing the #key_for method. The #key_for method accepts an arbitrary object as param and the method is free to return whatever value from that method. Obviously this will most likely be some attribute or value otherwise derived from the object that got passed in.
They must determine which objects are valid set entries by overwriting the #valid? method. The #valid? method accepts an arbitrary object as param and the overwriting method must return either true or false.
The motivation behind this is that set semantics cannot always be enforced by calling #eql? and #hash on the set’s entries. For example, two entries might be considered unique wrt the set if their names are the same, but other internal state differs. This is exactly the case for Property and Associations::Relationship objects.
Instance Method Summary collapse
-
#[](entry) ⇒ Integer?
private
Return the index for the entry in the cache.
-
#[]=(entry, index) ⇒ Integer
private
Set the index for the entry in the cache.
-
#clear ⇒ API
private
Removes all entries and returns self.
-
#delete(entry) ⇒ API
private
Delete an entry from the cache.
-
#include?(entry) ⇒ Boolean
private
Check if the entry exists in the cache.
-
#initialize ⇒ Object
private
Initialize a new Cache.
-
#key_for(entry) ⇒ Object?
private
Given an entry, return the key to be used in the cache.
-
#valid?(entry) ⇒ Boolean
private
Tests if the given entry qualifies to be added to the cache.
Instance Method Details
#[](entry) ⇒ Integer?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the index for the entry in the cache
112 113 114 |
# File 'lib/dm-core/support/ordered_set.rb', line 112 def [](entry) @cache[key_for(entry)] end |
#[]=(entry, index) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set the index for the entry in the cache
127 128 129 130 131 |
# File 'lib/dm-core/support/ordered_set.rb', line 127 def []=(entry, index) if valid?(entry) @cache[key_for(entry)] = index end end |
#clear ⇒ API
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes all entries and returns self
156 157 158 159 |
# File 'lib/dm-core/support/ordered_set.rb', line 156 def clear @cache.clear self end |
#delete(entry) ⇒ API
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delete an entry from the cache
141 142 143 144 145 146 147 148 149 |
# File 'lib/dm-core/support/ordered_set.rb', line 141 def delete(entry) deleted_index = @cache.delete(key_for(entry)) if deleted_index @cache.each do |key, index| @cache[key] -= 1 if index > deleted_index end end deleted_index end |
#include?(entry) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the entry exists in the cache
99 100 101 |
# File 'lib/dm-core/support/ordered_set.rb', line 99 def include?(entry) @cache.has_key?(key_for(entry)) end |
#initialize ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new Cache
60 61 62 |
# File 'lib/dm-core/support/ordered_set.rb', line 60 def initialize @cache = {} end |
#key_for(entry) ⇒ Object?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Given an entry, return the key to be used in the cache
86 87 88 |
# File 'lib/dm-core/support/ordered_set.rb', line 86 def key_for(entry) raise NotImplementedError, "#{self}#key_for must be implemented" end |
#valid?(entry) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Tests if the given entry qualifies to be added to the cache
73 74 75 |
# File 'lib/dm-core/support/ordered_set.rb', line 73 def valid?(entry) raise NotImplementedError, "#{self}#valid? must be implemented" end |