Class: Peanuts::XML::LibXML::Writer

Inherits:
Writer
  • Object
show all
Defined in:
lib/peanuts/xml/libxml.rb

Instance Method Summary collapse

Methods inherited from Writer

method_missing, new

Constructor Details

#initialize(dest, dest_type, options = {}) ⇒ Writer

Returns a new instance of Writer.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/peanuts/xml/libxml.rb', line 9

def initialize(dest, dest_type, options = {})
  @dest_type = dest_type
  @dest = case dest_type
  when :string
    dest || ''
  when :io
    dest
  when :document
    dest || ::LibXML::XML::Document.new
  else
    raise ArgumentError, "unrecognized destination type #{dest_type.inspect}"
  end
end

Instance Method Details

#resultObject



23
24
25
# File 'lib/peanuts/xml/libxml.rb', line 23

def result
  @dest
end

#value=(value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/peanuts/xml/libxml.rb', line 27

def value=(value)
  case @node
  when ::LibXML::XML::Attr
    @node.value = value || ''
  else
    @node.content = value || ''
  end
end

#write(node_type, local_name = nil, namespace_uri = nil, prefix = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/peanuts/xml/libxml.rb', line 36

def write(node_type, local_name = nil, namespace_uri = nil, prefix = nil)
  case node_type
  when :element
    @node = ::LibXML::XML::Node.new(local_name)
    @parent << @node if @parent
    @node.namespaces.namespace = mkns(@node, namespace_uri, prefix) if namespace_uri
  when :attribute
    @node = ::LibXML::XML::Attr.new(@parent, local_name, '', namespace_uri && mkns(@parent, namespace_uri, prefix))
  else
    raise "unsupported node type #{node_type.inspect}"
  end

  exparent, @parent = @parent, @node

  yield self

  if exparent.nil?
    case @dest_type
    when :string, :io
      @dest << @parent.to_s
    when :document
      @dest.root = @parent
    end
  end

  @parent = exparent
end