Class: Squeel::Context Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/squeel/context.rb

Overview

This class is abstract.

Subclass and implement #traverse, ##find and #get_table to create a Context that supports a given ORM.

Direct Known Subclasses

Adapters::ActiveRecord::Context

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • object

    The object the context will use for contextualization



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_visitorObject (readonly)

Returns the value of attribute arel_visitor.



7
8
9
# File 'lib/squeel/context.rb', line 7

def arel_visitor
  @arel_visitor
end

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/squeel/context.rb', line 7

def base
  @base
end

#engineObject (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.

Parameters:

  • object

    A contextualizable object (this will depend on the subclass’s implementation)

Returns:

  • (Arel::Table)

    A table corresponding to the object param



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.

Parameters:

  • object

    The object to find

  • parent (defaults to: @base)

    The parent object, if applicable

Returns:

  • a valid “parent” or contextualizable object

Raises:

  • (NotImplementedError)


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.

Parameters:

  • object

    A contextualizable object (this will depend on the subclass’s implementation)

Returns:

  • (Arel::Table)

    A table corresponding to the object param.

Raises:

  • (NotImplementedError)


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.

Parameters:

  • keypath (Nodes::KeyPath)

    The keypath to traverse

  • parent (defaults to: @base)

    The parent object from which traversal should start.

  • include_endpoint (Boolean) (defaults to: false)

    Whether or not the KeyPath’s endpoint should be treated as a traversable key

Returns:

  • a valid “parent” or contextualizable object

Raises:

  • (NotImplementedError)


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