Class: Foobara::WeakObjectSet
- Inherits:
-
Object
- Object
- Foobara::WeakObjectSet
- Includes:
- Enumerable
- Defined in:
- foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb,
foobara-0.0.110/projects/weak_object_set/lib/foobara/weak_object_set.rb
Defined Under Namespace
Classes: GarbageCleaner
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #[](object_or_object_id) ⇒ Object
- #clear ⇒ Object
- #delete(object) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #find_by_key(key) ⇒ Object
- #garbage_cleaner ⇒ Object
- #include?(object_or_object_id) ⇒ Boolean
- #include_key?(key) ⇒ Boolean
-
#initialize(key_method = nil) ⇒ WeakObjectSet
constructor
A new instance of WeakObjectSet.
- #key_to_object_id ⇒ Object
- #object_id_to_key ⇒ Object
- #objects ⇒ Object
- #ref_for(object_or_object_id) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(key_method = nil) ⇒ WeakObjectSet
Returns a new instance of WeakObjectSet.
41 42 43 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 41 def initialize(key_method = nil) @key_method = key_method end |
Instance Method Details
#<<(object) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 99 def <<(object) garbage_cleaner.track(object) object_id = object.object_id objects[object_id] = WeakRef.new(object) if @key_method key = object.send(@key_method) if key key_to_object_id[key] = object_id object_id_to_key[object_id] = key end end object end |
#[](object_or_object_id) ⇒ Object
45 46 47 48 49 50 51 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 45 def [](object_or_object_id) ref = ref_for(object_or_object_id) if ref&.weakref_alive? ref.__getobj__ end end |
#clear ⇒ Object
158 159 160 161 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 158 def clear @garbage_cleaner&.deactivate @key_to_object_id = @object_id_to_key = @objects = @garbage_cleaner = nil end |
#delete(object) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 122 def delete(object) if @key_method key = @object_id_to_key&.delete(object.object_id) if key @key_to_object_id.delete(key) end end objects.delete(object) end |
#each ⇒ Object
63 64 65 66 67 68 69 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 63 def each objects.each_value do |ref| if ref.weakref_alive? yield ref.__getobj__ end end end |
#empty? ⇒ Boolean
79 80 81 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 79 def empty? objects.empty? || objects.values.none?(&:weakref_alive?) end |
#find_by_key(key) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 144 def find_by_key(key) unless @key_method # :nocov: raise "Cannot find by key if there was no key_method given." # :nocov: end object = objects[key_to_object_id[key]] if object&.weakref_alive? object.__getobj__ end end |
#garbage_cleaner ⇒ Object
91 92 93 94 95 96 97 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 91 def garbage_cleaner @garbage_cleaner ||= if @key_method GarbageCleaner.new(objects, key_to_object_id, object_id_to_key) else GarbageCleaner.new(objects) end end |
#include?(object_or_object_id) ⇒ Boolean
118 119 120 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 118 def include?(object_or_object_id) ref_for(object_or_object_id)&.weakref_alive? end |
#include_key?(key) ⇒ Boolean
134 135 136 137 138 139 140 141 142 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 134 def include_key?(key) unless @key_method # :nocov: raise "Cannot check by key if there was no key_method given." # :nocov: end objects[key_to_object_id[key]]&.weakref_alive? end |
#key_to_object_id ⇒ Object
83 84 85 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 83 def key_to_object_id @key_to_object_id ||= {} end |
#object_id_to_key ⇒ Object
87 88 89 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 87 def object_id_to_key @object_id_to_key ||= {} end |
#objects ⇒ Object
71 72 73 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 71 def objects @objects ||= {} end |
#ref_for(object_or_object_id) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 53 def ref_for(object_or_object_id) object_id = if object_or_object_id.is_a?(::Integer) object_or_object_id else object_or_object_id.object_id end objects[object_id] end |
#size ⇒ Object
75 76 77 |
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 75 def size count end |