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
218 219 220 221 222 |
# File 'lib/dm-core/support/ordered_set.rb', line 218 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.
206 207 208 |
# File 'lib/dm-core/support/ordered_set.rb', line 206 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.
258 259 260 261 262 263 264 265 266 |
# File 'lib/dm-core/support/ordered_set.rb', line 258 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
241 242 243 |
# File 'lib/dm-core/support/ordered_set.rb', line 241 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
301 302 303 304 305 |
# File 'lib/dm-core/support/ordered_set.rb', line 301 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
290 291 292 293 294 |
# File 'lib/dm-core/support/ordered_set.rb', line 290 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
318 319 320 321 322 |
# File 'lib/dm-core/support/ordered_set.rb', line 318 def each return to_enum unless block_given? 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
340 341 342 |
# File 'lib/dm-core/support/ordered_set.rb', line 340 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
353 354 355 |
# File 'lib/dm-core/support/ordered_set.rb', line 353 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
366 367 368 |
# File 'lib/dm-core/support/ordered_set.rb', line 366 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
227 228 229 230 |
# File 'lib/dm-core/support/ordered_set.rb', line 227 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
276 277 278 279 |
# File 'lib/dm-core/support/ordered_set.rb', line 276 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
330 331 332 |
# File 'lib/dm-core/support/ordered_set.rb', line 330 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
376 377 378 |
# File 'lib/dm-core/support/ordered_set.rb', line 376 def to_ary entries end |