Class: Moxml::Builder
- Inherits:
-
Object
- Object
- Moxml::Builder
- Defined in:
- lib/moxml/builder.rb
Instance Method Summary collapse
- #build(&block) ⇒ Object
- #cdata(content) ⇒ Object
- #comment(content) ⇒ Object
- #declaration(version: "1.0", encoding: "UTF-8", standalone: nil) ⇒ Object
-
#doctype(name, external_id = nil, system_id = nil) ⇒ Object
Convenience method for DOCTYPE.
- #element(name, attributes = {}, &block) ⇒ Object
-
#elements(element_specs) ⇒ Object
Batch element creation.
-
#initialize(context) ⇒ Builder
constructor
A new instance of Builder.
- #namespace(prefix, uri) ⇒ Object
-
#ns_element(namespace_uri, name, attributes = {}, &block) ⇒ Object
Helper for creating namespaced elements.
- #processing_instruction(target, content) ⇒ Object
- #text(content) ⇒ Object
Constructor Details
#initialize(context) ⇒ Builder
Returns a new instance of 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
55 56 57 |
# File 'lib/moxml/builder.rb', line 55 def cdata(content) @current.add_child(@document.create_cdata(content)) end |
#comment(content) ⇒ Object
59 60 61 |
# File 'lib/moxml/builder.rb', line 59 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 |
#doctype(name, external_id = nil, system_id = nil) ⇒ Object
Convenience method for DOCTYPE
75 76 77 78 79 |
# File 'lib/moxml/builder.rb', line 75 def doctype(name, external_id = nil, system_id = nil) @current.add_child( @document.create_doctype(name, external_id, system_id), ) end |
#element(name, attributes = {}, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/moxml/builder.rb', line 22 def element(name, attributes = {}, &block) el = @document.create_element(name) attributes.each do |key, value| if key.to_s == "xmlns" # Handle default namespace el.add_namespace(nil, value.to_s) elsif key.to_s.start_with?("xmlns:") # Handle prefixed namespace prefix = key.to_s.sub("xmlns:", "") el.add_namespace(prefix, value.to_s) else # Regular attribute el[key] = value end end @current.add_child(el) if block previous = @current @current = el instance_eval(&block) @current = previous end el end |
#elements(element_specs) ⇒ Object
Batch element creation
82 83 84 85 86 87 88 89 90 |
# File 'lib/moxml/builder.rb', line 82 def elements(element_specs) element_specs.each do |name, content_or_attrs| if content_or_attrs.is_a?(Hash) element(name, content_or_attrs) else element(name) { text(content_or_attrs) } end end end |
#namespace(prefix, uri) ⇒ Object
69 70 71 72 |
# File 'lib/moxml/builder.rb', line 69 def namespace(prefix, uri) @current.add_namespace(prefix, uri) @namespaces[prefix] = uri end |
#ns_element(namespace_uri, name, attributes = {}, &block) ⇒ Object
Helper for creating namespaced elements
93 94 95 96 97 98 |
# File 'lib/moxml/builder.rb', line 93 def ns_element(namespace_uri, name, attributes = {}, &block) el = element(name, attributes, &block) prefix = @namespaces.key(namespace_uri) el.namespace = { prefix => namespace_uri } if prefix el end |
#processing_instruction(target, content) ⇒ Object
63 64 65 66 67 |
# File 'lib/moxml/builder.rb', line 63 def processing_instruction(target, content) @current.add_child( @document.create_processing_instruction(target, content), ) end |
#text(content) ⇒ Object
51 52 53 |
# File 'lib/moxml/builder.rb', line 51 def text(content) @current.add_child(@document.create_text(content)) end |