Module: RubySpeech::GenericElement::ClassMethods

Defined in:
lib/ruby_speech/generic_element.rb

Constant Summary collapse

@@registrations =
{}

Instance Method Summary collapse

Instance Method Details

#class_from_registration(name) ⇒ Class?

Find the class to use given the name and namespace of a stanza

Parameters:

  • name (#to_s)

    the name to lookup

Returns:

  • (Class, nil)

    the class appropriate for the name


33
34
35
# File 'lib/ruby_speech/generic_element.rb', line 33

def class_from_registration(name)
  @@registrations[[name.to_s, namespace]]
end

#import(node) ⇒ Object

Import an XML::Node to the appropriate class

Looks up the class the node should be then creates it based on the elements of the XML::Node

Parameters:

  • node (XML::Node)

    the node to import

Returns:

  • the appropriate object based on the node name and namespace


43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_speech/generic_element.rb', line 43

def import(node)
  node = Nokogiri::XML.parse(node, nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).root unless node.is_a?(Nokogiri::XML::Node) || node.is_a?(GenericElement)
  return node.content if node.is_a?(Nokogiri::XML::Text)
  klass = class_from_registration node.node_name
  if klass && klass != self
    klass.import node
  else
    new(node.document).inherit node
  end
end

#register(name) ⇒ Object

Register a new stanza class to a name and/or namespace

This registers a namespace that is used when looking up the class name of the object to instantiate when a new stanza is received

Parameters:

  • name (#to_s)

    the name of the node


22
23
24
25
26
# File 'lib/ruby_speech/generic_element.rb', line 22

def register(name)
  self.registered_name = name.to_s
  self.registered_ns = namespace
  @@registrations[[self.registered_name, self.registered_ns]] = self
end