Class: ActiveRecordExtended::QueryMethods::WithCTE::WithCTE
- Inherits:
-
Object
- Object
- ActiveRecordExtended::QueryMethods::WithCTE::WithCTE
- 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
-
#materialized_keys ⇒ Object
readonly
Returns the value of attribute materialized_keys.
-
#not_materialized_keys ⇒ Object
readonly
Returns the value of attribute not_materialized_keys.
-
#with_keys ⇒ Object
readonly
Returns the value of attribute with_keys.
-
#with_values ⇒ Object
Returns the value of attribute with_values.
Instance Method Summary collapse
-
#each ⇒ Enumerable
(also: #each_pair)
Returns the order for which CTE’s were imported as.
-
#initialize(scope) ⇒ WithCTE
constructor
A new instance of WithCTE.
- #materialized_key?(key) ⇒ Boolean
- #not_materialized_key?(key) ⇒ Boolean
- #pipe_cte_with!(value) ⇒ Object
- #reset! ⇒ Object
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.
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
#materialized_keys ⇒ Object (readonly)
Returns the value of attribute materialized_keys.
12 13 14 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 12 def materialized_keys @materialized_keys end |
#not_materialized_keys ⇒ Object (readonly)
Returns the value of attribute not_materialized_keys.
12 13 14 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 12 def not_materialized_keys @not_materialized_keys end |
#with_keys ⇒ Object (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_values ⇒ Object
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
#each ⇒ Enumerable Also known as: each_pair
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 |
#materialized_key?(key) ⇒ Boolean
37 38 39 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 37 def materialized_key?(key) materialized_keys.include?(key.to_sym) end |
#not_materialized_key?(key) ⇒ Boolean
42 43 44 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 42 def not_materialized_key?(key) not_materialized_keys.include?(key.to_sym) end |
#pipe_cte_with!(value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 47 def pipe_cte_with!(value) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity 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? expression.cte = expression.cte.dup if expression.cte # Add child's materialized keys to the parent @materialized_keys += expression.cte.materialized_keys @not_materialized_keys += expression.cte.not_materialized_keys 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
74 75 76 77 78 79 |
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 74 def reset! @with_keys = [] @with_values = {} @materialized_keys = Set.new @not_materialized_keys = Set.new end |