Class: Squeel::Nodes::Stub

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

#asc, #desc

Methods included from Aliasing

#as

Methods included from Operators

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

Methods included from PredicateMethods

#eq

Constructor Details

#initialize(symbol) ⇒ Stub

Create a new Stub.

Parameters:

  • symbol (Symbol)

    The symbol that this Stub contains



36
37
38
# File 'lib/squeel/nodes/stub.rb', line 36

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:



77
78
79
80
81
82
83
84
85
86
# File 'lib/squeel/nodes/stub.rb', line 77

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



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

def symbol
  @symbol
end

Instance Method Details

#eql?(other) ⇒ Boolean

Object comparison

Returns:

  • (Boolean)


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

def eql?(other)
  # Should we maybe allow a stub to equal a symbol?
  # I can see not doing to leading to confusion, but I don't like it. :(
  self.class.eql?(other.class) &&
  self.symbol.eql?(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:



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

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

#hashObject

To support object equality tests



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

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



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

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



123
124
125
# File 'lib/squeel/nodes/stub.rb', line 123

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



116
117
118
# File 'lib/squeel/nodes/stub.rb', line 116

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

#sift(name, *args) ⇒ KeyPath

Create a keypath with a sifter as its endpoint

Returns:



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

def sift(name, *args)
  KeyPath.new([self, Sifter.new(name, args)])
end

#to_aArray

Returns An array with a copy of this Stub as the only element.

Returns:

  • (Array)

    An array with a copy of this Stub as the only element.



65
66
67
# File 'lib/squeel/nodes/stub.rb', line 65

def to_a
  [dup]
end

#to_sString Also known as: to_str

Returns The Stub’s String equivalent.

Returns:

  • (String)

    The Stub’s String equivalent.



59
60
61
# File 'lib/squeel/nodes/stub.rb', line 59

def to_s
  symbol.to_s
end

#to_symSymbol

Returns The symbol this Stub contains.

Returns:

  • (Symbol)

    The symbol this Stub contains.



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

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



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

def ~
  KeyPath.new [self], true
end