Class: OM::XML::Terminology::Builder
- Inherits:
-
Object
- Object
- OM::XML::Terminology::Builder
- 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
-
#namespaces ⇒ Object
Returns the value of attribute namespaces.
-
#schema ⇒ Object
Returns the value of attribute schema.
-
#term_builders ⇒ Object
readonly
Returns the value of attribute term_builders.
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(options = {}, root = nil) {|_self| ... } ⇒ Builder
constructor
Create a new Terminology Builder object.
-
#method_missing(method, *args, &block) ⇒ Object
:nodoc:.
-
#retrieve_term_builder(*args) ⇒ Object
Returns the TermBuilder corresponding to the given pointer.
-
#root(opts, &block) ⇒ Object
Set the root of the Terminology, along with namespace & schema info.
-
#root_term_builders ⇒ Object
Returns an array of Terms that have been marked as “root” terms.
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
35 36 37 38 39 40 41 42 |
# File 'lib/om/xml/terminology.rb', line 35 def initialize( = {}, root = nil, &block) @schema = .fetch(:schema,nil) @namespaces = .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:
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/om/xml/terminology.rb', line 67 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
#namespaces ⇒ Object
Returns the value of attribute namespaces.
23 24 25 |
# File 'lib/om/xml/terminology.rb', line 23 def namespaces @namespaces end |
#schema ⇒ Object
Returns the value of attribute schema.
23 24 25 |
# File 'lib/om/xml/terminology.rb', line 23 def schema @schema end |
#term_builders ⇒ Object (readonly)
Returns the value of attribute term_builders.
24 25 26 |
# File 'lib/om/xml/terminology.rb', line 24 def term_builders @term_builders end |
Instance Method Details
#build ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/om/xml/terminology.rb', line 104 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/om/xml/terminology.rb', line 89 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/om/xml/terminology.rb', line 45 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_builders ⇒ Object
Returns an array of Terms that have been marked as “root” terms
63 64 65 |
# File 'lib/om/xml/terminology.rb', line 63 def root_term_builders @term_builders.values.select {|term_builder| term_builder.settings[:is_root_term] == true } end |