Class: Moxml::Builder

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

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Builder



5
6
7
8
9
# File 'lib/moxml/builder.rb', line 5

def initialize(context)
  @context = context
  @current = @document = context.create_document
  @namespaces = {}
end

Instance Method Details

#build(&block) ⇒ Object



11
12
13
14
# File 'lib/moxml/builder.rb', line 11

def build(&block)
  instance_eval(&block)
  @document
end

#cdata(content) ⇒ Object



45
46
47
# File 'lib/moxml/builder.rb', line 45

def cdata(content)
  @current.add_child(@document.create_cdata(content))
end

#comment(content) ⇒ Object



49
50
51
# File 'lib/moxml/builder.rb', line 49

def comment(content)
  @current.add_child(@document.create_comment(content))
end

#declaration(version: "1.0", encoding: "UTF-8", standalone: nil) ⇒ Object



16
17
18
19
20
# File 'lib/moxml/builder.rb', line 16

def declaration(version: "1.0", encoding: "UTF-8", standalone: nil)
  @current.add_child(
    @document.create_declaration(version, encoding, standalone)
  )
end

#element(name, attributes = {}, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/moxml/builder.rb', line 22

def element(name, attributes = {}, &block)
  el = @document.create_element(name)

  attributes.each do |key, value|
    el[key] = value
  end

  @current.add_child(el)

  if block_given?
    previous = @current
    @current = el
    instance_eval(&block)
    @current = previous
  end

  el
end

#namespace(prefix, uri) ⇒ Object



59
60
61
62
# File 'lib/moxml/builder.rb', line 59

def namespace(prefix, uri)
  @current.add_namespace(prefix, uri)
  @namespaces[prefix] = uri
end

#processing_instruction(target, content) ⇒ Object



53
54
55
56
57
# File 'lib/moxml/builder.rb', line 53

def processing_instruction(target, content)
  @current.add_child(
    @document.create_processing_instruction(target, content)
  )
end

#text(content) ⇒ Object



41
42
43
# File 'lib/moxml/builder.rb', line 41

def text(content)
  @current.add_child(@document.create_text(content))
end