Class: HumanQL::QueryGenerator
- Inherits:
-
Object
- Object
- HumanQL::QueryGenerator
- Defined in:
- lib/human-ql/query_generator.rb
Overview
Generate a Human Query Language String from an abstract syntax tree (AST). This allows query simplification (e.g. via TreeNormalizer) and re-writing queries.
Instance Attribute Summary collapse
-
#and ⇒ Object
The AND operator (if not default).
-
#colon ⇒ Object
COLON character used a prefix delimiter.
-
#default_op ⇒ Object
The default operator (:and or :or).
-
#lparen ⇒ Object
Left parenthesis character.
-
#lquote ⇒ Object
Left quote character for phrases.
-
#not ⇒ Object
The NOT operator.
-
#or ⇒ Object
The OR operator (if not default).
-
#precedence ⇒ Object
Hash of operators to precedence integer values, as per QueryParser#precedence.
-
#rparen ⇒ Object
Right parenthesis character.
-
#rquote ⇒ Object
Right quote character for phrases.
-
#space ⇒ Object
SPACE delimiter.
Instance Method Summary collapse
-
#generate(node) ⇒ Object
Given the root node of the AST, return a String in Human Query Language syntax.
-
#initialize(opts = {}) ⇒ QueryGenerator
constructor
Construct given options which are interpreted as attribute names to set.
-
#parser=(p) ⇒ Object
Set #default_op and #precedence from the given QueryParser, as a convenience.
Constructor Details
#initialize(opts = {}) ⇒ QueryGenerator
Construct given options which are interpreted as attribute names to set.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/human-ql/query_generator.rb', line 80 def initialize( opts = {} ) @and = ' and '.freeze @or = ' or '.freeze @not = '-'.freeze @space = ' '.freeze @lquote = @rquote = '"'.freeze @colon = ':'.freeze @lparen = '('.freeze @rparen = ')'.freeze @default_op = nil @precedence = nil opts.each do |name,val| send( name.to_s + '=', val ) end end |
Instance Attribute Details
#and ⇒ Object
The AND operator (if not default). Default: ‘ and ’
26 27 28 |
# File 'lib/human-ql/query_generator.rb', line 26 def and @and end |
#colon ⇒ Object
COLON character used a prefix delimiter. Default: ‘:’
50 51 52 |
# File 'lib/human-ql/query_generator.rb', line 50 def colon @colon end |
#default_op ⇒ Object
The default operator (:and or :or). If set, will output a :space instead of the operator. Default: nil
63 64 65 |
# File 'lib/human-ql/query_generator.rb', line 63 def default_op @default_op end |
#lparen ⇒ Object
Left parenthesis character. Default: ‘(’
54 55 56 |
# File 'lib/human-ql/query_generator.rb', line 54 def lparen @lparen end |
#lquote ⇒ Object
Left quote character for phrases. Default: ‘“’
42 43 44 |
# File 'lib/human-ql/query_generator.rb', line 42 def lquote @lquote end |
#not ⇒ Object
The NOT operator. Default: ‘-’
34 35 36 |
# File 'lib/human-ql/query_generator.rb', line 34 def not @not end |
#or ⇒ Object
The OR operator (if not default). Default: ‘ or ’
30 31 32 |
# File 'lib/human-ql/query_generator.rb', line 30 def or @or end |
#precedence ⇒ Object
Hash of operators to precedence integer values, as per QueryParser#precedence. If set, outputs parentheses only when precedence dictates that it is necessary. Default: nil
69 70 71 |
# File 'lib/human-ql/query_generator.rb', line 69 def precedence @precedence end |
#rparen ⇒ Object
Right parenthesis character. Default: ‘)’
58 59 60 |
# File 'lib/human-ql/query_generator.rb', line 58 def rparen @rparen end |
#rquote ⇒ Object
Right quote character for phrases. Default: ‘“’
46 47 48 |
# File 'lib/human-ql/query_generator.rb', line 46 def rquote @rquote end |
#space ⇒ Object
SPACE delimiter. Default: ‘ ’
38 39 40 |
# File 'lib/human-ql/query_generator.rb', line 38 def space @space end |
Instance Method Details
#generate(node) ⇒ Object
Given the root node of the AST, return a String in Human Query Language syntax.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/human-ql/query_generator.rb', line 99 def generate( node ) op,*args = node if ! node.is_a?( Array ) op elsif args.empty? nil else case op when :and terms_join( args, :and ) when :or terms_join( args, :or ) when :not @not + pwrap_gen( args[0], op ) when :phrase @lquote + args.join( @space ) + @rquote when String op + @colon + pwrap_gen( args[0], op ) else raise "Unsupported op: #{node.inspect}" end end end |
#parser=(p) ⇒ Object
Set #default_op and #precedence from the given QueryParser, as a convenience.
73 74 75 76 |
# File 'lib/human-ql/query_generator.rb', line 73 def parser=( p ) @default_op = p.default_op @precedence = p.precedence end |