Class: OM::XML::DynamicNode
- Inherits:
-
Object
- Object
- OM::XML::DynamicNode
- Defined in:
- lib/om/xml/dynamic_node.rb
Overview
Provides a natural syntax for using OM Terminologies to access values from xml Documents
Note: All of these examples assume that @article is an instance of OM::Samples::ModsArticle. Look at that file to see the Terminology.
Defined Under Namespace
Classes: AddressedNode
Instance Attribute Summary (collapse)
-
- (Object) addressed_node
Returns the value of attribute addressed_node.
-
- (Object) index
Returns the value of attribute index.
-
- (Object) key
Returns the value of attribute key.
-
- (Object) parent
Returns the value of attribute parent.
-
- (Object) term
Returns the value of attribute term.
Instance Method Summary (collapse)
- - (Object) ==(other)
- - (Object) delete
- - (Boolean) eql?(other)
-
- (DynamicNode) initialize(key, index, document, term, parent = nil)
constructor
TODO a real term object in here would make it easier to lookup.
- - (Object) inspect
- - (Object) method_missing(name, *args, &block)
- - (Object) new_update_node(name, args)
- - (Object) new_update_node_with_index(name, args)
- - (Object) nodeset
-
- (Object) retrieve_addressed_node
This is very similar to Terminology#retrieve_term, however it expands proxy paths out into their cannonical paths.
- - (Object) term_child_by_name(term, name)
- - (Object) to_pointer
- - (Object) val
- - (Object) val=(args)
- - (Object) xpath
Constructor Details
- (DynamicNode) initialize(key, index, document, term, parent = nil)
TODO a real term object in here would make it easier to lookup
33 34 35 36 37 38 39 |
# File 'lib/om/xml/dynamic_node.rb', line 33 def initialize(key, index, document, term, parent=nil) ##TODO a real term object in here would make it easier to lookup self.key = key self.index = index @document = document self.term = term self.parent = parent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(name, *args, &block)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/om/xml/dynamic_node.rb', line 41 def method_missing (name, *args, &block) if /=$/.match(name.to_s) new_update_node(name, args) elsif args.length > 1 new_update_node_with_index(name, args) else child = term_child_by_name(term.nil? ? parent.term : term, name) if child OM::XML::DynamicNode.new(name, args.first, @document, child, self) else val.send(name, *args, &block) end end end |
Instance Attribute Details
- (Object) addressed_node
Returns the value of attribute addressed_node
32 33 34 |
# File 'lib/om/xml/dynamic_node.rb', line 32 def addressed_node @addressed_node end |
- (Object) index
Returns the value of attribute index
32 33 34 |
# File 'lib/om/xml/dynamic_node.rb', line 32 def index @index end |
- (Object) key
Returns the value of attribute key
32 33 34 |
# File 'lib/om/xml/dynamic_node.rb', line 32 def key @key end |
- (Object) parent
Returns the value of attribute parent
32 33 34 |
# File 'lib/om/xml/dynamic_node.rb', line 32 def parent @parent end |
- (Object) term
Returns the value of attribute term
32 33 34 |
# File 'lib/om/xml/dynamic_node.rb', line 32 def term @term end |
Instance Method Details
- (Object) ==(other)
115 116 117 |
# File 'lib/om/xml/dynamic_node.rb', line 115 def ==(other) val == other end |
- (Object) delete
107 108 109 |
# File 'lib/om/xml/dynamic_node.rb', line 107 def delete nodeset.delete end |
- (Boolean) eql?(other)
119 120 121 |
# File 'lib/om/xml/dynamic_node.rb', line 119 def eql?(other) self == other end |
- (Object) inspect
111 112 113 |
# File 'lib/om/xml/dynamic_node.rb', line 111 def inspect val.inspect end |
- (Object) new_update_node(name, args)
56 57 58 59 60 61 |
# File 'lib/om/xml/dynamic_node.rb', line 56 def new_update_node(name, args) modified_name = name.to_s.chop.to_sym child = term.retrieve_term(modified_name) node = OM::XML::DynamicNode.new(modified_name, nil, @document, child, self) node.val=args end |
- (Object) new_update_node_with_index(name, args)
63 64 65 66 67 68 |
# File 'lib/om/xml/dynamic_node.rb', line 63 def new_update_node_with_index(name, args) index = args.shift child = term.retrieve_term(name) node = OM::XML::DynamicNode.new(name, index, @document, child, self) node.val=args end |
- (Object) nodeset
101 102 103 104 105 |
# File 'lib/om/xml/dynamic_node.rb', line 101 def nodeset query = xpath trim_text = !query.index("text()").nil? return @document.find_by_xpath(query) end |
- (Object) retrieve_addressed_node
This is very similar to Terminology#retrieve_term, however it expands proxy paths out into their cannonical paths
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 |
# File 'lib/om/xml/dynamic_node.rb', line 153 def retrieve_addressed_node() chain = [] if parent chain += parent.retrieve_addressed_node() end if (self.index) ### This is an index node = AddressedNode.new(key, term.xpath_relative, self) node.xpath = OM::XML::TermXpathGenerator.add_node_index_predicate(node.xpath, index) chain << node elsif (term.kind_of? NamedTermProxy) proxy = term.proxy_pointer.dup first = proxy.shift p = @document.class.terminology.retrieve_node(*first) chain << AddressedNode.new(p, p.xpath_relative, self) while !proxy.empty? first = proxy.shift p = p.retrieve_term(first) chain << AddressedNode.new(p, p.xpath_relative, self) end else chain << AddressedNode.new(key, term.xpath_relative, self) end chain end |
- (Object) term_child_by_name(term, name)
86 87 88 89 90 91 92 |
# File 'lib/om/xml/dynamic_node.rb', line 86 def term_child_by_name(term, name) if (term.kind_of? NamedTermProxy) @document.class.terminology.retrieve_node(*(term.proxy_pointer.dup << name)) else term.retrieve_term(name) end end |
- (Object) to_pointer
123 124 125 126 127 128 129 |
# File 'lib/om/xml/dynamic_node.rb', line 123 def to_pointer if self.index parent.nil? ? [{key => index}] : parent.to_pointer << {key => index} else ### A pointer parent.nil? ? [key] : parent.to_pointer << key end end |
- (Object) val
94 95 96 97 98 99 |
# File 'lib/om/xml/dynamic_node.rb', line 94 def val query = xpath trim_text = !query.index("text()").nil? val = @document.find_by_xpath(query).collect {|node| (trim_text ? node.text.strip : node.text) } term.deserialize(val) end |
- (Object) val=(args)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/om/xml/dynamic_node.rb', line 70 def val=(args) @document.ng_xml_will_change! new_values = term.sanitize_new_values(args.first) new_values.keys.sort { |a,b| a.to_i <=> b.to_i }.each do |y| z = new_values[y] ## If we pass something that already has an index on it, we should be able to add it. if @document.find_by_xpath(xpath)[y.to_i].nil? || y.to_i == -1 parent_pointer = parent ? parent.to_pointer : nil @document.term_values_append(:parent_select=> parent_pointer,:parent_index=>0,:template=>to_pointer,:values=>z) else @document.term_value_update(xpath, y.to_i, z) end end end |
- (Object) xpath
131 132 133 134 135 136 137 138 139 |
# File 'lib/om/xml/dynamic_node.rb', line 131 def xpath if parent.nil? @document.class.terminology.xpath_with_indexes(*(to_pointer << {})) ### last element is always filters else chain = retrieve_addressed_node( ) '//' + chain.map { |n| n.xpath}.join('/') end end |