Class: Foobara::WeakObjectSet

Inherits:
Object
  • Object
show all
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

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

#clearObject



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

#eachObject



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

Returns:

  • (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_cleanerObject



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

Returns:

  • (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

Returns:

  • (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_idObject



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_keyObject



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

#objectsObject



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

#sizeObject



75
76
77
# File 'foobara-0.0.110/projects/weak_object_set/src/weak_object_set.rb', line 75

def size
  count
end