Class: Moxml::Builder
- Inherits:
-
Object
- Object
- Moxml::Builder
- Defined in:
- lib/moxml/builder.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
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.
- #entity_reference(name) ⇒ Object
-
#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.
7 8 9 10 11 |
# File 'lib/moxml/builder.rb', line 7 def initialize(context) @context = context @current = @document = context.create_document @namespaces = {} end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
5 6 7 |
# File 'lib/moxml/builder.rb', line 5 def document @document end |
Instance Method Details
#build(&block) ⇒ Object
13 14 15 16 |
# File 'lib/moxml/builder.rb', line 13 def build(&block) instance_eval(&block) @document end |
#cdata(content) ⇒ Object
57 58 59 |
# File 'lib/moxml/builder.rb', line 57 def cdata(content) @current.add_child(@document.create_cdata(content)) end |
#comment(content) ⇒ Object
61 62 63 |
# File 'lib/moxml/builder.rb', line 61 def comment(content) @current.add_child(@document.create_comment(content)) end |
#declaration(version: "1.0", encoding: "UTF-8", standalone: nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/moxml/builder.rb', line 18 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
81 82 83 84 85 |
# File 'lib/moxml/builder.rb', line 81 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
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 50 51 |
# File 'lib/moxml/builder.rb', line 24 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
88 89 90 91 92 93 94 95 96 |
# File 'lib/moxml/builder.rb', line 88 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 |
#entity_reference(name) ⇒ Object
65 66 67 |
# File 'lib/moxml/builder.rb', line 65 def entity_reference(name) @current.add_child(@document.create_entity_reference(name)) end |
#namespace(prefix, uri) ⇒ Object
75 76 77 78 |
# File 'lib/moxml/builder.rb', line 75 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
99 100 101 102 103 104 |
# File 'lib/moxml/builder.rb', line 99 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
69 70 71 72 73 |
# File 'lib/moxml/builder.rb', line 69 def processing_instruction(target, content) @current.add_child( @document.create_processing_instruction(target, content), ) end |
#text(content) ⇒ Object
53 54 55 |
# File 'lib/moxml/builder.rb', line 53 def text(content) @current.add_child(@document.create_text(content)) end |