Module: RubySpeech::GenericElement
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#+(other) ⇒ Object
-
#<<(other) ⇒ Object
-
#==(o) ⇒ Object
-
#base_uri ⇒ String
The base URI to which relative URLs are resolved.
-
#base_uri=(uri) ⇒ Object
-
#build(atts, &block) ⇒ Object
-
#children(type = nil, attributes = nil) ⇒ Object
-
#clone ⇒ Object
-
#create_node ⇒ Object
-
#embed(other) ⇒ Object
-
#eql?(o, *fields) ⇒ Fixnum<-1,0,1>
Check that a set of fields are equal between nodes.
-
#eval_dsl_block(&block) ⇒ Object
-
#inherit(node) ⇒ Object
-
#initialize(doc, atts = nil, &block) ⇒ Object
-
#inspect ⇒ String
-
#mass_assign(attrs) ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
-
#namespace=(namespaces) ⇒ Object
Attach a namespace to the node.
-
#namespace_href ⇒ XML::Namespace?
Helper method to get the node’s namespace.
-
#node ⇒ Object
-
#nokogiri_children ⇒ Object
-
#read_attr(attr_name, to_call = nil) ⇒ Object
Helper method to read an attribute.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#string(other) ⇒ Object
-
#to_s ⇒ Object
-
#traverse(&block) ⇒ Object
-
#version ⇒ Object
-
#version=(other) ⇒ Object
-
#write_attr(attr_name, value, to_call = nil) ⇒ Object
Helper method to write a value to an attribute.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/ruby_speech/generic_element.rb', line 266
def method_missing(method_name, *args, &block)
if node.respond_to?(method_name)
return node.send method_name, *args, &block
end
const_name = method_name.to_s.sub('ssml_', '').gsub('_', '-')
if const = self.class.class_from_registration(const_name)
embed const.new(self.document, *args, &block)
elsif @block_binding && @block_binding.respond_to?(method_name)
@block_binding.send method_name, *args, &block
else
super
end
end
|
Instance Attribute Details
#parent ⇒ Object
66
67
68
|
# File 'lib/ruby_speech/generic_element.rb', line 66
def parent
@parent || super
end
|
Class Method Details
.included(klass) ⇒ Object
6
7
8
9
|
# File 'lib/ruby_speech/generic_element.rb', line 6
def self.included(klass)
klass.class_attribute :registered_ns, :registered_name, :defaults
klass.extend ClassMethods
end
|
Instance Method Details
#+(other) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/ruby_speech/generic_element.rb', line 104
def +(other)
new_doc = Nokogiri::XML::Document.new
self.class.new(new_doc).tap do |new_element|
new_doc.root = new_element.node
string_types = [String, Nokogiri::XML::Text]
include_spacing = string_types.include?(self.nokogiri_children.last.class) && string_types.include?(other.nokogiri_children.first.class)
if Nokogiri.jruby?
new_element.add_child self.clone.nokogiri_children
new_element << " " if include_spacing
new_element.add_child other.clone.nokogiri_children
else
new_element.add_child self.nokogiri_children.to_xml
new_element << " " if include_spacing
new_element.add_child other.nokogiri_children.to_xml
end
end
end
|
#<<(other) ⇒ Object
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/ruby_speech/generic_element.rb', line 148
def <<(other)
case other
when GenericElement
super other.node
when String
string other
else
super other
end
end
|
#==(o) ⇒ Object
252
253
254
|
# File 'lib/ruby_speech/generic_element.rb', line 252
def ==(o)
eql?(o)
end
|
#base_uri ⇒ String
Returns the base URI to which relative URLs are resolved.
93
94
95
|
# File 'lib/ruby_speech/generic_element.rb', line 93
def base_uri
read_attr 'xml:base'
end
|
#base_uri=(uri) ⇒ Object
100
101
102
|
# File 'lib/ruby_speech/generic_element.rb', line 100
def base_uri=(uri)
self['xml:base'] = uri
end
|
#build(atts, &block) ⇒ Object
76
77
78
79
80
|
# File 'lib/ruby_speech/generic_element.rb', line 76
def build(atts, &block)
mass_assign atts
block_return = eval_dsl_block &block
string block_return if block_return.is_a?(String) && !block_return.length.zero?
end
|
#children(type = nil, attributes = nil) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/ruby_speech/generic_element.rb', line 129
def children(type = nil, attributes = nil)
if type
expression = namespace_href ? 'ns:' : ''
expression << type.to_s
expression << '[' << attributes.inject([]) do |h, (key, value)|
h << "@#{key}='#{value}'"
end.join(',') << ']' if attributes
xpath expression, :ns => self.class.namespace
else
super()
end.map { |c| self.class.import c }
end
|
#clone ⇒ Object
178
179
180
|
# File 'lib/ruby_speech/generic_element.rb', line 178
def clone
self.class.import to_xml
end
|
#create_node ⇒ Object
256
257
258
259
260
|
# File 'lib/ruby_speech/generic_element.rb', line 256
def create_node
@node = Nokogiri::XML::Node.new self.class.registered_name, @doc
mass_assign self.class.defaults
@node
end
|
#embed(other) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/ruby_speech/generic_element.rb', line 159
def embed(other)
case other
when String
string other
when self.class.root_element
other.children.each do |child|
self << child
end
when self.class.module::Element
self << other
else
raise ArgumentError, "Can only embed a String or a #{self.class.module} element, not a #{other.class}"
end
end
|
#eql?(o, *fields) ⇒ Fixnum<-1,0,1>
Check that a set of fields are equal between nodes
247
248
249
|
# File 'lib/ruby_speech/generic_element.rb', line 247
def eql?(o, *fields)
o.is_a?(self.class) && ([:content, :children] + fields).all? { |f| self.__send__(f) == o.__send__(f) }
end
|
#eval_dsl_block(&block) ⇒ Object
123
124
125
126
127
|
# File 'lib/ruby_speech/generic_element.rb', line 123
def eval_dsl_block(&block)
return unless block_given?
@block_binding = eval "self", block.binding
instance_eval &block
end
|
#inherit(node) ⇒ Object
70
71
72
73
74
|
# File 'lib/ruby_speech/generic_element.rb', line 70
def inherit(node)
self.parent = node.parent
@node = node
self
end
|
#initialize(doc, atts = nil, &block) ⇒ Object
55
56
57
58
|
# File 'lib/ruby_speech/generic_element.rb', line 55
def initialize(doc, atts = nil, &block)
@doc = doc
build atts, &block if atts || block_given?
end
|
#inspect ⇒ String
234
235
236
|
# File 'lib/ruby_speech/generic_element.rb', line 234
def inspect
self.to_xml
end
|
#mass_assign(attrs) ⇒ Object
262
263
264
|
# File 'lib/ruby_speech/generic_element.rb', line 262
def mass_assign(attrs)
attrs.each_pair { |k, v| send :"#{k}=", v } if attrs
end
|
#namespace=(ns) ⇒ Object
#namespace=(ns) ⇒ Object
Attach a namespace to the node
214
215
216
217
218
219
220
221
222
|
# File 'lib/ruby_speech/generic_element.rb', line 214
def namespace=(namespaces)
case namespaces
when Nokogiri::XML::Namespace
super namespaces
when String
ns = self.add_namespace nil, namespaces
super ns
end
end
|
#namespace_href ⇒ XML::Namespace?
Helper method to get the node’s namespace
227
228
229
|
# File 'lib/ruby_speech/generic_element.rb', line 227
def namespace_href
namespace.href if namespace
end
|
#node ⇒ Object
62
63
64
|
# File 'lib/ruby_speech/generic_element.rb', line 62
def node
@node || create_node
end
|
#nokogiri_children ⇒ Object
144
145
146
|
# File 'lib/ruby_speech/generic_element.rb', line 144
def nokogiri_children
node.children
end
|
#read_attr(attr_name, to_call = nil) ⇒ Object
Helper method to read an attribute
the returned value
193
194
195
196
|
# File 'lib/ruby_speech/generic_element.rb', line 193
def read_attr(attr_name, to_call = nil)
val = self[attr_name]
val && to_call ? val.__send__(to_call) : val
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
281
282
283
|
# File 'lib/ruby_speech/generic_element.rb', line 281
def respond_to_missing?(method_name, include_private = false)
node.respond_to?(method_name, include_private) || super
end
|
#string(other) ⇒ Object
174
175
176
|
# File 'lib/ruby_speech/generic_element.rb', line 174
def string(other)
self << Nokogiri::XML::Text.new(other.to_s, document)
end
|
#to_s ⇒ Object
238
239
240
|
# File 'lib/ruby_speech/generic_element.rb', line 238
def to_s
to_xml
end
|
#traverse(&block) ⇒ Object
182
183
184
185
|
# File 'lib/ruby_speech/generic_element.rb', line 182
def traverse(&block)
nokogiri_children.each { |j| j.traverse &block }
block.call self
end
|
#version ⇒ Object
82
83
84
|
# File 'lib/ruby_speech/generic_element.rb', line 82
def version
read_attr :version
end
|
#version=(other) ⇒ Object
86
87
88
|
# File 'lib/ruby_speech/generic_element.rb', line 86
def version=(other)
self[:version] = other
end
|
#write_attr(attr_name, value, to_call = nil) ⇒ Object
Helper method to write a value to an attribute
202
203
204
|
# File 'lib/ruby_speech/generic_element.rb', line 202
def write_attr(attr_name, value, to_call = nil)
self[attr_name] = value && to_call ? value.__send__(to_call) : value
end
|