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:



25
26
27
28
29
30
31
32
# File 'lib/om/xml/terminology.rb', line 25

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:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/om/xml/terminology.rb', line 57

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.



13
14
15
# File 'lib/om/xml/terminology.rb', line 13

def namespaces
  @namespaces
end

#schemaObject

Returns the value of attribute schema.



13
14
15
# File 'lib/om/xml/terminology.rb', line 13

def schema
  @schema
end

#term_buildersObject (readonly)

Returns the value of attribute term_builders.



14
15
16
# File 'lib/om/xml/terminology.rb', line 14

def term_builders
  @term_builders
end

Instance Method Details

#buildObject



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

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

#retrieve_term_builder(*args) ⇒ Object

Returns the TermBuilder corresponding to the given pointer.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/om/xml/terminology.rb', line 79

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/om/xml/terminology.rb', line 35

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
  root_term_builder = OM::XML::Term::Builder.new(opts.fetch(:path,:root).to_s.sub(/[_!]$/, '')).is_root_term(true)
  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



53
54
55
# File 'lib/om/xml/terminology.rb', line 53

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