Class: Moxml::NativeAttachment
- Inherits:
-
Object
- Object
- Moxml::NativeAttachment
- Defined in:
- lib/moxml/native_attachment.rb
Overview
Stores Moxml-specific state associated with native adapter objects without polluting their internals.
Uses object_id as key with GC finalizer cleanup to prevent memory leaks. Thread-safe via Monitor (reentrant-safe).
Replaces the anti-pattern of using instance_variable_set/get on foreign library objects (Nokogiri, REXML, Oga, Ox, LibXML nodes).
Instance Method Summary collapse
- #delete(native, key) ⇒ Object
- #get(native, key) ⇒ Object
-
#initialize ⇒ NativeAttachment
constructor
A new instance of NativeAttachment.
- #key?(native, key) ⇒ Boolean
- #set(native, key, value) ⇒ Object
Constructor Details
#initialize ⇒ NativeAttachment
Returns a new instance of NativeAttachment.
19 20 21 22 23 |
# File 'lib/moxml/native_attachment.rb', line 19 def initialize @data = {} @finalizer_registered = {} @monitor = Monitor.new end |
Instance Method Details
#delete(native, key) ⇒ Object
42 43 44 |
# File 'lib/moxml/native_attachment.rb', line 42 def delete(native, key) @monitor.synchronize { @data[native.object_id]&.delete(key) } end |
#get(native, key) ⇒ Object
25 26 27 |
# File 'lib/moxml/native_attachment.rb', line 25 def get(native, key) @monitor.synchronize { @data[native.object_id]&.[](key) } end |
#key?(native, key) ⇒ Boolean
38 39 40 |
# File 'lib/moxml/native_attachment.rb', line 38 def key?(native, key) @monitor.synchronize { @data[native.object_id]&.key?(key) || false } end |
#set(native, key, value) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/moxml/native_attachment.rb', line 29 def set(native, key, value) id = native.object_id @monitor.synchronize do @data[id] ||= {} @data[id][key] = value register_finalizer(native, id) unless @finalizer_registered[id] end end |