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



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.

Returns:

  • (Array)

    this set's entries



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.

Parameters:

  • entry (Object)

    the entry to be added

Returns:



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

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



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

Returns:



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

Parameters:

  • entry (Object)

    the entry to delete

Returns:

  • (Object, nil)

    the deleted entry or nil



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

Returns:



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

Returns:

  • (Boolean)

    true if the set is empty



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

Parameters:

  • entry (Object)

    the entry to test for

Returns:

  • (Boolean)

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

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



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

Parameters:

  • other (#each)

    the Enumerable to merge with this OrderedSet

Returns:



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

Returns:

  • (Integer)

    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

Returns:

  • (Array)

    an array containing all the OrderedSet's entries



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

def to_ary
  entries
end