Class: Moxml::Builder
- Inherits:
-
Object
- Object
- Moxml::Builder
- Defined in:
- lib/moxml/builder.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
(also: #doc)
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.
8 9 10 11 12 |
# File 'lib/moxml/builder.rb', line 8 def initialize(context) @context = context @current = @document = context.create_document @namespaces = {} end |
Instance Attribute Details
#document ⇒ Object (readonly) Also known as: doc
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
14 15 16 17 |
# File 'lib/moxml/builder.rb', line 14 def build(&block) instance_eval(&block) @document end |
#cdata(content) ⇒ Object
58 59 60 |
# File 'lib/moxml/builder.rb', line 58 def cdata(content) @current.add_child(@document.create_cdata(content)) end |
#comment(content) ⇒ Object
62 63 64 |
# File 'lib/moxml/builder.rb', line 62 def comment(content) @current.add_child(@document.create_comment(content)) end |
#declaration(version: "1.0", encoding: "UTF-8", standalone: nil) ⇒ Object
19 20 21 22 23 |
# File 'lib/moxml/builder.rb', line 19 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
82 83 84 85 86 |
# File 'lib/moxml/builder.rb', line 82 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
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 52 |
# File 'lib/moxml/builder.rb', line 25 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
89 90 91 92 93 94 95 96 97 |
# File 'lib/moxml/builder.rb', line 89 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
66 67 68 |
# File 'lib/moxml/builder.rb', line 66 def entity_reference(name) @current.add_child(@document.create_entity_reference(name)) end |
#namespace(prefix, uri) ⇒ Object
76 77 78 79 |
# File 'lib/moxml/builder.rb', line 76 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
100 101 102 103 104 105 |
# File 'lib/moxml/builder.rb', line 100 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
70 71 72 73 74 |
# File 'lib/moxml/builder.rb', line 70 def processing_instruction(target, content) @current.add_child( @document.create_processing_instruction(target, content), ) end |
#text(content) ⇒ Object
54 55 56 |
# File 'lib/moxml/builder.rb', line 54 def text(content) @current.add_child(@document.create_text(content)) end |