Class: Moxml::Builder
- Inherits:
-
Object
show all
- Defined in:
- lib/moxml/builder.rb
Constant Summary
collapse
- RESERVED_METHOD_PATTERN =
/\A(to_|as_json|marshal_|inspect|freeze|dup|clone)/
Instance Attribute Summary collapse
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_or_attrs = nil, attributes = {}, &block) ⇒ Object
When called with a String name: creates element via instance_eval (DSL block context).
-
#elements(element_specs) ⇒ Object
-
#entity_reference(name) ⇒ Object
-
#initialize(context) ⇒ Builder
constructor
A new instance of Builder.
-
#method_missing(method_name, *args, &block) ⇒ Object
Dynamic element creation DSL.
-
#namespace(prefix, uri) ⇒ Object
-
#ns_element(namespace_uri, name, attributes = {}, &block) ⇒ Object
Helper for creating namespaced elements.
-
#processing_instruction(target, content) ⇒ Object
-
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
-
#text(content) ⇒ Object
Constructor Details
#initialize(context) ⇒ Builder
Returns a new instance of Builder.
10
11
12
13
14
|
# File 'lib/moxml/builder.rb', line 10
def initialize(context)
@context = context
@current = @document = context.create_document
@namespaces = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Dynamic element creation DSL. xml.schema(attrs) { } creates <schema> with those attributes. Uses yield so blocks preserve the caller’s self context. Supported call shapes: (), (String), (Hash), (String, Hash).
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/moxml/builder.rb', line 100
def method_missing(method_name, *args, &block)
return super if RESERVED_METHOD_PATTERN.match?(method_name.to_s)
text_content = args.first.is_a?(String) ? args.shift : nil
attrs = args.first.is_a?(Hash) ? args.shift : {}
unless args.empty?
raise ArgumentError,
"unexpected arguments for #{method_name}: #{args.inspect}"
end
if text_content && block
raise ArgumentError,
"#{method_name}: cannot combine text content with a block"
end
tag_name = method_name.to_s.chomp("_")
create_element_node(tag_name, attrs, text_content: text_content,
block: block, eval_block: false)
end
|
Instance Attribute Details
#document ⇒ Object
Also known as:
doc
Returns the value of attribute document.
7
8
9
|
# File 'lib/moxml/builder.rb', line 7
def document
@document
end
|
Instance Method Details
#build(&block) ⇒ Object
16
17
18
19
|
# File 'lib/moxml/builder.rb', line 16
def build(&block)
instance_eval(&block)
@document
end
|
#cdata(content) ⇒ Object
47
48
49
|
# File 'lib/moxml/builder.rb', line 47
def cdata(content)
@current.add_child(@document.create_cdata(content))
end
|
51
52
53
|
# File 'lib/moxml/builder.rb', line 51
def (content)
@current.add_child(@document.(content))
end
|
#declaration(version: "1.0", encoding: "UTF-8", standalone: nil) ⇒ Object
21
22
23
24
25
|
# File 'lib/moxml/builder.rb', line 21
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
71
72
73
74
75
|
# File 'lib/moxml/builder.rb', line 71
def doctype(name, external_id = nil, system_id = nil)
@current.add_child(
@document.create_doctype(name, external_id, system_id),
)
end
|
#element(name_or_attrs = nil, attributes = {}, &block) ⇒ Object
When called with a String name: creates element via instance_eval (DSL block context). When called with a Hash (e.g., element(name: “foo”)): creates <element> tag via yield — handles collision where “element” is both a builder method and a valid XML tag name (XSD/RelaxNG).
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/moxml/builder.rb', line 31
def element(name_or_attrs = nil, attributes = {}, &block)
if name_or_attrs.is_a?(Hash)
return create_element_node("element", name_or_attrs, block: block,
eval_block: false)
end
raise ArgumentError, "element requires a tag name" if name_or_attrs.nil?
create_element_node(name_or_attrs, attributes, block: block,
eval_block: true)
end
|
#elements(element_specs) ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/moxml/builder.rb', line 78
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
55
56
57
|
# File 'lib/moxml/builder.rb', line 55
def entity_reference(name)
@current.add_child(@document.create_entity_reference(name))
end
|
#namespace(prefix, uri) ⇒ Object
65
66
67
68
|
# File 'lib/moxml/builder.rb', line 65
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
89
90
91
92
93
94
|
# File 'lib/moxml/builder.rb', line 89
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
59
60
61
62
63
|
# File 'lib/moxml/builder.rb', line 59
def processing_instruction(target, content)
@current.add_child(
@document.create_processing_instruction(target, content),
)
end
|
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
124
125
126
127
128
|
# File 'lib/moxml/builder.rb', line 124
def respond_to_missing?(method_name, _include_private = false)
return super if RESERVED_METHOD_PATTERN.match?(method_name.to_s)
true
end
|
#text(content) ⇒ Object
43
44
45
|
# File 'lib/moxml/builder.rb', line 43
def text(content)
@current.add_child(@document.create_text(content))
end
|