Class: Squeel::Context Abstract
- Inherits:
-
Object
- Object
- Squeel::Context
- Defined in:
- lib/squeel/context.rb
Overview
Subclass and implement #traverse, ##find and #get_table to create a Context that supports a given ORM.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#arel_visitor ⇒ Object
readonly
Returns the value of attribute arel_visitor.
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
Instance Method Summary collapse
-
#contextualize(object) ⇒ Arel::Table
This method, as implemented, just makes use of the table cache, which will call get_table, where the real work of getting the ARel Table occurs.
-
#find(object, parent = @base) ⇒ Object
This method should find a given object inside the context.
-
#get_table(object) ⇒ Arel::Table
private
Returns an Arel::Table that’s appropriate for the object it’s been sent.
-
#initialize(object) ⇒ Context
constructor
The Squeel context expects some kind of context object that is representative of the current joins in a query in order to return appropriate tables.
-
#traverse(keypath, parent = @base, include_endpoint = false) ⇒ Object
This method should traverse a keypath and return an object for use in future calls to #traverse, #find, or #contextualize.
Constructor Details
#initialize(object) ⇒ Context
The Squeel context expects some kind of context object that is representative of the current joins in a query in order to return appropriate tables. Again, in the case of an ActiveRecord context, this will be a JoinDependency. Subclasses are expected to set the @base
, @engine
, and @arel_visitor
instance variables to appropriate values for use in their implementations of other required methods.
18 19 20 21 |
# File 'lib/squeel/context.rb', line 18 def initialize(object) @object = object @tables = Hash.new {|hash, key| hash[key] = get_table(key)} end |
Instance Attribute Details
#arel_visitor ⇒ Object (readonly)
Returns the value of attribute arel_visitor.
7 8 9 |
# File 'lib/squeel/context.rb', line 7 def arel_visitor @arel_visitor end |
#base ⇒ Object (readonly)
Returns the value of attribute base.
7 8 9 |
# File 'lib/squeel/context.rb', line 7 def base @base end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
7 8 9 |
# File 'lib/squeel/context.rb', line 7 def engine @engine end |
Instance Method Details
#contextualize(object) ⇒ Arel::Table
This method, as implemented, just makes use of the table cache, which will call get_table, where the real work of getting the ARel Table occurs.
49 50 51 |
# File 'lib/squeel/context.rb', line 49 def contextualize(object) @tables[object] end |
#find(object, parent = @base) ⇒ Object
This method should find a given object inside the context.
28 29 30 |
# File 'lib/squeel/context.rb', line 28 def find(object, parent = @base) raise NotImplementedError, "Subclasses must implement public method find" end |
#get_table(object) ⇒ Arel::Table (private)
Returns an Arel::Table that’s appropriate for the object it’s been sent. What’s “appropriate”? Well, that’s up to the implementation to decide, but it should probably generate a table that is least likely to result in invalid SQL.
62 63 64 |
# File 'lib/squeel/context.rb', line 62 def get_table(object) raise NotImplementedError, "Subclasses must implement private method get_table" end |
#traverse(keypath, parent = @base, include_endpoint = false) ⇒ Object
This method should traverse a keypath and return an object for use in future calls to #traverse, #find, or #contextualize.
40 41 42 |
# File 'lib/squeel/context.rb', line 40 def traverse(keypath, parent = @base, include_endpoint = false) raise NotImplementedError, "Subclasses must implement public method traverse" end |