Module: RubySpeech::GenericElement

Included in:
RubySpeech::GRXML::Element, SSML::Element
Defined in:
lib/ruby_speech/generic_element.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#parentObject



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
      # TODO: This is yucky because it requires serialization
      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_uriString

Returns the base URI to which relative URLs are resolved.

Returns:

  • (String)

    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

Parameters:

  • uri (String)

    the base URI to which relative URLs are resolved



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

#cloneObject



178
179
180
# File 'lib/ruby_speech/generic_element.rb', line 178

def clone
  self.class.import to_xml
end

#create_nodeObject



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

Parameters:

  • other (Node)

    the other node to compare against

  • fields (*#to_s)

    the set of fields to compare

Returns:

  • (Fixnum<-1,0,1>)


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

#inspectString

The node as XML

Returns:

  • (String)

    XML representation of the node



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

Overloads:

  • #namespace=(ns) ⇒ Object

    Attach an already created XML::Namespace

    Parameters:

    • ns (XML::Namespace)

      the namespace object

  • #namespace=(ns) ⇒ Object

    Create a new namespace and attach it

    Parameters:

    • ns (String)

      the namespace uri



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_hrefXML::Namespace?

Helper method to get the node’s namespace

Returns:

  • (XML::Namespace, nil)

    The node’s namespace object if it exists



227
228
229
# File 'lib/ruby_speech/generic_element.rb', line 227

def namespace_href
  namespace.href if namespace
end

#nodeObject



62
63
64
# File 'lib/ruby_speech/generic_element.rb', line 62

def node
  @node || create_node
end

#nokogiri_childrenObject



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

Parameters:

  • attr_name (#to_sym)

    the name of the attribute

  • to_call (String, Symbol, nil) (defaults to: nil)

    the name of the method to call on

Returns:

  • nil or the 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

Returns:

  • (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_sObject



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

#versionObject



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

Parameters:

  • attr_name (#to_sym)

    the name of the attribute

  • value (#to_s)

    the value to set the attribute to



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