Class: OM::XML::Terminology::Builder

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

Overview

Terminology::Builder Class Definition

When coding against Builders, remember that they rely on MethodMissing, so any time you call a method on the Builder that it doesn’t explicitly recognize, the Builder will add your method & arguments to the it’s settings and return itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, root = nil) {|_self| ... } ⇒ Builder

Create a new Terminology Builder object. options are sent to the top level Document that is being built. (not yet supported:) root can be a point in an existing Terminology that you want to add Mappers into

Building a document with a particular encoding for example:

Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
  ...
end

Yields:

  • (_self)

Yield Parameters:



36
37
38
39
40
41
42
43
# File 'lib/om/xml/terminology.rb', line 36

def initialize(options = {}, root = nil, &block)
  @schema = options.fetch(:schema,nil)
  @namespaces = options.fetch(:namespaces,{})
  @term_builders = {}
  @cur_term_builder = nil

  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/om/xml/terminology.rb', line 71

def method_missing method, *args, &block # :nodoc:
  parent_builder = @cur_term_builder
  @cur_term_builder = OM::XML::Term::Builder.new(method.to_s.sub(/[_!]$/, ''), self)

  # Attach to parent
  if parent_builder
    parent_builder.add_child @cur_term_builder
  else
    @term_builders [@cur_term_builder.name] = @cur_term_builder
  end

  # Apply options
  opts = args.shift
  @cur_term_builder.settings.merge!(opts) if opts

  # Parse children
  yield if block

  @cur_term_builder = parent_builder
end

Instance Attribute Details

#namespacesObject

Returns the value of attribute namespaces.



24
25
26
# File 'lib/om/xml/terminology.rb', line 24

def namespaces
  @namespaces
end

#schemaObject

Returns the value of attribute schema.



24
25
26
# File 'lib/om/xml/terminology.rb', line 24

def schema
  @schema
end

#term_buildersObject (readonly)

Returns the value of attribute term_builders.



25
26
27
# File 'lib/om/xml/terminology.rb', line 25

def term_builders
  @term_builders
end

Instance Method Details

#buildObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/om/xml/terminology.rb', line 113

def build
  terminology = OM::XML::Terminology.new(:schema=>@schema, :namespaces=>@namespaces)
  root_term_builders.each do |root_term_builder|
    root_term_builder.children = self.term_builders.dup
    root_term_builder.children.delete(root_term_builder.name)
  end
  @term_builders.each_value do |root_builder|
    terminology.add_term root_builder.build(terminology)
  end
  terminology
end

#extend_terminology {|_self| ... } ⇒ Object

Add additional terms into this terminology

Yields:

  • (_self)

Yield Parameters:



109
110
111
# File 'lib/om/xml/terminology.rb', line 109

def extend_terminology &block
  yield self if block_given?
end

#retrieve_term_builder(*args) ⇒ Object

Returns the TermBuilder corresponding to the given pointer.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/om/xml/terminology.rb', line 93

def retrieve_term_builder(*args)
  args_cp = args.dup
  current_term = @term_builders[args_cp.delete_at(0)]
  if current_term.nil?
    raise OM::XML::Terminology::BadPointerError, "This TerminologyBuilder does not have a root TermBuilder defined that corresponds to \"#{args.first.inspect}\""
  end
  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 TermBuilder using this pointer: #{args.inspect} but no TermBuilder exists at that location. Everything is fine until \"#{arg.inspect}\", which doesn't exist."
    end
  end
  return current_term
end

#root(opts, &block) ⇒ Object

Set the root of the Terminology, along with namespace & schema info



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/om/xml/terminology.rb', line 46

def root opts, &block
  @schema = opts.fetch(:schema,nil)
  opts.select {|k,v| k.to_s.include?("xmlns")}.each do |ns_pair|
    @namespaces[ns_pair.first.to_s] = ns_pair.last
    if ns_pair.first.to_s == "xmlns"
      @namespaces["oxns"] = ns_pair.last
    end
  end
  path = opts.fetch(:path,:root).to_s.sub(/[_!]$/, '')
  root_term_builder = OM::XML::Term::Builder.new(path).tap do |t|
    t.root_term= true
  end
  term_opts = opts.dup
  term_opts.delete(:schema)
  root_term_builder.settings.merge!(term_opts)
  @term_builders[root_term_builder.name] = root_term_builder

  return root_term_builder
end

#root_term_buildersObject

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



67
68
69
# File 'lib/om/xml/terminology.rb', line 67

def root_term_builders
  @term_builders.values.select {|term_builder| term_builder.settings[:is_root_term] == true }
end