Class: BabySqueel::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/baby_squeel/table.rb

Direct Known Subclasses

Association, DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Table

Returns a new instance of Table.



13
14
15
16
17
# File 'lib/baby_squeel/table.rb', line 13

def initialize(scope)
  @scope = scope
  @_table = scope.arel_table
  @_join = Arel::Nodes::InnerJoin
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



103
104
105
106
107
108
109
# File 'lib/baby_squeel/table.rb', line 103

def method_missing(name, *args, &block)
  if !args.empty? || block_given?
    super
  else
    resolve(name) || super
  end
end

Instance Attribute Details

#_joinObject

Returns the value of attribute _join.



11
12
13
# File 'lib/baby_squeel/table.rb', line 11

def _join
  @_join
end

#_onObject

Returns the value of attribute _on.



11
12
13
# File 'lib/baby_squeel/table.rb', line 11

def _on
  @_on
end

#_tableObject

Returns the value of attribute _table.



11
12
13
# File 'lib/baby_squeel/table.rb', line 11

def _table
  @_table
end

Instance Method Details

#[](key) ⇒ Object

See Arel::Table#[]



20
21
22
# File 'lib/baby_squeel/table.rb', line 20

def [](key)
  Nodes::Attribute.new(self, key)
end

#_arel(associations = []) ⇒ Object

This method will be invoked by BabySqueel::Nodes::unwrap. When called, there are two possible outcomes:

  1. Join explicitly using an on clause.

  2. Resolve the assocition’s join clauses using ActiveRecord.



81
82
83
84
85
86
87
# File 'lib/baby_squeel/table.rb', line 81

def _arel(associations = [])
  if _on
    _join.new(_table, _on)
  else
    JoinDependency.new(@scope, associations).constraints
  end
end

#alias(alias_name) ⇒ Object

Alias a table. This is only possible when joining an association explicitly.



36
37
38
# File 'lib/baby_squeel/table.rb', line 36

def alias(alias_name)
  clone.alias! alias_name
end

#alias!(alias_name) ⇒ Object



40
41
42
43
# File 'lib/baby_squeel/table.rb', line 40

def alias!(alias_name)
  self._table = _table.alias(alias_name)
  self
end

#association(name) ⇒ Object

Constructs a new BabySqueel::Association. Raises an exception if the association is not found.



26
27
28
29
30
31
32
# File 'lib/baby_squeel/table.rb', line 26

def association(name)
  if reflection = @scope.reflect_on_association(name)
    Association.new(self, reflection)
  else
    raise AssociationNotFoundError.new(@scope, name)
  end
end

#innerObject

Instruct the table to be joined with an INNER JOIN.



56
57
58
# File 'lib/baby_squeel/table.rb', line 56

def inner
  clone.inner!
end

#inner!Object



60
61
62
63
# File 'lib/baby_squeel/table.rb', line 60

def inner!
  self._join = Arel::Nodes::InnerJoin
  self
end

#on(node) ⇒ Object

Specify an explicit join.



66
67
68
# File 'lib/baby_squeel/table.rb', line 66

def on(node)
  clone.on! node
end

#on!(node) ⇒ Object



70
71
72
73
# File 'lib/baby_squeel/table.rb', line 70

def on!(node)
  self._on = Arel::Nodes::On.new(node)
  self
end

#outerObject

Instruct the table to be joined with an INNER JOIN.



46
47
48
# File 'lib/baby_squeel/table.rb', line 46

def outer
  clone.outer!
end

#outer!Object



50
51
52
53
# File 'lib/baby_squeel/table.rb', line 50

def outer!
  self._join = Arel::Nodes::OuterJoin
  self
end