Class: OM::XML::Terminology

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

Overview

When you’re defining a Terminology, you will usually use a “Terminology Builder”:OM/XML/Terminology/Builder.html to create it Each line you put into a “Terminology Builder”:OM/XML/Terminology/Builder.html is passed to the constructor for a “Term Builder”:OM/XML/Term/Builder.html. See the “OM::XML::Term::Builder”:OM/XML/Term/Builder.html API docs for complete description of your options for defining each Term.

The most important thing to define in a Terminology is the root term. This is the place where you set namespaces and schemas for the Terminology If you do not set a namespace, the terminology will assume there is no namespace on the xml document.

Examples:

Define a Terminology with a root term “mods”, a default namespace of “www.loc.gov/mods/v3” and a schema of “www.loc.gov/standards/mods/v3/mods-3-2.xsd” (schema is optional)

terminology_builder = OM::XML::Terminology::Builder.new do |t|
  t.root(:path=>"mods", :xmlns=>"http://www.loc.gov/mods/v3", :schema=>"http://www.loc.gov/standards/mods/v3/mods-3-2.xsd")
end
terminology = terminology_builder.build

Defined Under Namespace

Classes: BadPointerError, Builder, CircularReferenceError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Terminology

Returns a new instance of Terminology.



130
131
132
133
134
# File 'lib/om/xml/terminology.rb', line 130

def initialize(options={})
  @schema = options.fetch(:schema,nil)
  @namespaces = options.fetch(:namespaces,{})
  @terms = {}
end

Instance Attribute Details

#namespacesObject

Terminology Class Definition



128
129
130
# File 'lib/om/xml/terminology.rb', line 128

def namespaces
  @namespaces
end

#schemaObject

Terminology Class Definition



128
129
130
# File 'lib/om/xml/terminology.rb', line 128

def schema
  @schema
end

#termsObject

Terminology Class Definition



128
129
130
# File 'lib/om/xml/terminology.rb', line 128

def terms
  @terms
end

Class Method Details

.pointers_to_flat_array(pointers, include_indices = true) ⇒ Object



293
294
295
# File 'lib/om/xml/terminology.rb', line 293

def self.pointers_to_flat_array(pointers, include_indices=true)
  OM.pointers_to_flat_array(pointers, include_indices)
end

.term_generic_name(*pointers) ⇒ Object



285
286
287
# File 'lib/om/xml/terminology.rb', line 285

def self.term_generic_name(*pointers)
  pointers_to_flat_array(pointers, false).join("_")
end

.term_hierarchical_name(*pointers) ⇒ Object



289
290
291
# File 'lib/om/xml/terminology.rb', line 289

def self.term_hierarchical_name(*pointers)
  pointers_to_flat_array(pointers, true).join("_")
end

Instance Method Details

#add_term(term) ⇒ Object

Add a term to the root of the terminology



137
138
139
# File 'lib/om/xml/terminology.rb', line 137

def add_term(term)
  @terms[term.name.to_sym] = term
end

#has_term?(*pointers) ⇒ Boolean

Returns true if the current terminology has a term defined at the location indicated by pointers array

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
# File 'lib/om/xml/terminology.rb', line 142

def has_term?(*pointers)
  begin
    retrieve_term(*OM.pointers_to_flat_array(pointers, false))
    return true
  rescue
    return false
  end
end

#retrieve_node(*args) ⇒ Object

This is very similar to retrieve_term, however it expands proxy paths out into their cannonical paths



181
182
183
184
185
186
187
188
# File 'lib/om/xml/terminology.rb', line 181

def retrieve_node(*args)
  current_term = terms[args.shift]
  if current_term.kind_of? OM::XML::NamedTermProxy
    args = (current_term.proxy_pointer + args).flatten
    current_term = terms[args.shift]
  end
  args.empty? ? current_term : retrieve_node_subsequent(args, current_term)
end

#retrieve_node_subsequent(args, context) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/om/xml/terminology.rb', line 169

def retrieve_node_subsequent(args, context)
  current_term = context.children[args.shift]
  if current_term.kind_of? OM::XML::NamedTermProxy
    args = (current_term.proxy_pointer + args).flatten
    current_term = context.children[args.shift]
  end
  args.empty? ? current_term : retrieve_node_subsequent(args, current_term)
end

#retrieve_term(*args) ⇒ Object

Returns the Term corresponding to the given pointer. Proxies are not expanded



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/om/xml/terminology.rb', line 153

