Class: Prato::Internal::QueryState

Inherits:
Object
  • Object
show all
Defined in:
lib/prato/internal/query_state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



6
7
8
# File 'lib/prato/internal/query_state.rb', line 6

def dataset
  @dataset
end

Class Method Details

.create(base_scope, materialization_fields) ⇒ Object



8
9
10
11
12
# File 'lib/prato/internal/query_state.rb', line 8

def self.create(base_scope, materialization_fields)
  dataset = base_scope.dup

  new(dataset, nil, materialization_fields)
end

Instance Method Details

#materialized_dataset(spec) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/prato/internal/query_state.rb', line 22

def materialized_dataset(spec)
  return [@dataset, @ruby_loaded_data] unless unmaterialized?

  columns = spec.columns
  scope = dataset
  selects = Set.new([Arel.sql("#{scope.model.table_name}.*")])
  association_load_values = []

  @materialization_fields.each do |field|
    column = columns[field]

    case column
    when Types::AggregateColumn, Types::ExpressionColumn
      selects << column.select_node
    when Types::AssociationColumn
      association_load_values << association_path_to_association_load(column.association_path)
    when Types::RubyColumn
      association_load_values << column.includes if column.includes

      loader = spec.ruby_loaders&.[](column.loader)
      association_load_values << loader[:includes] if loader && loader[:includes]
    end
  end

  if association_load_values.any?
    scope = apply_association_loading(scope, association_load_values)
  end

  scope = scope.select(selects.to_a)
  records = scope.to_a

  ruby_loaded_data = nil
  if spec.ruby_loaders&.any?
    ruby_loaded_data = LazyLoaderCache.new(records)
    spec.ruby_loaders.each { |key, loader| ruby_loaded_data[key] = loader[:block] }
  end

  @records = records
  @ruby_loaded_data = ruby_loaded_data
  [records, ruby_loaded_data]
end

#unmaterialized?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/prato/internal/query_state.rb', line 18

def unmaterialized?
  !dataset.is_a?(Array)
end

#with_dataset(dataset) ⇒ Object



14
15
16
# File 'lib/prato/internal/query_state.rb', line 14

def with_dataset(dataset)
  self.class.new(dataset, @ruby_loaded_data, @materialization_fields)
end