Class: Squeel::Nodes::Stub
- 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
-
#symbol ⇒ Symbol
readonly
The symbol contained by this stub.
Instance Method Summary collapse
- #add_to_tree(hash) ⇒ Object
-
#eql?(other) ⇒ Boolean
Object comparison.
-
#func(*args) ⇒ Function
Create a Function node for a function named the same as this Stub and with the given arguments.
-
#hash ⇒ Object
To support object equality tests.
-
#initialize(symbol) ⇒ Stub
constructor
Create a new Stub.
-
#inner ⇒ Join
Create an inner Join node for the association named by this Stub.
-
#method_missing(method_id, *args) ⇒ Object
Create a KeyPath when any undefined method is called on a Stub.
-
#of_class(klass) ⇒ Join
Create a polymorphic Join node for the association named by this Stub,.
-
#outer ⇒ Join
Create an outer Join node for the association named by this Stub.
-
#sift(name, *args) ⇒ KeyPath
Create a keypath with a sifter as its endpoint.
-
#to_a ⇒ Array
An array with a copy of this Stub as the only element.
-
#to_s ⇒ String
(also: #to_str)
The Stub’s String equivalent.
-
#to_sym ⇒ Symbol
The symbol this Stub contains.
-
#~ ⇒ KeyPath
Return a KeyPath containing only this Stub, but flagged as absolute.
Methods included from Ordering
Methods included from Aliasing
Methods included from Operators
Methods included from PredicateMethods
Methods inherited from Node
Constructor Details
#initialize(symbol) ⇒ Stub
Create a new Stub.
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_name ⇒ KeyPath #node_name(klass) ⇒ KeyPath
Create a KeyPath when any undefined method is called on a Stub.
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, InnerJoin, args[0])]) else KeyPath.new([self, Nodes::Function.new(method_id, args)]) end end |
Instance Attribute Details
#symbol ⇒ Symbol (readonly)
Returns The symbol contained by this stub.
32 33 34 |
# File 'lib/squeel/nodes/stub.rb', line 32 def symbol @symbol end |
Instance Method Details
#add_to_tree(hash) ⇒ Object
127 128 129 |
# File 'lib/squeel/nodes/stub.rb', line 127 def add_to_tree(hash) hash[symbol] ||= {} end |
#eql?(other) ⇒ Boolean
Object comparison
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
98 99 100 |
# File 'lib/squeel/nodes/stub.rb', line 98 def func(*args) Function.new(self.symbol, args) end |
#hash ⇒ Object
To support object equality tests
49 50 51 |
# File 'lib/squeel/nodes/stub.rb', line 49 def hash symbol.hash end |
#inner ⇒ Join
Create an inner Join node for the association named by this Stub
104 105 106 |
# File 'lib/squeel/nodes/stub.rb', line 104 def inner Join.new(self.symbol, InnerJoin) end |
#of_class(klass) ⇒ Join
Create a polymorphic Join node for the association named by this Stub,
123 124 125 |
# File 'lib/squeel/nodes/stub.rb', line 123 def of_class(klass) Join.new(self.symbol, InnerJoin, klass) end |
#outer ⇒ Join
Create an outer Join node for the association named by this Stub
116 117 118 |
# File 'lib/squeel/nodes/stub.rb', line 116 def outer Join.new(self.symbol, OuterJoin) end |
#sift(name, *args) ⇒ KeyPath
Create a keypath with a sifter as its endpoint
110 111 112 |
# File 'lib/squeel/nodes/stub.rb', line 110 def sift(name, *args) KeyPath.new([self, Sifter.new(name, args)]) end |
#to_a ⇒ Array
Returns 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_s ⇒ String Also known as: to_str
Returns The Stub’s String equivalent.
59 60 61 |
# File 'lib/squeel/nodes/stub.rb', line 59 def to_s symbol.to_s end |
#to_sym ⇒ Symbol
Returns 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.
92 93 94 |
# File 'lib/squeel/nodes/stub.rb', line 92 def ~ KeyPath.new [self], true end |