Class: ActiveRecordExtended::QueryMethods::WithCTE::WithCTE

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utilities::Support, Enumerable
Defined in:
lib/active_record_extended/query_methods/with_cte.rb

Constant Summary

Constants included from Utilities::Support

Utilities::Support::A_TO_Z_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities::Support

#double_quote, #flatten_safely, #flatten_to_sql, #from_clause_constructor, #generate_grouping, #generate_named_function, #group_when_needed, #key_generator, #literal_key, #needs_to_be_grouped?, #nested_alias_escape, #to_arel_sql, #wrap_with_agg_array, #wrap_with_array

Constructor Details

#initialize(scope) ⇒ WithCTE

Returns a new instance of WithCTE.

Parameters:

  • scope (ActiveRecord::Relation)


15
16
17
18
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 15

def initialize(scope)
  @scope = scope
  reset!
end

Instance Attribute Details

#with_keysObject (readonly)

Returns the value of attribute with_keys.



12
13
14
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 12

def with_keys
  @with_keys
end

#with_valuesObject

Returns the value of attribute with_values.



12
13
14
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 12

def with_values
  @with_values
end

Instance Method Details

#eachEnumerable Also known as: each_pair

Returns the order for which CTE's were imported as.

Returns:

  • (Enumerable)

    Returns the order for which CTE's were imported as.



21
22
23
24
25
26
27
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 21

def each
  return to_enum(:each) unless block_given?

  with_keys.each do |key|
    yield(key, with_values[key])
  end
end

#pipe_cte_with!(value) ⇒ Object

Parameters:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 37

def pipe_cte_with!(value)
  return if value.nil? || value.empty?

  value.each_pair do |name, expression|
    sym_name = name.to_sym
    next if with_values.key?(sym_name)

    # Ensure we follow FIFO pattern.
    # If the parent has similar CTE alias keys, we want to favor the parent's expressions over its children's.
    if expression.is_a?(ActiveRecord::Relation) && expression.with_values?
      pipe_cte_with!(expression.cte)
      expression.cte.reset!
    end

    @with_keys            |= [sym_name]
    @with_values[sym_name] = expression
  end

  value.reset! if value.is_a?(WithCTE)
end

#reset!Object



58
59
60
61
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 58

def reset!
  @with_keys   = []
  @with_values = {}
end