Class: ActiveRecord::Query

Inherits:
BasicObject
Defined in:
lib/active_record/query.rb,
lib/active_record/query/context.rb,
lib/active_record/query/subject.rb

Defined Under Namespace

Classes: And, Context, Or, Subject

Constant Summary collapse

CONTEXT =
{
  :or  => Or,
  :and => And
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, context) ⇒ Query

Returns a new instance of Query.



15
16
17
18
19
# File 'lib/active_record/query.rb', line 15

def initialize(table, context)
  @table = table
  @nodes = []
  @stack = [CONTEXT[context].new]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



21
22
23
24
25
26
27
# File 'lib/active_record/query.rb', line 21

def method_missing(name, *args, &block)
  if args.empty? && !block
    Subject.new(self, name)
  else
    ::Kernel.raise ::ArgumentError
  end
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



13
14
15
# File 'lib/active_record/query.rb', line 13

def table
  @table
end

Instance Method Details

#<<(node) ⇒ Object



45
46
47
# File 'lib/active_record/query.rb', line 45

def <<(node)
  @stack.last << node
end

#andObject



33
34
35
36
37
# File 'lib/active_record/query.rb', line 33

def and
  @stack << And.new
  yield
  self << @stack.pop.arel
end

#arelObject



49
50
51
# File 'lib/active_record/query.rb', line 49

def arel
  @stack.last.arel
end

#inspectObject



53
54
55
# File 'lib/active_record/query.rb', line 53

def inspect
  "#<ActiveRecord::Query table=#{table.inspect}>"
end

#orObject



39
40
41
42
43
# File 'lib/active_record/query.rb', line 39

def or
  @stack << Or.new
  yield
  self << @stack.pop.arel
end

#respond_to?(name, include_private = nil) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_record/query.rb', line 29

def respond_to?(name, include_private = nil)
  true
end