Class: Philiprehberger::XmlBuilder::ProcessingInstruction

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/xml_builder/processing_instruction.rb

Overview

Represents an XML processing instruction (PI).

Renders as <?target key=“value” key2=“value2”?>, with attribute values escaped via Escaper.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, attributes = {}) ⇒ ProcessingInstruction

Returns a new instance of ProcessingInstruction.

Parameters:

  • target (String)

    the PI target name

  • attributes (Hash) (defaults to: {})

    attribute key/value pairs



14
15
16
17
# File 'lib/philiprehberger/xml_builder/processing_instruction.rb', line 14

def initialize(target, attributes = {})
  @target = target.to_s
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/philiprehberger/xml_builder/processing_instruction.rb', line 10

def attributes
  @attributes
end

#targetObject (readonly)

Returns the value of attribute target.



10
11
12
# File 'lib/philiprehberger/xml_builder/processing_instruction.rb', line 10

def target
  @target
end

Instance Method Details

#render(indent: nil, level: 0) ⇒ Object

Alias for to_xml to match Node#render semantics.



32
33
34
# File 'lib/philiprehberger/xml_builder/processing_instruction.rb', line 32

def render(indent: nil, level: 0)
  to_xml(indent: indent, level: level, pretty: !indent.nil?)
end

#to_xml(indent: nil, level: 0, pretty: false) ⇒ String

Render this processing instruction as an XML string.

Parameters:

  • indent (Integer, nil) (defaults to: nil)

    number of spaces per indentation level, or nil for compact output

  • level (Integer) (defaults to: 0)

    current nesting depth (used internally)

  • pretty (Boolean) (defaults to: false)

    whether to apply pretty-print formatting

Returns:

  • (String)

    the rendered processing instruction



25
26
27
28
29
# File 'lib/philiprehberger/xml_builder/processing_instruction.rb', line 25

def to_xml(indent: nil, level: 0, pretty: false)
  prefix = indent && pretty ? ' ' * (indent * level) : ''
  newline = indent && pretty ? "\n" : ''
  "#{prefix}<?#{@target}#{render_attributes}?>#{newline}"
end