Class: Squeel::Nodes::Stub

Inherits:
Object
  • Object
show all
Includes:
Aliasing, 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 Aliasing

#as

Methods included from Operators

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

Constructor Details

#initialize(symbol) ⇒ Stub

Create a new Stub.

Parameters:

  • symbol (Symbol)

    The symbol that this Stub contains



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

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:



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

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



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

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



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

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



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

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

#eql?(other) ⇒ Boolean

Object comparison

Returns:

  • (Boolean)


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

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:



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

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

#hashObject

To support object equality tests



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

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



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

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



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

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



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

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

#to_sString

Returns The Stub’s String equivalent.

Returns:

  • (String)

    The Stub’s String equivalent.



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

def to_s
  symbol.to_s
end

#to_symSymbol

Returns The symbol this Stub contains.

Returns:

  • (Symbol)

    The symbol this Stub contains.



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

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



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

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