Class: QuerySyntax::CriteriaScope
- Extended by:
- Forwardable
- Defined in:
- lib/query_syntax/scope/criteria.rb
Overview
Criteria Scopes are scopes containing key:value conditions ex.: where!(a:0, b:1).where!(a:2) becomes (a=0 OR a=2) AND b=1
Instance Attribute Summary collapse
-
#conditions ⇒ Object
Returns the value of attribute conditions.
Attributes inherited from Scope
Instance Method Summary collapse
-
#initialize(operator, conditions = {}) ⇒ CriteriaScope
constructor
A new instance of CriteriaScope.
-
#to_s ⇒ Object
Collapse all conditions to a string.
- #where!(args = {}) ⇒ Object
Constructor Details
#initialize(operator, conditions = {}) ⇒ CriteriaScope
Returns a new instance of CriteriaScope.
14 15 16 17 18 |
# File 'lib/query_syntax/scope/criteria.rb', line 14 def initialize(operator, conditions={}) super operator @conditions = conditions where!(conditions) end |
Instance Attribute Details
#conditions ⇒ Object
Returns the value of attribute conditions.
20 21 22 |
# File 'lib/query_syntax/scope/criteria.rb', line 20 def conditions @conditions end |
Instance Method Details
#to_s ⇒ Object
Collapse all conditions to a string
37 38 39 40 41 42 43 44 |
# File 'lib/query_syntax/scope/criteria.rb', line 37 def to_s value = @conditions.map do |key,values| query = values.map { |value| "#{key}:#{value}" }.join(" OR ") values.count > 1 ? "(#{query})" : query end.join(" AND ") conditions.count > 1 ? "(#{value})" : value end |
#where!(args = {}) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/query_syntax/scope/criteria.rb', line 25 def where!(args={}) args.each do |k,v| next if v.nil? @conditions[k] = [] unless conditions.key?(k) @conditions[k].concat(Array(v)).uniq! end self end |