Class: Squeel::Nodes::Stub

Inherits:
Object
  • Object
show all
Includes:
Operators, PredicateMethods
Defined in:
lib/squeel/nodes/stub.rb

Overview

Stub nodes are basically a container for a Symbol that can have handy predicate methods and operators defined on it since doing so on Symbol will incur the nerdrage of many.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Operators

#*, #+, #-, #/, #op

Constructor Details

#initialize(symbol) ⇒ Stub

Create a new Stub.

Parameters:

  • symbol (Symbol)

    The symbol that this Stub contains



33
34
35
# File 'lib/squeel/nodes/stub.rb', line 33

def initialize(symbol)
  @symbol = symbol
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#node_nameKeyPath #node_name(klass) ⇒ KeyPath

Create a KeyPath when any undefined method is called on a Stub.

Overloads:

  • #node_nameKeyPath

    Creates a new KeyPath with this Stub as the base and the method_name as the endpoint

    Returns:

  • #node_name(klass) ⇒ KeyPath

    Creates a new KeyPath with this Stub as the base and a polymorphic belongs_to join as the endpoint

    Parameters:

    • klass (Class)

      The polymorphic class for the join

    Returns:



66
67
68
69
70
71
72
73
74
75
# File 'lib/squeel/nodes/stub.rb', line 66

def method_missing(method_id, *args)
  super if method_id == :to_ary
  if args.empty?
    KeyPath.new(self, method_id)
  elsif (args.size == 1) && (Class === args[0])
    KeyPath.new(self, Join.new(method_id, Arel::InnerJoin, args[0]))
  else
    KeyPath.new(self, Nodes::Function.new(method_id, args))
  end
end

Instance Attribute Details

#symbolSymbol (readonly)

Returns The symbol contained by this stub.

Returns:

  • (Symbol)

    The symbol contained by this stub



29
30
31
# File 'lib/squeel/nodes/stub.rb', line 29

def symbol
  @symbol
end

Instance Method Details

#ascOrder

Create an ascending Order node with this Stub’s symbol as its expression

Returns:

  • (Order)

    The new Order node



87
88
89
# File 'lib/squeel/nodes/stub.rb', line 87

def asc
  Order.new self.symbol, 1
end

#descOrder

Create a descending Order node with this Stub’s symbol as its expression

Returns:

  • (Order)

    The new Order node



93
94
95
# File 'lib/squeel/nodes/stub.rb', line 93

def desc
  Order.new self.symbol, -1
end

#eql?(other) ⇒ Boolean

Object comparison

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/squeel/nodes/stub.rb', line 38

def eql?(other)
  self.class == other.class &&
  self.symbol == other.symbol
end

#func(*args) ⇒ Function

Create a Function node for a function named the same as this Stub and with the given arguments

Returns:



99
100
101
# File 'lib/squeel/nodes/stub.rb', line 99

def func(*args)
  Function.new(self.symbol, args)
end

#hashObject

To support object equality tests



44
45
46
# File 'lib/squeel/nodes/stub.rb', line 44

def hash
  symbol.hash
end

#innerJoin

Create an inner Join node for the association named by this Stub

Returns:

  • (Join)

    The new inner Join node



105
106
107
# File 'lib/squeel/nodes/stub.rb', line 105

def inner
  Join.new(self.symbol, Arel::InnerJoin)
end

#of_class(klass) ⇒ Join

Create a polymorphic Join node for the association named by this Stub,

Parameters:

  • klass (Class)

    The polymorphic belongs_to class for this Join

Returns:

  • (Join)

    The new polymorphic Join node



118
119
120
# File 'lib/squeel/nodes/stub.rb', line 118

def of_class(klass)
  Join.new(self.symbol, Arel::InnerJoin, klass)
end

#outerJoin

Create an outer Join node for the association named by this Stub

Returns:

  • (Join)

    The new outer Join node



111
112
113
# File 'lib/squeel/nodes/stub.rb', line 111

def outer
  Join.new(self.symbol, Arel::OuterJoin)
end

#to_sString

Returns The Stub’s String equivalent.

Returns:

  • (String)

    The Stub’s String equivalent.



54
55
56
# File 'lib/squeel/nodes/stub.rb', line 54

def to_s
  symbol.to_s
end

#to_symSymbol

Returns The symbol this Stub contains.

Returns:

  • (Symbol)

    The symbol this Stub contains.



49
50
51
# File 'lib/squeel/nodes/stub.rb', line 49

def to_sym
  symbol
end

#~KeyPath

Return a KeyPath containing only this Stub, but flagged as absolute. This helps Stubs behave more like a KeyPath, as anyone using the Squeel DSL is likely to think of them as such.

Returns:

  • (KeyPath)

    An absolute KeyPath, containing only this Stub



81
82
83
# File 'lib/squeel/nodes/stub.rb', line 81

def ~
  KeyPath.new [], self, true
end