Class: Atomsphere::Query::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/atomsphere/query/builder.rb,
lib/atomsphere/query/builder/group.rb,
lib/atomsphere/query/builder/property.rb

Overview

DSL for onstructing an Atomsphere::Query

See Also:

  • Atomsphere#query

Defined Under Namespace

Classes: Group, Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_type) ⇒ Builder

Returns a new instance of Builder.



10
11
12
# File 'lib/atomsphere/query/builder.rb', line 10

def initialize(object_type)
  @query = object_type.nil? ? Query.new : Query.new(object_type)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Allows for defining of SimpleExpressions at the top level of the query. Creates a top level GroupingExpression if none yet exists to contain it.



31
32
33
34
35
36
# File 'lib/atomsphere/query/builder.rb', line 31

def method_missing m, *args, &block
  @query.filter = GroupingExpression.new(:and) unless @query.filter
  Group.new(@query.filter).instance_eval do
    send(m, *args, &block)
  end
end

Instance Attribute Details

#queryAtomsphere::Query (readonly)

output query object

Returns:



7
8
9
# File 'lib/atomsphere/query/builder.rb', line 7

def query
  @query
end

Instance Method Details

#group(operator, &block) ⇒ Object

Define a GroupingExpression at the top level of the query. If called more than once, subsequent calls create a grouping expression within the first created GroupingExpression.



17
18
19
20
21
22
23
24
25
26
# File 'lib/atomsphere/query/builder.rb', line 17

def group operator, &block
  new_group = GroupingExpression.new(operator)
  Group.new(new_group).instance_eval(&block)

  if @query.filter
    @query.filter.nested_expression << new_group
  else
    @query.filter = new_group
  end
end