Class: ExistDB::XQLFactory::XQLFactory

Inherits:
Object
  • Object
show all
Includes:
Meta
Defined in:
lib/existdb/xql_factory.rb

Overview

This is an attempt to create a Ruby DSL for the Most Common XQuery use cases.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Meta

#initialize_with_options, #named_or_ordered_options

Constructor Details

#initialize(*options) {|_self| ... } ⇒ XQLFactory

Accepts Ordered or Named Parameters for any of the attr_accessors

Ordered Options – :doc, :start, :max, :sort

E.g.

xql = XQLFactory.new("doc('http://example.com')", 5, 10, :node_xpath => '//a').xquery

Would create an XQuery statement to find the fifth through tenth links on example.com

See also ExistDB::XQLFactory.Build as a shorthand way of calling this.

Yields:

  • (_self)

Yield Parameters:



96
97
98
99
# File 'lib/existdb/xql_factory.rb', line 96

def initialize(*options)
  initialize_with_options(options, [:doc, :start, :max, :sort])
  yield self if block_given?
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def doc
  @doc
end

#ftqueryObject

Returns the value of attribute ftquery.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def ftquery
  @ftquery
end

#maxObject

Returns the value of attribute max.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def max
  @max
end

#node_remapObject

Returns the value of attribute node_remap.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def node_remap
  @node_remap
end

#node_xpathObject

Returns the value of attribute node_xpath.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def node_xpath
  @node_xpath
end

#return_attributesObject

Returns the value of attribute return_attributes.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def return_attributes
  @return_attributes
end

#return_tagObject

Returns the value of attribute return_tag.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def return_tag
  @return_tag
end

#searchObject

Returns the value of attribute search.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def search
  @search
end

#sortObject

Returns the value of attribute sort.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def sort
  @sort
end

#startObject

Returns the value of attribute start.



78
79
80
# File 'lib/existdb/xql_factory.rb', line 78

def start
  @start
end

Instance Method Details

#init_statementObject



139
140
141
142
143
# File 'lib/existdb/xql_factory.rb', line 139

def init_statement
  raise "doc attribute required" if doc.nil? or doc.to_s.empty?
  raise "node_xpath attribute required" if node_xpath.nil? or node_xpath.empty?
  "let $scope := for $node in #{doc if doc.is_a?(String)}#{node_xpath}#{search_statement} #{sort_statement} return $node\n"
end

#limit_statementObject



101
102
103
104
105
106
107
# File 'lib/existdb/xql_factory.rb', line 101

def limit_statement
  if start or max then
    "let $scope := subsequence($scope, #{start || 1}, #{max || 'count($scope)'})\n"
  else
    ''
  end
end

#node_remap_statementObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/existdb/xql_factory.rb', line 168

def node_remap_statement
  return '' if node_remap.nil? or node_remap.empty?
  output = "let $scope := for $node in $scope return\n"
  current_context = Path.new('')
  node_remap.to_a.map{|a|
    a[1] = Path.new(a[1]); a 
  }.sort.each do |a|
    (src_path, dest_path) = a
    output << current_context.switch_context(dest_path)
    current_context = dest_path
    output << "<#{dest_path.name}>{ $node#{src_path} }</#{dest_path.name}>"
  end
  output << current_context.switch_context( Path.new('') ) 
  output << "\n"
  return output
end

#return_attributes_statementObject



161
162
163
164
165
166
# File 'lib/existdb/xql_factory.rb', line 161

def return_attributes_statement
  return '' if return_attributes.nil? or return_attributes.empty?
  ' ' + return_attributes.keys.map{ |key|
    "#{key}=#{return_attributes[key].to_s.inspect}"
  }.join(' ')
end

#return_statementObject



153
154
155
156
157
158
159
# File 'lib/existdb/xql_factory.rb', line 153

def return_statement
  if return_attributes_statement.empty? and @return_tag.nil? then
    "return $scope"
  else
    "return <#{return_tag}#{return_attributes_statement}> { $scope } </#{return_tag}>"
  end
end

#search_statementObject



134
135
136
137
# File 'lib/existdb/xql_factory.rb', line 134

def search_statement
  "[contains(., #{ search.inspect })]" if search
  "[ft:query(., #{ ftquery.inspect })]" if ftquery
end

#sort_direction(dir) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/existdb/xql_factory.rb', line 123

def sort_direction(dir)
  dir = dir.to_s.downcase
  if %w|asc ascending|.include?(dir)
      return :ascending
  elsif %|desc descending|.include?(dir)
      return :descending
  else
      return :ascending
  end
end

#sort_statementObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/existdb/xql_factory.rb', line 109

def sort_statement
  if sort then
    if sort.is_a?(String) then
      "order by $node/#{sort}"
    elsif sort.is_a?(Hash) then
      "order by $node/#{sort.keys.first} #{sort_direction(sort.values.first)}"
    elsif sort.is_a?(Array) then
      "order by " + sort.map{ |h| "$node/#{h.keys.first} #{sort_direction(h.values.first)}" }.join(', ')
    end
  else
    ''
  end
end

#xqueryObject



185
186
187
# File 'lib/existdb/xql_factory.rb', line 185

def xquery
  init_statement + limit_statement + node_remap_statement + return_statement
end