Class: OM::XML::TemplateRegistry

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

Overview

Extend an OM::XML::Document with reusable templates, then use them to add content to instance documents.

Example:

require 'om/samples/mods_article'

class OM::Samples::ModsArticle
  define_template :personalName do |xml, family, given, address|
    xml.name(:type => 'personal') do
      xml.namePart(:type => 'family') { xml.text(family) }
      xml.namePart(:type => 'given') { xml.text(given) }
      xml.namePart(:type => 'termsOfAddress') { xml.text(address) }
    end
  end

  define_template :role do |xml, text, attrs|
    xml.role do
      attrs = { :type => 'text' }.merge(attrs)
      xml.roleTerm(attrs) { xml.text(text) }
    end
  end
end

mods = OM::Samples::ModsArticle.from_xml(File.read('./spec/fixtures/CBF_MODS/ARS0025_016.xml'))

mods.add_previous_sibling_node([:person => 0], :personalName, 'Shmoe', 'Joseph', 'Dr.') { |person|
  person.add_child(mods.template(:role, 'author', :authority => 'marcrelator'))
  person.add_child(mods.template(:role, 'sub', :authority => 'local', :type => 'code'))
  person
}

Instance Method Summary collapse

Constructor Details

#initializeTemplateRegistry

Returns a new instance of TemplateRegistry.



35
36
37
# File 'lib/om/xml/template_registry.rb', line 35

def initialize
  @templates = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/om/xml/template_registry.rb', line 133

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]



89
90
91
# File 'lib/om/xml/template_registry.rb', line 89

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]



95
96
97
# File 'lib/om/xml/template_registry.rb', line 95

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]



101
102
103
# File 'lib/om/xml/template_registry.rb', line 101

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



107
108
109
# File 'lib/om/xml/template_registry.rb', line 107

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



113
114
115
# File 'lib/om/xml/template_registry.rb', line 113

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



43
44
45
46
47
48
49
50
# File 'lib/om/xml/template_registry.rb', line 43

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]



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

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



76
77
78
79
80
81
82
83
84
85
# File 'lib/om/xml/template_registry.rb', line 76

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



129
130
131
# File 'lib/om/xml/template_registry.rb', line 129

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

#node_typesArray

List defined node_types

Returns:

  • (Array)

    of node_type symbols.



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

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]



119
120
121
# File 'lib/om/xml/template_registry.rb', line 119

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



125
126
127
# File 'lib/om/xml/template_registry.rb', line 125

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



55
56
57
58
# File 'lib/om/xml/template_registry.rb', line 55

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