Class: ExistDB::XQLFactory::Path

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_aObject (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_sObject (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

#contextObject



35
36
37
# File 'lib/existdb/xql_factory.rb', line 35

def context
  @to_a[0..-2]
end

#nameObject



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