Class: Devlin::Query
- Inherits:
-
Object
- Object
- Devlin::Query
- Defined in:
- lib/devlin/query.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#select ⇒ Object
readonly
Returns the value of attribute select.
Instance Method Summary collapse
-
#initialize(parent, q) ⇒ Query
constructor
A new instance of Query.
-
#result ⇒ Object
This method returns the resulting relation to calculate the given query.
Constructor Details
#initialize(parent, q) ⇒ Query
Returns a new instance of Query.
5 6 7 8 9 10 11 12 |
# File 'lib/devlin/query.rb', line 5 def initialize(parent, q) @parent = parent @query = YAML.load(q) @scope = parent.scope(query['scope']) @select = query['select'] @conditions = query['conditions'] @group = query['group'] end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
3 4 5 |
# File 'lib/devlin/query.rb', line 3 def conditions @conditions end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
3 4 5 |
# File 'lib/devlin/query.rb', line 3 def group @group end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/devlin/query.rb', line 3 def query @query end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
3 4 5 |
# File 'lib/devlin/query.rb', line 3 def scope @scope end |
#select ⇒ Object (readonly)
Returns the value of attribute select.
3 4 5 |
# File 'lib/devlin/query.rb', line 3 def select @select end |
Instance Method Details
#result ⇒ Object
This method returns the resulting relation to calculate the given query
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/devlin/query.rb', line 16 def result res = @scope.relation res = res.select(self.select.map { |c| @scope.column(c).select_definition }) @conditions.each do |col, val| col, op = col.split('.') res = case op when 'g' res.where(["#{@scope.column(col).definition}>?", val]) when 'geq' res.where(["#{@scope.column(col).definition}>=?", val]) when 'l' res.where(["#{@scope.column(col).definition}<?", val]) when 'leq' res.where(["#{@scope.column(col).definition}<=?", val]) when 'in' res.where(["#{@scope.column(col).definition} IN (?)", val]) else res.where(["#{@scope.column(col).definition}=?", val]) end end res = res.group(self.group.map { |c| @scope.column(c).definition }) res end |