Class: QuerySyntax::NestedScope
- Extended by:
- Forwardable
- Defined in:
- lib/query_syntax/scope/nested.rb
Overview
Nested Scopes are scopes used for nesting other related scopes within ()
Direct Known Subclasses
Instance Attribute Summary collapse
-
#scopes ⇒ Object
Returns the value of attribute scopes.
Attributes inherited from Scope
Instance Method Summary collapse
- #and!(args = {}) ⇒ Object
- #compact ⇒ Object
- #compact! ⇒ Object
-
#conditions ⇒ Object
Return the last scope’s criteria.
-
#empty? ⇒ Boolean
Convenience.
-
#initialize(operator, *scopes) ⇒ NestedScope
constructor
A new instance of NestedScope.
- #nest!(operator) ⇒ Object
- #not!(args = {}) ⇒ Object
- #or!(args = {}) ⇒ Object
- #push(other) ⇒ Object
-
#scope ⇒ Object
Return the last scope so that we can continue adding to it.
-
#scope!(operator) ⇒ Object
Return a new CriteriaScope which is added to previous scopes.
-
#to_s ⇒ Object
Collapse all scopes to a string.
-
#where!(args = {}) ⇒ Object
Add criteria to the last scope.
Constructor Details
#initialize(operator, *scopes) ⇒ NestedScope
Returns a new instance of NestedScope.
14 15 16 17 |
# File 'lib/query_syntax/scope/nested.rb', line 14 def initialize(operator, *scopes) super operator @scopes = scopes end |
Instance Attribute Details
#scopes ⇒ Object
Returns the value of attribute scopes.
19 20 21 |
# File 'lib/query_syntax/scope/nested.rb', line 19 def scopes @scopes end |
Instance Method Details
#and!(args = {}) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/query_syntax/scope/nested.rb', line 42 def and!(args={}) if args.empty? then nest!("AND") else scope!("AND").where!(args) end self end |
#compact ⇒ Object
88 89 90 |
# File 'lib/query_syntax/scope/nested.rb', line 88 def compact scopes.reject { |scope| scope.empty? } end |
#compact! ⇒ Object
92 93 94 95 |
# File 'lib/query_syntax/scope/nested.rb', line 92 def compact! scopes = compact self end |
#conditions ⇒ Object
Return the last scope’s criteria
68 69 70 |
# File 'lib/query_syntax/scope/nested.rb', line 68 def conditions scope.conditions end |
#empty? ⇒ Boolean
Convenience
84 85 86 |
# File 'lib/query_syntax/scope/nested.rb', line 84 def empty? compact.empty? end |
#nest!(operator) ⇒ Object
30 31 32 33 |
# File 'lib/query_syntax/scope/nested.rb', line 30 def nest!(operator) @scopes = [NestedScope.new(operator, *scopes)] self end |
#not!(args = {}) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/query_syntax/scope/nested.rb', line 35 def not!(args={}) if args.empty? then nest!("NOT") else scope!("NOT").where!(args) end self end |
#or!(args = {}) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/query_syntax/scope/nested.rb', line 49 def or!(args={}) if args.empty? then nest!("OR") else scope!("OR").where!(args) end self end |
#push(other) ⇒ Object
97 98 99 100 101 |
# File 'lib/query_syntax/scope/nested.rb', line 97 def push(other) @scopes << other scope!(operator) self end |
#scope ⇒ Object
Return the last scope so that we can continue adding to it
59 60 61 62 63 |
# File 'lib/query_syntax/scope/nested.rb', line 59 def scope scope!(operator) if @scopes.empty? scope!("AND") if @scopes.last.is_a?(NestedScope) @scopes.last end |
#scope!(operator) ⇒ Object
Return a new CriteriaScope which is added to previous scopes
24 25 26 27 28 |
# File 'lib/query_syntax/scope/nested.rb', line 24 def scope!(operator) scope = CriteriaScope.new(operator) scopes << scope self end |
#to_s ⇒ Object
Collapse all scopes to a string
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/query_syntax/scope/nested.rb', line 106 def to_s values = compact.map do |scope| value = scope.to_s value = if scope.is_a?(NestedScope) scope.compact.count > 1 ? "(#{value})" : value else value end [scope.operator, value] end values = values.flatten.compact values.shift values.join(" ") end |
#where!(args = {}) ⇒ Object
Add criteria to the last scope
75 76 77 78 |
# File 'lib/query_syntax/scope/nested.rb', line 75 def where!(args={}) scope.where!(args) self end |