Class: Moxml::NativeAttachment::Native

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/native_attachment/native.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).

Examples:

attachments = NativeAttachment.new
attachments.set(native_element, :entity_refs, [])
refs = attachments.get(native_element, :entity_refs)
attachments.key?(native_element, :doctype) #=> false

Instance Method Summary collapse

Constructor Details

#initializeNative

Returns a new instance of Native.



22
23
24
25
26
# File 'lib/moxml/native_attachment/native.rb', line 22

def initialize
  @data = {}
  @finalizer_registered = {}
  @monitor = Monitor.new
end

Instance Method Details

#delete(native, key) ⇒ Object



45
46
47
# File 'lib/moxml/native_attachment/native.rb', line 45

def delete(native, key)
  @monitor.synchronize { @data[native.object_id]&.delete(key) }
end

#get(native, key) ⇒ Object



28
29
30
# File 'lib/moxml/native_attachment/native.rb', line 28

def get(native, key)
  @monitor.synchronize { @data[native.object_id]&.[](key) }
end

#key?(native, key) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/moxml/native_attachment/native.rb', line 41

def key?(native, key)
  @monitor.synchronize { @data[native.object_id]&.key?(key) || false }
end

#set(native, key, value) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/moxml/native_attachment/native.rb', line 32

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