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

Parameters:

  • entries (#each) (defaults to: [])

    the entries to initialize this set with

  • cache (Class<Cache::API>) (defaults to: Cache)

    the cache implementation to use



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

#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.

Returns:

  • (Array)

    this set’s entries



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.

Parameters:

  • entry (Object)

    the entry to be added

Returns:



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

Parameters:

  • index (Integer)

    the index of the desired entry

Returns:

  • (Object, nil)

    the entry at the given index, or nil if no entry is present



243
244
245
# File 'lib/dm-core/support/ordered_set.rb', line 243

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

Returns:



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

Parameters:

  • entry (Object)

    the entry to delete

Returns:

  • (Object, nil)

    the deleted entry or nil



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

Yields:

  • (entry)

    all entries in the set

Yield Parameters:

  • entry (Object)

    an entry in the set

Returns:



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

Returns:

  • (Boolean)

    true if the set is empty



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

Parameters:

  • entry (Object)

    the entry to test for

Returns:

  • (Boolean)

    true if entry is included 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

Parameters:

  • entry (Object)

    the entry to check the set for

Returns:

  • (Integer, nil)

    the index for the entry, or nil if it does not exist



367
368
369
# File 'lib/dm-core/support/ordered_set.rb', line 367

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



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

Parameters:

  • other (#each)

    the Enumerable to merge with this OrderedSet

Returns:



278
279
280
281
# File 'lib/dm-core/support/ordered_set.rb', line 278

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

Returns:

  • (Integer)

    the number of entries



331
332
333
# File 'lib/dm-core/support/ordered_set.rb', line 331

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

Returns:

  • (Array)

    an array containing all the OrderedSet’s entries



377
378
379
# File 'lib/dm-core/support/ordered_set.rb', line 377

def to_ary
  entries
end