def retrieve_term(*args)
  args_cp = args.dup
  current_term = terms[args_cp.delete_at(0)]
  if current_term.nil?
    raise OM::XML::Terminology::BadPointerError, "This Terminology does not have a root term defined that corresponds to \"#{args.first.inspect}\""
  else
    args_cp.each do |arg|
      current_term = current_term.retrieve_child(arg)
      if current_term.nil?
        raise OM::XML::Terminology::BadPointerError, "You attempted to retrieve a Term using this pointer: #{args.inspect} but no Term exists at that location. Everything is fine until \"#{arg.inspect}\", which doesn't exist."
      end
    end
  end
  return current_term
end

#root_termsObject

Returns an array of Terms that have been marked as “root” terms



252
253
254
# File 'lib/om/xml/terminology.rb', line 252

def root_terms
  terms.values.select {|term| term.is_root_term? }
end

#to_xml(options = {}, document = Nokogiri::XML::Document.new) ⇒ Nokogiri::XML::Document

Return an XML representation of the Terminology and its terms

Examples:

If :children=>false, skips rendering child Terms

terminology.to_xml(:children=>false)

You can provide your own Nokogiri document to insert the xml into

doc = Nokogiri::XML::Document.new
terminology.to_xml({}, document=doc)

Parameters:

  • options (Hash) (defaults to: {})

    the term will be added to it. If :children=>false, skips rendering child Terms

  • document (Nokogiri::XML::Document) (defaults to: Nokogiri::XML::Document.new)

    (optional) document to insert the term xml into

Returns:

  • (Nokogiri::XML::Document)


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/om/xml/terminology.rb', line 265

def to_xml(options={}, document=Nokogiri::XML::Document.new)
  builder = Nokogiri::XML::Builder.with(document) do |xml|
    xml.terminology {
      xml.schema schema
      xml.namespaces {
        namespaces.each_pair do |ns_name, ns_value|
          xml.namespace {
            xml.name ns_name
            xml.identifier ns_value
          }
        end
      }
      xml.terms
    }
  end
  document = builder.doc
  terms.values.each {|term| term.to_xml(options,document.xpath("//terms").first)}
  return document
end

#xml_builder_template(*term_pointers) ⇒ Object

Retrieves a Term corresponding to term_pointers and return the corresponding xml_builder_template for that term. The resulting xml_builder_template can be passed as a block into Nokogiri::XML::Builder.new

term_pointers point to the Term you want to generate a builder template for If the last term_pointer is a String or a Hash, it will be passed into the Term’s xml_builder_template method as extra_opts see also: Term.xml_builder_template



240
241
242
243
244
245
246
247
248
249
# File 'lib/om/xml/terminology.rb', line 240

def xml_builder_template(*term_pointers)
  extra_opts = {}

  if term_pointers.length > 1 && !term_pointers.last.kind_of?(Symbol)
    extra_opts = term_pointers.pop
  end

  term = retrieve_term(*term_pointers)
  return term.xml_builder_template(extra_opts)
end

#xpath_for(*pointers) ⇒ Object

Return the appropriate xpath query for retrieving nodes corresponding to the term identified by pointers. If the last argument is a String or a Hash, it will be used to add constraints to the resulting xpath query. If you provide an xpath query as the argument, it will be returne untouched.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/om/xml/terminology.rb', line 194

def xpath_for(*pointers)
  if pointers.length == 1 && pointers.first.instance_of?(String)
    return pointers.first
  end
  query_constraints = nil

  if pointers.length > 1 && !pointers.last.kind_of?(Symbol)
    query_constraints = pointers.pop
  end

  term = retrieve_node( *pointers )

  if !term.nil?
    if query_constraints.kind_of?(String)
      constraint_value = query_constraints
      xpath_template = term.xpath_constrained
      xpath_query = eval( '"' + xpath_template + '"' )
    elsif query_constraints.kind_of?(Hash) && !query_constraints.empty?
      key_value_pair = query_constraints.first
      constraint_value = key_value_pair.last
      xpath_template = term.children[key_value_pair.first].xpath_constrained
      xpath_query = eval( '"' + xpath_template + '"' )
    else
      xpath_query = term.xpath
    end
  else
    xpath_query = nil
  end
  xpath_query
end

#xpath_with_indexes(*pointers) ⇒ Object

Use the current terminology to generate an xpath with (optional) node indexes for each of the term pointers. Ex. terminology.xpath_with_indexes(:conference=>0, :role=>1, :text )

will yield an xpath like this: '//oxns:name[@type="conference"][1]/oxns:role[2]/oxns:roleTerm[@type="text"]'

Ex. terminology.xpath_with_indexes(:conference=>0, :role=>1, => 0 )

will yield an xpath like this: '//oxns:name[@type="conference"][1]/oxns:role[2]/oxns:roleTerm[@type="text"][1]'


230
231
232
# File 'lib/om/xml/terminology.rb', line 230

def xpath_with_indexes(*pointers)
  OM::XML::TermXpathGenerator.generate_xpath_with_indexes(self, *pointers)
end