Class: UserQuery::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/user_query/parser.rb

Overview

Do:

require 'currency'

for :type => :money

Defined Under Namespace

Classes: Error, SyntaxError

Constant Summary collapse

@@now =
nil
@@today =
nil
@@this_year =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Parser



28
29
30
31
32
33
34
35
36
# File 'lib/user_query/parser.rb', line 28

def initialize(*opts)
  self.verbose = false
  self.default_join_op = :and
  self.default_literal_op = :like
  self.keywords = [ ]
  self.type = :string
  opts = Hash[*opts]
  opts.each{|k, v| self.send("#{k}=", v)}
end

Instance Attribute Details

#default_join_opObject

Returns the value of attribute default_join_op.



21
22
23
# File 'lib/user_query/parser.rb', line 21

def default_join_op
  @default_join_op
end

#default_literal_opObject

Returns the value of attribute default_literal_op.



22
23
24
# File 'lib/user_query/parser.rb', line 22

def default_literal_op
  @default_literal_op
end

#exprObject

Returns the value of attribute expr.



25
26
27
# File 'lib/user_query/parser.rb', line 25

def expr
  @expr
end

#keywordsObject

Returns the value of attribute keywords.



23
24
25
# File 'lib/user_query/parser.rb', line 23

def keywords
  @keywords
end

#typeObject

Returns the value of attribute type.



24
25
26
# File 'lib/user_query/parser.rb', line 24

def type
  @type
end

#verboseObject

Returns the value of attribute verbose.



26
27
28
# File 'lib/user_query/parser.rb', line 26

def verbose
  @verbose
end

Instance Method Details

#<<(str) ⇒ Object



64
65
66
# File 'lib/user_query/parser.rb', line 64

def <<(str)
  parse(str)
end

#nowObject



73
74
75
# File 'lib/user_query/parser.rb', line 73

def now
  @now || @@now || Time.new
end

#now=(t) ⇒ Object

Configuration



70
71
72
# File 'lib/user_query/parser.rb', line 70

def now=(t)
  @now = t
end

#parse(str) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/user_query/parser.rb', line 38

def parse(str)
  return nil if str.nil?

  str = str.to_s

  @level = 0
  @input_original = str.clone
  @input = str.clone
  @lex_next = nil
  @expr = lex_peek && parse_top_level

  eat_whitespace

  @lex_extra = lex_peek
  if @lex_extra
    raise SyntaxError, "extra characters for #{type.to_s} field at #{@lex_extra[1].inspect}"
  end

  if @verbose
    q_sql = UserQuery::Generator.new.sql(@expr)
    $stderr.puts "#{str.inspect} =>\n  " + @expr.inspect + " =>\n  " + q_sql.inspect
  end

  @expr
end

#this_yearObject



81
82
83
# File 'lib/user_query/parser.rb', line 81

def this_year
  @this_year || @@this_year || now.year
end

#this_year=(y) ⇒ Object



77
78
79
80
# File 'lib/user_query/parser.rb', line 77

def this_year=(y)
  @this_year = y.respond_to?(:year) ? y.year : y
  y
end

#todayObject



89
90
91
92
93
94
95
96
# File 'lib/user_query/parser.rb', line 89

def today
  t = @today || @@today 
  unless t
    t = now
    t = Time.utc(t.year, t.month, t.day, 0, 0, 0, 0)
  end
  t
end

#today=(t) ⇒ Object



85
86
87
88
# File 'lib/user_query/parser.rb', line 85

def today=(t)
  t = t && Time.utc(t.year, t.month, t.day, 0, 0, 0, 0)
  @today = t
end