Class: Rql::Scope::RqlScope

Inherits:
Object show all
Includes:
BlockMethods, ParamMethods
Defined in:
lib/rql/scope/rql_scope.rb

Instance Method Summary collapse

Methods included from BlockMethods

#includes, #joins, #order, #pluck, #select, #where

Methods included from ParamMethods

#includes, #joins, #order, #pluck, #select, #where

Constructor Details

#initialize(scope) ⇒ RqlScope

Returns a new instance of RqlScope.

Parameters:

  • scope (ActiveRecord::Relation)

    the underlying scope to wrap



8
9
10
11
12
# File 'lib/rql/scope/rql_scope.rb', line 8

def initialize(scope)
  @block_methods = BlockMethodGroup.new(scope)
  @param_methods = ParamMethodGroup.new(scope)
  @scope = scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *params, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/rql/scope/rql_scope.rb', line 21

def method_missing(method_name, *params, &block)
  if block && @block_methods.respond_to?(method_name)
    RqlScope.new(@block_methods.send(method_name, &block))
  elsif @param_methods.respond_to?(method_name)
    RqlScope.new(@param_methods.send(method_name, *params))
  else
    scope.send(method_name, *params)
  end
end

Instance Method Details

#arelArel::SelectManager

Gets the underlying arel object for the scope

Returns:

  • (Arel::SelectManager)

    the underlying arel object



50
51
52
# File 'lib/rql/scope/rql_scope.rb', line 50

def arel
  scope.arel
end

#merge(other) ⇒ Rql::Scope::RqlScope

Creates a new scope merging the current scope with the other scope

Parameters:

Returns:



42
43
44
45
# File 'lib/rql/scope/rql_scope.rb', line 42

def merge(other)
  other = other.scope if other.is_a?(RqlScope)
  RqlScope.new(scope.merge(other))
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/rql/scope/rql_scope.rb', line 31

def respond_to_missing?(method_name, include_private = false)
  @block_methods.respond_to?(method_name, include_private) ||
    @param_methods.respond_to?(method_name, include_private) ||
    super.respond_to?(method_name, include_private) ||
    super
end

#scopeActiveRecord::Relation

Gets the underlying ActiveRecord Relation object for the scope

Returns:

  • (ActiveRecord::Relation)

    the underlying arel object



17
18
19
# File 'lib/rql/scope/rql_scope.rb', line 17

def scope
  @scope
end

#to_aArray

Executes the scope against the database returning the results as an array

Returns:

  • (Array)

    the results as an array



57
58
59
# File 'lib/rql/scope/rql_scope.rb', line 57

def to_a
  scope.to_a
end