Class: Mclone::ObjectSet
Overview
Two-way mapping between an object and its ID
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
Either add brand new object or replace existing one equal to the specified object.
-
#>>(obj) ⇒ Object
Remove object considered equal to the specified obj.
-
#[](obj) ⇒ Object
Return object considered equal to obj or nil.
- #commit! ⇒ Object
- #each(&code) ⇒ Object
- #each_id(&code) ⇒ Object
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#id(obj) ⇒ Object
Return ID of the object considered equal to the specified obj or nil.
-
#initialize ⇒ ObjectSet
constructor
A new instance of ObjectSet.
-
#merge!(objs) ⇒ Object
Add all tasks from enumerable.
- #modified? ⇒ Boolean
-
#object(id) ⇒ Object
Return object with specified ID or nil.
-
#resolve(pattern) ⇒ Object
Return a list of registered IDs (fully or partially) matching the specified pattern.
- #size ⇒ Object
Constructor Details
#initialize ⇒ ObjectSet
Returns a new instance of ObjectSet.
80 81 82 83 84 |
# File 'lib/mclone.rb', line 80 def initialize @ids = {} # { id => object } @objects = {} # { object => object } @modified = false end |
Instance Method Details
#<<(obj) ⇒ Object
Either add brand new object or replace existing one equal to the specified object
136 137 138 139 140 141 |
# File 'lib/mclone.rb', line 136 def <<(obj) forget(obj) @objects[obj] = @ids[obj.id] = obj @modified = true obj end |
#>>(obj) ⇒ Object
Remove object considered equal to the specified obj
144 145 146 147 |
# File 'lib/mclone.rb', line 144 def >>(obj) @modified = true if (status = forget(obj)) status end |
#[](obj) ⇒ Object
Return object considered equal to obj or nil
111 112 113 |
# File 'lib/mclone.rb', line 111 def [](obj) @objects[obj] end |
#commit! ⇒ Object
120 121 122 123 |
# File 'lib/mclone.rb', line 120 def commit! @modified = false self end |
#each(&code) ⇒ Object
66 67 68 |
# File 'lib/mclone.rb', line 66 def each(&code) @objects.each_value(&code) end |
#each_id(&code) ⇒ Object
61 62 63 |
# File 'lib/mclone.rb', line 61 def each_id(&code) @ids.each_key(&code) end |
#empty? ⇒ Boolean
71 72 73 |
# File 'lib/mclone.rb', line 71 def empty? @objects.empty? end |
#eql?(other) ⇒ Boolean Also known as: ==
94 95 96 |
# File 'lib/mclone.rb', line 94 def eql?(other) equal?(other) || objects == other.objects end |
#hash ⇒ Object
89 90 91 |
# File 'lib/mclone.rb', line 89 def hash @objects.hash end |
#id(obj) ⇒ Object
Return ID of the object considered equal to the specified obj or nil
101 102 103 |
# File 'lib/mclone.rb', line 101 def id(obj) @objects[obj]&.id end |
#merge!(objs) ⇒ Object
Add all tasks from enumerable
150 151 152 153 |
# File 'lib/mclone.rb', line 150 def merge!(objs) objs.each { |x| self << x } self end |
#modified? ⇒ Boolean
116 117 118 |
# File 'lib/mclone.rb', line 116 def modified? @modified end |
#object(id) ⇒ Object
Return object with specified ID or nil
106 107 108 |
# File 'lib/mclone.rb', line 106 def object(id) @ids[id] end |
#resolve(pattern) ⇒ Object
Return a list of registered IDs (fully or partially) matching the specified pattern
131 132 133 |
# File 'lib/mclone.rb', line 131 def resolve(pattern) each_id.to_a.resolve(pattern) end |
#size ⇒ Object
76 77 78 |
# File 'lib/mclone.rb', line 76 def size @objects.size end |