class QuerySyntax::NestedScope

Nested Scopes are scopes used for nesting other related scopes within ()

Attributes

scopes[RW]

Public Class Methods

new(operator, *scopes) click to toggle source
Calls superclass method QuerySyntax::Scope::new
# File lib/query_syntax/scope/nested.rb, line 14
def initialize(operator, *scopes)
  super operator
  @scopes = scopes
end

Public Instance Methods

and!(args={}) click to toggle source
# 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() click to toggle source
# File lib/query_syntax/scope/nested.rb, line 88
def compact
  scopes.reject { |scope| scope.empty? }
end
compact!() click to toggle source
# File lib/query_syntax/scope/nested.rb, line 92
def compact!
  scopes = compact
  self
end
conditions() click to toggle source

Return the last scope’s criteria

# File lib/query_syntax/scope/nested.rb, line 68
def conditions
  scope.conditions
end
empty?() click to toggle source

Convenience

# File lib/query_syntax/scope/nested.rb, line 84
def empty?
  compact.empty?
end
nest!(operator) click to toggle source
# File lib/query_syntax/scope/nested.rb, line 30
def nest!(operator)
  @scopes = [NestedScope.new(operator, *scopes)]
  self
end
not!(args={}) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# File lib/query_syntax/scope/nested.rb, line 97
def push(other)
  @scopes << other
  self
end
scope() click to toggle source

Return the last scope so that we can continue adding to it

# 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) click to toggle source

Return a new CriteriaScope which is added to previous scopes

# File lib/query_syntax/scope/nested.rb, line 24
def scope!(operator)
  scope = CriteriaScope.new(operator)
  scopes << scope
  self
end
to_s() click to toggle source

Collapse all scopes to a string

# File lib/query_syntax/scope/nested.rb, line 105
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={}) click to toggle source

Add criteria to the last scope

# File lib/query_syntax/scope/nested.rb, line 75
def where!(args={})
  scope.where!(args)
  self
end