Class: DataMapper::OrderedSet Private

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

Instance Method Summary collapse

Methods included from Equalizer

equalize

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

#entriesArray (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

#clearOrderedSet

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

Yields:

  • (entry)

    all entries in the set

Yield Parameters:

  • entry (Object)

    an entry in the set



318
319
320
321
# File 'lib/dm-core/support/ordered_set.rb', line 318

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



339
340
341
# File 'lib/dm-core/support/ordered_set.rb', line 339

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



352
353
354
# File 'lib/dm-core/support/ordered_set.rb', line 352

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



365
366
367
# File 'lib/dm-core/support/ordered_set.rb', line 365

def index(entry)
  @cache[entry]
end

#initialize_copyObject

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

#sizeInteger

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



329
330
331
# File 'lib/dm-core/support/ordered_set.rb', line 329

def size
  entries.size
end

#to_aryArray

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



375
376
377
# File 'lib/dm-core/support/ordered_set.rb', line 375

def to_ary
  entries
end