Class: QDA::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/weft/query.rb

Defined Under Namespace

Classes: AND, AND_NOT, CodedByFunction, Function, LogicalExpression, OR, WordSearchFunction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(func = nil) ⇒ Query

Returns a new instance of Query.



6
7
8
# File 'lib/weft/query.rb', line 6

def initialize( func = nil )
  @root = func
end

Instance Attribute Details

#dbidObject

Returns the value of attribute dbid.



4
5
6
# File 'lib/weft/query.rb', line 4

def dbid
  @dbid
end

#rootObject

Returns the value of attribute root.



4
5
6
# File 'lib/weft/query.rb', line 4

def root
  @root
end

Instance Method Details

#add_expression(expr, arg_2 = nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/weft/query.rb', line 10

def add_expression(expr, arg_2 = nil)
  begin
    expr_class = self.class.const_get( expr.upcase.gsub(/\s+/, '_') )
  rescue NameError
    raise ArgumentError.new("Unknown expression '#{expr}'")
  end
  @root = expr_class.new(@root, arg_2)
end

#calculateObject



52
53
54
# File 'lib/weft/query.rb', line 52

def calculate()
  @root.calculate()  
end

#empty?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/weft/query.rb', line 19

def empty?()
  @root.nil?
end

#items(entry = @root) ⇒ Object



23
24
25
# File 'lib/weft/query.rb', line 23

def items(entry = @root)
  entry.to_a.flatten
end

#num_expressionsObject



48
49
50
# File 'lib/weft/query.rb', line 48

def num_expressions()
  items.grep(LogicalExpression).length
end

#num_functionsObject



44
45
46
# File 'lib/weft/query.rb', line 44

def num_functions()
  items.grep(Function).length
end

#to_sObject



56
57
58
# File 'lib/weft/query.rb', line 56

def to_s()
  @root.to_s()
end

#traverse(entry = @root) ⇒ Object



27
28
29
# File 'lib/weft/query.rb', line 27

def traverse(entry = @root)
  items(entry).each { | expr | yield(expr) }
end

#unroll(entry = @root) ⇒ Object

Yields a series of pairs of functions and expressions, in the evaluating left-to-right order of the query. Note that in the last iteration, the expr will be nil.

query.unroll { | func, expr | … }



37
38
39
40
41
42
# File 'lib/weft/query.rb', line 37

def unroll(entry = @root)
  things = items(entry)
  while func = things.shift
    yield func, things.shift
  end
end