Class: Nom::XML::TemplateRegistry

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

Constant Summary collapse

ACTIONS =
{
 :add_child => :self, :add_next_sibling => :parent, :add_previous_sibling => :parent, 
 :after => :parent, :before => :parent, :replace => :parent, :swap  => :parent
}

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, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nom/xml/template_registry.rb', line 61

def method_missing(sym,*args,&block)
  method = sym.to_s.sub(/_+$/,'').to_sym
  if @templates.has_key?(method)
    instantiate(sym,*args)
  elsif ACTIONS.has_key?(method)
    target_node = args.shift
    attach_node(method, target_node, ACTIONS[method], *args, &block)
  elsif method.to_s =~ /^(#{ACTIONS.keys.join('|')})_(.+)$/
    method = $1.to_sym
    template = $2.to_sym
    if ACTIONS.has_key?(method) and @templates.has_key?(template)
      target_node = args.shift
      attach_node(method, target_node, ACTIONS[method], template, *args, &block)
    end
  else
    super(sym,*args)
  end

end

Instance Method Details

#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
54
55
# 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.
    unless jruby?
      result.traverse { |node|
        if node.is_a?(Nokogiri::XML::CharacterData)
          node.namespace = nil
        end
      }
    end
  return result
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

#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