class Mclone::ObjectSet

Two-way mapping between an object and its ID

Attributes

objects[R]

Public Class Methods

new() click to toggle source
# File lib/mclone.rb, line 80
def initialize
  @ids = {} # { id => object }
  @objects = {} # { object => object }
  @modified = false
end

Public Instance Methods

<<(obj) click to toggle source

Either add brand new object or replace existing one equal to the specified object

# File lib/mclone.rb, line 136
def <<(obj)
  forget(obj)
  @objects[obj] = @ids[obj.id] = obj
  @modified = true
  obj
end
==(other)
Alias for: eql?
>>(obj) click to toggle source

Remove object considered equal to the specified obj

# File lib/mclone.rb, line 144
def >>(obj)
  @modified = true if (status = forget(obj))
  status
end
[](obj) click to toggle source

Return object considered equal to obj or nil

# File lib/mclone.rb, line 111
def [](obj)
  @objects[obj]
end
commit!() click to toggle source
# File lib/mclone.rb, line 120
def commit!
  @modified = false
  self
end
each(&code) click to toggle source
# File lib/mclone.rb, line 66
def each(&code)
  @objects.each_value(&code)
end
each_id(&code) click to toggle source
# File lib/mclone.rb, line 61
def each_id(&code)
  @ids.each_key(&code)
end
empty?() click to toggle source
# File lib/mclone.rb, line 71
def empty?
  @objects.empty?
end
eql?(other) click to toggle source
# File lib/mclone.rb, line 94
def eql?(other)
  equal?(other) || objects == other.objects
end
Also aliased as: ==
hash() click to toggle source
# File lib/mclone.rb, line 89
def hash
  @objects.hash
end
id(obj) click to toggle source

Return ID of the object considered equal to the specified obj or nil

# File lib/mclone.rb, line 101
def id(obj)
  @objects[obj]&.id
end
merge!(objs) click to toggle source

Add all tasks from enumerable

# File lib/mclone.rb, line 150
def merge!(objs)
  objs.each { |x| self << x }
  self
end
modified?() click to toggle source
# File lib/mclone.rb, line 116
def modified?
  @modified
end
object(id) click to toggle source

Return object with specified ID or nil

# File lib/mclone.rb, line 106
def object(id)
  @ids[id]
end
resolve(pattern) click to toggle source

Return a list of registered IDs (fully or partially) matching the specified pattern

# File lib/mclone.rb, line 131
def resolve(pattern)
  each_id.to_a.resolve(pattern)
end
size() click to toggle source
# File lib/mclone.rb, line 76
def size
  @objects.size
end

Private Instance Methods

forget(obj) click to toggle source

Unregister an object considered equal to the specified obj and return true if object has been actually removed

# File lib/mclone.rb, line 126
        def forget(obj)
  !@ids.delete(@objects.delete(obj)&.id).nil?
end