Class: DataMapper::OrderedSet Private
- Inherits:
-
Object
- Object
- DataMapper::OrderedSet
- Extended by:
- Equalizer
- Includes:
- Enumerable
- Defined in:
- lib/dm-core/support/ordered_set.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
An ordered set of things
OrderedSet implements set behavior and keeps track of the order in which entries were added.
OrderedSet allows to inject a class that implements Cache::API at construction time, and will use that cache implementation to enforce set semantics and perform internal caching of insertion order.
Defined Under Namespace
Classes: Cache
Instance Attribute Summary collapse
-
#entries ⇒ Array
readonly
private
This set’s entries.
Instance Method Summary collapse
-
#<<(entry) ⇒ OrderedSet
private
Add or update an entry in the set.
-
#[](index) ⇒ Object?
private
Get the entry at the given index.
-
#clear ⇒ OrderedSet
private
Removes all entries and returns self.
-
#delete(entry) ⇒ Object?
private
Delete an entry from this OrderedSet.
-
#each {|entry| ... } ⇒ OrderedSet
private
Iterate over each entry in the set.
-
#empty? ⇒ Boolean
private
Check if there are any entries.
-
#include?(entry) ⇒ Boolean
private
Check if the entry exists in the set.
-
#index(entry) ⇒ Integer?
private
Return the index for the entry in the set.
-
#initialize(entries = [], cache = Cache) ⇒ OrderedSet
constructor
private
Initialize an OrderedSet.
-
#initialize_copy ⇒ Object
private
Initialize a copy of OrderedSet.
-
#merge(other) ⇒ OrderedSet
private
Merge with another Enumerable object.
-
#size ⇒ Integer
private
The number of entries.
-
#to_ary ⇒ Array
private
Convert the OrderedSet into an Array.
Methods included from Equalizer
Constructor Details
#initialize(entries = [], cache = Cache) ⇒ OrderedSet
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 an OrderedSet
220 221 222 223 224 |
# File 'lib/dm-core/support/ordered_set.rb', line 220 def initialize(entries = [], cache = Cache) @cache = cache.new @entries = [] merge(entries.to_ary) end |
Instance Attribute Details
#entries ⇒ Array (readonly)
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.
This set’s entries
The order in this Array is not guaranteed to be the order in which the entries were inserted. Use #each to access the entries in insertion order.
208 209 210 |
# File 'lib/dm-core/support/ordered_set.rb', line 208 def entries @entries end |
Instance Method Details
#<<(entry) ⇒ OrderedSet
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.
Add or update an entry in the set
If the entry to add isn’t part of the set already, it will be added. If an entry with the same cache key as the entry to add is part of the set already, it will be replaced with the given entry.
260 261 262 263 264 265 266 267 268 |
# File 'lib/dm-core/support/ordered_set.rb', line 260 def <<(entry) if index = @cache[entry] entries[index] = entry else @cache[entry] = size entries << entry end self end |
#[](index) ⇒ 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.
Get the entry at the given index
243 244 245 |
# File 'lib/dm-core/support/ordered_set.rb', line 243 def [](index) entries[index] end |
#clear ⇒ OrderedSet
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
303 304 305 306 307 |
# File 'lib/dm-core/support/ordered_set.rb', line 303 def clear @cache.clear entries.clear self end |
#delete(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.
Delete an entry from this OrderedSet
292 293 294 295 296 |
# File 'lib/dm-core/support/ordered_set.rb', line 292 def delete(entry) if index = @cache.delete(entry) entries.delete_at(index) end end |
#each {|entry| ... } ⇒ OrderedSet
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.
Iterate over each entry in the set
320 321 322 323 |
# File 'lib/dm-core/support/ordered_set.rb', line 320 def each entries.each { |entry| yield(entry) } self end |
#empty? ⇒ 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 there are any entries
341 342 343 |
# File 'lib/dm-core/support/ordered_set.rb', line 341 def empty? entries.empty? 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 set
354 355 356 |
# File 'lib/dm-core/support/ordered_set.rb', line 354 def include?(entry) entries.include?(entry) end |
#index(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 set
367 368 369 |
# File 'lib/dm-core/support/ordered_set.rb', line 367 def index(entry) @cache[entry] end |
#initialize_copy ⇒ 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 copy of OrderedSet
229 230 231 232 |
# File 'lib/dm-core/support/ordered_set.rb', line 229 def initialize_copy(*) @cache = @cache.dup @entries = @entries.dup end |
#merge(other) ⇒ OrderedSet
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.
Merge with another Enumerable object
278 279 280 281 |
# File 'lib/dm-core/support/ordered_set.rb', line 278 def merge(other) other.each { |entry| self << entry } self end |
#size ⇒ 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.
The number of entries
331 332 333 |
# File 'lib/dm-core/support/ordered_set.rb', line 331 def size entries.size end |
#to_ary ⇒ Array
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.
Convert the OrderedSet into an Array
377 378 379 |
# File 'lib/dm-core/support/ordered_set.rb', line 377 def to_ary entries end |