Class: Moxml::Adapter::CustomizedLibxml::Declaration
- Inherits:
-
Object
- Object
- Moxml::Adapter::CustomizedLibxml::Declaration
- 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
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#native ⇒ Object
readonly
Returns the value of attribute native.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(native_doc, version = nil, encoding = nil, standalone = nil) ⇒ Declaration
constructor
A new instance of Declaration.
- #standalone ⇒ Object
- #standalone=(value) ⇒ Object
-
#to_xml ⇒ Object
Generate XML declaration string.
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
#encoding ⇒ Object
Returns the value of attribute encoding.
12 13 14 |
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 12 def encoding @encoding end |
#native ⇒ Object (readonly)
Returns the value of attribute native.
13 14 15 |
# File 'lib/moxml/adapter/customized_libxml/declaration.rb', line 13 def native @native end |
#version ⇒ Object
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
#standalone ⇒ Object
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_xml ⇒ Object
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 |