Class: ExistDB::XQLFactory::Path
- Inherits:
-
Object
- Object
- ExistDB::XQLFactory::Path
- Defined in:
- lib/existdb/xql_factory.rb
Overview
This is used by the XQLFactory for remapping nodes
This could possibly be simplified by using XSLT if there was a Ruby DSL for XSLT
The primary goal is to keep the barrier to entry low for rubyists who have limited XML experience
Instance Attribute Summary collapse
-
#to_a ⇒ Object
readonly
Returns the value of attribute to_a.
-
#to_s ⇒ Object
readonly
Returns the value of attribute to_s.
Instance Method Summary collapse
- #<=>(path) ⇒ Object
- #common(path) ⇒ Object
- #context ⇒ Object
-
#initialize(path) ⇒ Path
constructor
A new instance of Path.
- #name ⇒ Object
- #switch_context(path) ⇒ Object
Constructor Details
#initialize(path) ⇒ Path
Returns a new instance of Path.
21 22 23 24 25 26 27 28 29 |
# File 'lib/existdb/xql_factory.rb', line 21 def initialize(path) if path.is_a?(String) then @to_s = path @to_a = path.split('/') elsif path.is_a?(Array) then @to_s = path.join('/') @to_a = path end end |
Instance Attribute Details
#to_a ⇒ Object (readonly)
Returns the value of attribute to_a.
20 21 22 |
# File 'lib/existdb/xql_factory.rb', line 20 def to_a @to_a end |
#to_s ⇒ Object (readonly)
Returns the value of attribute to_s.
20 21 22 |
# File 'lib/existdb/xql_factory.rb', line 20 def to_s @to_s end |
Instance Method Details
#<=>(path) ⇒ Object
39 40 41 |
# File 'lib/existdb/xql_factory.rb', line 39 def <=>(path) to_a <=> path.to_a end |
#common(path) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/existdb/xql_factory.rb', line 43 def common(path) a = Array.new self.to_a.each_index do |i| if self.to_a[i].nil? or path.to_a[i].nil? or self.to_a[i] != path.to_a[i] then break else a << self.to_a[i] end end a << '' return Path.new(a) end |
#context ⇒ Object
35 36 37 |
# File 'lib/existdb/xql_factory.rb', line 35 def context @to_a[0..-2] end |
#name ⇒ Object
31 32 33 |
# File 'lib/existdb/xql_factory.rb', line 31 def name @to_a.last end |
#switch_context(path) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/existdb/xql_factory.rb', line 58 def switch_context(path) output = String.new common_path = self.common(path) diff = self.context.size - common_path.context.size if diff > 0 then output << self.context.last(diff).reverse.map{ |node| "</#{node}>"}.join end diff = path.context.size - common_path.context.size if diff > 0 then output << path.context.last(diff).map{ |node| "<#{node}>"}.join end return output end |