Class: Nom::XML::TemplateRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/nom/xml/template_registry.rb

Instance Method Summary collapse

Constructor Details

#initializeTemplateRegistry

Returns a new instance of TemplateRegistry.



3
4
5
# File 'lib/nom/xml/template_registry.rb', line 3

def initialize
  @templates = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/nom/xml/template_registry.rb', line 101

def method_missing(sym,*args)
  sym = sym.to_s.sub(/_$/,'').to_sym
  if @templates.has_key?(sym)
    instantiate(sym,*args)
  else
    super(sym,*args)
  end
end

Instance Method Details

#add_child(target_node, node_type, *args, &block) ⇒ Object

instantiate a node and add it as a child of the [Nokogiri::XML::Node] specified by target_node

Returns:

  • the new [Nokogiri::XML::Node]



57
58
59
# File 'lib/nom/xml/template_registry.rb', line 57

def add_child(target_node, node_type, *args, &block)
  attach_node(:add_child, target_node, :self, node_type, *args, &block)
end

#add_next_sibling(target_node, node_type, *args, &block) ⇒ Object

instantiate a node and add it as a following sibling of the [Nokogiri::XML::Node] specified by target_node

Returns:

  • the new [Nokogiri::XML::Node]



63
64
65
# File 'lib/nom/xml/template_registry.rb', line 63

def add_next_sibling(target_node, node_type, *args, &block)
  attach_node(:add_next_sibling, target_node, :parent, node_type, *args, &block)
end

#add_previous_sibling(target_node, node_type, *args, &block) ⇒ Object

instantiate a node and add it as a preceding sibling of the [Nokogiri::XML::Node] specified by target_node

Returns:

  • the new [Nokogiri::XML::Node]



69
70
71
# File 'lib/nom/xml/template_registry.rb', line 69

def add_previous_sibling(target_node, node_type, *args, &block)
  attach_node(:add_previous_sibling, target_node, :parent, node_type, *args, &block)
end

#after(target_node, node_type, *args, &block) ⇒ Object

instantiate a node and add it as a following sibling of the [Nokogiri::XML::Node] specified by target_node

Returns:

  • target_node



75
76
77
# File 'lib/nom/xml/template_registry.rb', line 75

def after(target_node, node_type, *args, &block)
  attach_node(:after, target_node, :parent, node_type, *args, &block)
end

#before(target_node, node_type, *args, &block) ⇒ Object

instantiate a node and add it as a preceding sibling of the [Nokogiri::XML::Node] specified by target_node

Returns:

  • target_node



81
82
83
# File 'lib/nom/xml/template_registry.rb', line 81

def before(target_node, node_type, *args, &block)
  attach_node(:before, target_node, :parent, node_type, *args, &block)
end

#define(node_type, &block) ⇒ Object

Define an XML template

Parameters:

  • a (Symbol)

    node_type key to associate with this template

  • a (Block)

    block that will receive a [Nokogiri::XML::Builder] object and any arbitrary parameters passed to instantiate

Returns:

  • the node_type Symbol



11
12
13
14
15
16
17
18
# File 'lib/nom/xml/template_registry.rb', line 11

def define(node_type, &block)
  unless node_type.is_a?(Symbol)
    raise TypeError, "Registered node type must be a Symbol (e.g., :person)"
  end

  @templates[node_type] = block
  node_type
end

#has_node_type?(node_type) ⇒ True

Check whether a particular node_type is defined

Parameters:

  • the (Symbol)

    node_type key to check

Returns:

  • (True)

    or [False]



31
32
33
# File 'lib/nom/xml/template_registry.rb', line 31

def has_node_type?(node_type)
  @templates.has_key?(node_type)
end

#instantiate(node_type, *args) ⇒ Object

Instantiate a detached, standalone node

Parameters:

  • the (Symbol)

    node_type to instantiate

  • additional

    arguments to pass to the template



44
45
46
47
48
49
50
51
52
53
# File 'lib/nom/xml/template_registry.rb', line 44

def instantiate(node_type, *args)
  result = create_detached_node(nil, node_type, *args)
  # Strip namespaces from text and CDATA nodes. Stupid Nokogiri.
  result.traverse { |node|
    if node.is_a?(Nokogiri::XML::CharacterData)
      node.namespace = nil
    end
  }
  return result
end

#methodsObject



97
98
99
# File 'lib/nom/xml/template_registry.rb', line 97

def methods
  super + @templates.keys.collect { |k| k.to_s }
end

#node_typesArray

List defined node_types

Returns:

  • (Array)

    of node_type symbols.



37
38
39
# File 'lib/nom/xml/template_registry.rb', line 37

def node_types
  @templates.keys
end

#replace(target_node, node_type, *args, &block) ⇒ Object

instantiate a node replace the [Nokogiri::XML::Node] specified by target_node with it

Returns:

  • the new [Nokogiri::XML::Node]



87
88
89
# File 'lib/nom/xml/template_registry.rb', line 87

def replace(target_node, node_type, *args, &block)
  attach_node(:replace, target_node, :parent, node_type, *args, &block)
end

#swap(target_node, node_type, *args, &block) ⇒ Object

instantiate a node replace the [Nokogiri::XML::Node] specified by target_node with it

Returns:

  • target_node



93
94
95
# File 'lib/nom/xml/template_registry.rb', line 93

def swap(target_node, node_type, *args, &block)
  attach_node(:swap, target_node, :parent, node_type, *args, &block)
end

#undefine(node_type) ⇒ Object

Undefine an XML template

Parameters:

  • the (Symbol)

    node_type key of the template to undefine

Returns:

  • the node_type Symbol



23
24
25
26
# File 'lib/nom/xml/template_registry.rb', line 23

def undefine(node_type)
  @templates.delete(node_type)
  node_type
end