Class: OM::XML::Terminology
- Inherits:
-
Object
- Object
- OM::XML::Terminology
- Defined in:
- lib/om/xml/terminology.rb
Defined Under Namespace
Classes: BadPointerError, Builder, CircularReferenceError
Instance Attribute Summary collapse
-
#namespaces ⇒ Object
Terminology Class Definition.
-
#schema ⇒ Object
Terminology Class Definition.
-
#terms ⇒ Object
Terminology Class Definition.
Class Method Summary collapse
- .pointers_to_flat_array(pointers, include_indices = true) ⇒ Object
- .term_generic_name(*pointers) ⇒ Object
- .term_hierarchical_name(*pointers) ⇒ Object
Instance Method Summary collapse
-
#add_term(term) ⇒ Object
Add a term to the root of the terminology.
-
#has_term?(*pointers) ⇒ Boolean
Returns true if the current terminology has a term defined at the location indicated by
pointers
array. -
#initialize(options = {}) ⇒ Terminology
constructor
A new instance of Terminology.
-
#retrieve_term(*args) ⇒ Object
Returns the Term corresponding to the given pointer.
-
#root_terms ⇒ Object
Returns an array of Terms that have been marked as “root” terms.
-
#xml_builder_template(*term_pointers) ⇒ Object
Retrieves a Term corresponding to
term_pointers
and return the corresponding xml_builder_template for that term. -
#xpath_for(*pointers) ⇒ Object
Return the appropriate xpath query for retrieving nodes corresponding to the term identified by
pointers
. -
#xpath_with_indexes(*pointers) ⇒ Object
Use the current terminology to generate an xpath with (optional) node indexes for each of the term pointers.
Constructor Details
#initialize(options = {}) ⇒ Terminology
Returns a new instance of Terminology.
111 112 113 114 115 |
# File 'lib/om/xml/terminology.rb', line 111 def initialize(={}) @schema = .fetch(:schema,nil) @namespaces = .fetch(:namespaces,{}) @terms = {} end |
Instance Attribute Details
#namespaces ⇒ Object
Terminology Class Definition
109 110 111 |
# File 'lib/om/xml/terminology.rb', line 109 def namespaces @namespaces end |
#schema ⇒ Object
Terminology Class Definition
109 110 111 |
# File 'lib/om/xml/terminology.rb', line 109 def schema @schema end |
#terms ⇒ Object
Terminology Class Definition
109 110 111 |
# File 'lib/om/xml/terminology.rb', line 109 def terms @terms end |
Class Method Details
.pointers_to_flat_array(pointers, include_indices = true) ⇒ Object
221 222 223 |
# File 'lib/om/xml/terminology.rb', line 221 def self.pointers_to_flat_array(pointers, include_indices=true) OM.pointers_to_flat_array(pointers, include_indices) end |
.term_generic_name(*pointers) ⇒ Object
213 214 215 |
# File 'lib/om/xml/terminology.rb', line 213 def self.term_generic_name(*pointers) pointers_to_flat_array(pointers, false).join("_") end |
.term_hierarchical_name(*pointers) ⇒ Object
217 218 219 |
# File 'lib/om/xml/terminology.rb', line 217 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
118 119 120 |
# File 'lib/om/xml/terminology.rb', line 118 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
123 124 125 126 127 128 129 130 |
# File 'lib/om/xml/terminology.rb', line 123 def has_term?(*pointers) begin retrieve_term(*OM.pointers_to_flat_array(pointers, false)) return true rescue return false end end |
#retrieve_term(*args) ⇒ Object
Returns the Term corresponding to the given pointer.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/om/xml/terminology.rb', line 133 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_terms ⇒ Object
Returns an array of Terms that have been marked as “root” terms
209 210 211 |
# File 'lib/om/xml/terminology.rb', line 209 def root_terms terms.values.select {|term| term.is_root_term? } 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
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/om/xml/terminology.rb', line 197 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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/om/xml/terminology.rb', line 152 def xpath_for(*pointers) if pointers.length == 1 && pointers.first.instance_of?(String) xpath_query = pointers.first else query_constraints = nil if pointers.length > 1 && !pointers.last.kind_of?(Symbol) query_constraints = pointers.pop end term = retrieve_term( *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 end return 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"]'
187 188 189 |
# File 'lib/om/xml/terminology.rb', line 187 def xpath_with_indexes(*pointers) OM::XML::TermXpathGenerator.generate_xpath_with_indexes(self, *pointers) end |