Class: Nom::XML::TemplateRegistry
- Inherits:
-
Object
- Object
- Nom::XML::TemplateRegistry
- Defined in:
- lib/nom/xml/template_registry.rb
Instance Method Summary collapse
-
#add_child(target_node, node_type, *args, &block) ⇒ Object
instantiate
a node and add it as a child of the [Nokogiri::XML::Node] specified bytarget_node
. -
#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 bytarget_node
. -
#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 bytarget_node
. -
#after(target_node, node_type, *args, &block) ⇒ Object
instantiate
a node and add it as a following sibling of the [Nokogiri::XML::Node] specified bytarget_node
. -
#before(target_node, node_type, *args, &block) ⇒ Object
instantiate
a node and add it as a preceding sibling of the [Nokogiri::XML::Node] specified bytarget_node
. -
#define(node_type, &block) ⇒ Object
Define an XML template.
-
#has_node_type?(node_type) ⇒ True
Check whether a particular node_type is defined.
-
#initialize ⇒ TemplateRegistry
constructor
A new instance of TemplateRegistry.
-
#instantiate(node_type, *args) ⇒ Object
Instantiate a detached, standalone node.
- #method_missing(sym, *args) ⇒ Object
- #methods ⇒ Object
-
#node_types ⇒ Array
List defined node_types.
-
#replace(target_node, node_type, *args, &block) ⇒ Object
instantiate
a node replace the [Nokogiri::XML::Node] specified bytarget_node
with it. -
#swap(target_node, node_type, *args, &block) ⇒ Object
instantiate
a node replace the [Nokogiri::XML::Node] specified bytarget_node
with it. -
#undefine(node_type) ⇒ Object
Undefine an XML template.
Constructor Details
#initialize ⇒ TemplateRegistry
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
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
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
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
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
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
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
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
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 |
#methods ⇒ Object
97 98 99 |
# File 'lib/nom/xml/template_registry.rb', line 97 def methods super + @templates.keys.collect { |k| k.to_s } end |
#node_types ⇒ Array
List defined node_types
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
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
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
23 24 25 26 |
# File 'lib/nom/xml/template_registry.rb', line 23 def undefine(node_type) @templates.delete(node_type) node_type end |