Class: UserQuery::Parser
- Inherits:
-
Object
- Object
- UserQuery::Parser
- 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
-
#default_join_op ⇒ Object
Returns the value of attribute default_join_op.
-
#default_literal_op ⇒ Object
Returns the value of attribute default_literal_op.
-
#expr ⇒ Object
Returns the value of attribute expr.
-
#keywords ⇒ Object
Returns the value of attribute keywords.
-
#type ⇒ Object
Returns the value of attribute type.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #<<(str) ⇒ Object
-
#initialize(*opts) ⇒ Parser
constructor
A new instance of Parser.
- #now ⇒ Object
-
#now=(t) ⇒ Object
Configuration.
- #parse(str) ⇒ Object
- #this_year ⇒ Object
- #this_year=(y) ⇒ Object
- #today ⇒ Object
- #today=(t) ⇒ Object
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_op ⇒ Object
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_op ⇒ Object
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 |
#expr ⇒ Object
Returns the value of attribute expr.
25 26 27 |
# File 'lib/user_query/parser.rb', line 25 def expr @expr end |
#keywords ⇒ Object
Returns the value of attribute keywords.
23 24 25 |
# File 'lib/user_query/parser.rb', line 23 def keywords @keywords end |
#type ⇒ Object
Returns the value of attribute type.
24 25 26 |
# File 'lib/user_query/parser.rb', line 24 def type @type end |
#verbose ⇒ Object
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 |
#now ⇒ Object
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_year ⇒ Object
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 |
#today ⇒ Object
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 |