Class: Moxml::Adapter::CustomizedLibxml::Declaration

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/adapter/customized_libxml/declaration.rb

Overview

Wrapper for LibXML document declarations

LibXML::XML::Document properties (version, encoding, standalone) are read-only after creation. This wrapper allows mutation by storing values internally and regenerating XML when needed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(native_doc, version = nil, encoding = nil, standalone = nil) ⇒ Declaration

Returns a new instance of Declaration.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 15

def initialize(native_doc, version = nil, encoding = nil,
               standalone = nil)
  @native = native_doc
  # Store explicit values - don't default from native_doc
  @version = version || native_doc.version || "1.0"
  # Only use encoding if explicitly provided, otherwise nil
  @encoding = encoding
  # Parse standalone value
  @standalone_value = case standalone
                      when "yes", true
                        true
                      when "no", false
                        false
                      end
end

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



12
13
14
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 12

def encoding
  @encoding
end

#nativeObject (readonly)

Returns the value of attribute native.



13
14
15
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 13

def native
  @native
end

#versionObject

Returns the value of attribute version.



12
13
14
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 12

def version
  @version
end

Instance Method Details

#standaloneObject



31
32
33
34
35
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 31

def standalone
  return nil if @standalone_value.nil?

  @standalone_value ? "yes" : "no"
end

#standalone=(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 37

def standalone=(value)
  @standalone_value = case value
                      when "yes", true
                        true
                      when "no", false
                        false
                      when nil
                        nil
                      end
end

#to_xmlObject

Generate XML declaration string



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 49

def to_xml
  output = "<?xml version=\"#{@version}\""
  if @encoding && !@encoding.empty?
    output << " encoding=\"#{@encoding}\""
  end
  # Include standalone attribute if explicitly set (true or false)
  unless @standalone_value.nil?
    output << " standalone=\"#{standalone}\""
  end
  output << "?>"
  output
end