Class: Nokogiri::XML::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/builder.rb

Direct Known Subclasses

HTML::Builder

Defined Under Namespace

Classes: NodeBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Returns a new instance of Builder.



5
6
7
8
9
10
11
12
# File 'lib/nokogiri/xml/builder.rb', line 5

def initialize(&block)
  namespace = self.class.name.split('::')
  namespace[-1] = 'Document'
  @doc = eval(namespace.join('::')).new
  @parent = @doc
  instance_eval(&block)
  @parent = @doc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nokogiri/xml/builder.rb', line 28

def method_missing(method, *args, &block)
  node = Nokogiri::XML::Node.new(method.to_s) { |n|
    if content = args.first
      if content.is_a?(Hash)
        content.each { |k,v| n[k.to_s] = v.to_s }
      else
        n.content = content
      end
    end
  }
  insert(node, &block)
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



4
5
6
# File 'lib/nokogiri/xml/builder.rb', line 4

def doc
  @doc
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/nokogiri/xml/builder.rb', line 4

def parent
  @parent
end

Instance Method Details

#cdata(string) ⇒ Object



19
20
21
22
# File 'lib/nokogiri/xml/builder.rb', line 19

def cdata(string)
  node = Nokogiri::XML::CData.new(@doc, string)
  insert(node)
end

#text(string) ⇒ Object



14
15
16
17
# File 'lib/nokogiri/xml/builder.rb', line 14

def text(string)
  node = Nokogiri::XML::Text.new(string)
  insert(node)
end

#to_xmlObject



24
25
26
# File 'lib/nokogiri/xml/builder.rb', line 24

def to_xml
  @doc.to_xml
end