class OM::XML::TermBuilder::Builder

Attributes

children[RW]
name[RW]
settings[RW]
terminology_builder[RW]

Public Class Methods

new(name, terminology_builder=nil) click to toggle source
# File lib/om/xml/term_builder.rb, line 16
def initialize(name, terminology_builder=nil)
  @name = name.to_sym
  @terminology_builder = terminology_builder
  @settings = {:required=>false, :type=>:string}
  @children = {}
end

Public Instance Methods

add_child(child) click to toggle source
# File lib/om/xml/term_builder.rb, line 23
def add_child(child)
  @children[child.name] = child
end
attributes=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 137
def attributes= val
  @settings[:attributes] = val
end
build(terminology=nil) click to toggle source

Builds a new OM::XML::Term based on the Builder object's current settings If no path has been provided, uses the Builder object's name as the term's path Recursively builds any children, appending the results as children of the Term that's being built. @param [OM::XML::Terminology] terminology that this Term is being built for

# File lib/om/xml/term_builder.rb, line 94
def build(terminology=nil)
  self.resolve_refs!
  if settings.has_key?(:proxy)
    term = OM::XML::NamedTermProxy.new(self.name, self.settings[:proxy], terminology, self.settings)
  else
    term = OM::XML::Term.new(self.name, {}, terminology)

    self.settings.each do |name, values|
      if term.respond_to?(name.to_s+"=")
        term.instance_variable_set("@#{name}", values)
      end
    end
    @children.each_value do |child|
      term.add_child child.build(terminology)
    end
    term.generate_xpath_queries!
  end

  return term
end
default_content_path=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 157
def default_content_path= val
  @settings[:default_content_path] = val
end
index_as=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 125
def index_as= val
  @settings[:index_as] = val
end
lookup_refs(nodes_visited=[]) click to toggle source
# File lib/om/xml/term_builder.rb, line 31
def lookup_refs(nodes_visited=[])
  result = []
  if @settings[:ref]
    # Fail if we do not have terminology builder
    if self.terminology_builder.nil?
      raise "Cannot perform lookup_ref for the #{self.name} builder.  It doesn't have a reference to any terminology builder"
    end
    target = self.terminology_builder.retrieve_term_builder(*@settings[:ref])

    # Fail on circular references and return an intelligible error message
    if nodes_visited.include?(target)
      nodes_visited << self
      nodes_visited << target
      trail = ""
      nodes_visited.each_with_index do |node, z|
        trail << node.name.inspect
        unless z == nodes_visited.length-1
          trail << " => "
        end
      end
      raise OM::XML::Terminology::CircularReferenceError, "Circular reference in Terminology: #{trail}"
    end
    result << target
    result.concat( target.lookup_refs(nodes_visited << self) )
  end
  return result
end
path=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 149
def path= val
  @settings[:path] = val
end
proxy=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 141
def proxy= val
  @settings[:proxy] = val
end
ref=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 133
def ref= val
  @settings[:ref] = val
end
required=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 129
def required= val
  @settings[:required] = val
end
resolve_refs!() click to toggle source

If a :ref value has been set, looks up the target of that ref and merges the target's settings & children with the current builder's settings & children operates recursively, so it is possible to apply refs that in turn refer to other nodes.

# File lib/om/xml/term_builder.rb, line 61
def resolve_refs!
  name_of_last_ref = nil
  lookup_refs.each_with_index do |ref,z|
    @settings = two_layer_merge(@settings, ref.settings)
    @children.merge!(ref.children)
    name_of_last_ref = ref.name
  end
  if @settings[:path].nil? && !name_of_last_ref.nil?
    @settings[:path] = name_of_last_ref.to_s
  end
  @settings.delete :ref
  return self
end
retrieve_child(child_name) click to toggle source
# File lib/om/xml/term_builder.rb, line 27
def retrieve_child(child_name)
  child = @children.fetch(child_name, nil)
end
root_term=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 121
def root_term= val
  @settings[:is_root_term] = val
end
two_layer_merge(downstream_hash, upstream_hash) click to toggle source

Returns a new Hash that merges downstream_hash with upstream_hash similar to calling upstream_hash.merge(downstream_hash) only it also merges any internal values that are themselves Hashes.

# File lib/om/xml/term_builder.rb, line 78
def two_layer_merge(downstream_hash, upstream_hash)
  up = upstream_hash.dup
  dn = downstream_hash.dup
  up.each_pair do |setting_name, value|
    if value.kind_of?(Hash) && downstream_hash.has_key?(setting_name)
      dn[setting_name] = value.merge(downstream_hash[setting_name])
      up.delete(setting_name)
    end
  end
  return up.merge(dn)
end
type(value) click to toggle source

We have to add this method so it will play nice with ruby 1.8.7

# File lib/om/xml/term_builder.rb, line 116
def type value
  @settings[:type] = value
  return self
end
type=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 145
def type= val
  @settings[:type] = val
end
variant_of=(val) click to toggle source
# File lib/om/xml/term_builder.rb, line 153
def variant_of= val
  @settings[:variant_of] = val
end