Class: Query::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/query/base.rb

Direct Known Subclasses

Wrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary, *args) ⇒ Base



7
8
9
10
11
12
13
14
# File 'lib/query/base.rb', line 7

def initialize(primary, *args)
  @primary_table = _make_table(primary)

  @arel = Arel::SelectManager.new(ActiveRecord::Base).
    from(@primary_table)

  _configure(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



38
39
40
41
# File 'lib/query/base.rb', line 38

def method_missing(sym, *args, &block)
  arel.send(sym, *args, &block)
  self
end

Instance Attribute Details

#arelObject (readonly)

Returns the value of attribute arel.



5
6
7
# File 'lib/query/base.rb', line 5

def arel
  @arel
end

#primary_tableObject (readonly)

Returns the value of attribute primary_table.



5
6
7
# File 'lib/query/base.rb', line 5

def primary_table
  @primary_table
end

Instance Method Details

#_configure(*args) ⇒ Object



51
52
53
# File 'lib/query/base.rb', line 51

def _configure(*args)
  # overridden by subclasses for per-query configuration
end

#_make_table(value) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/query/base.rb', line 43

def _make_table(value)
  case value
  when Arel::Table then value
  when Arel::Nodes::TableAlias then value
  else Arel::Table.new(value)
  end
end

#allObject



21
22
23
24
# File 'lib/query/base.rb', line 21

def all
  arel.project(primary_table[Arel.star])
  self
end

#as(name) ⇒ Object

ensures #as returns an Arel node, and not the Query object (so that it plays nice with our custom #with method, above).



28
29
30
# File 'lib/query/base.rb', line 28

def as(name)
  arel.as(name)
end

#reproject(*projections) ⇒ Object



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

def reproject(*projections)
  arel.projections = projections
  self
end

#to_sqlObject Also known as: to_s



32
33
34
# File 'lib/query/base.rb', line 32

def to_sql
  @arel.to_sql
end