Class: BatchAgg::Runner

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

Instance Method Summary collapse

Constructor Details

#initialize(aggs, model) ⇒ Runner

Returns a new instance of Runner.



172
173
174
175
176
177
# File 'lib/batchagg.rb', line 172

def initialize(aggs, model)
  @aggs = aggs
  @model = model
  @query = Query.new(model, aggs)
  @result_class = build_result_class(aggs)
end

Instance Method Details

#from(scope) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/batchagg.rb', line 183

def from(scope)
  rows = scope.klass.connection.select_all(@query.build(scope).to_sql).to_a
  rows.each_with_object({}) do |row, h|
    id = row[@model.primary_key.to_s]
    h[@model.primary_key ? id : row.keys.first] = @result_class.new(row)
  end
end

#only(record) ⇒ Object



179
180
181
# File 'lib/batchagg.rb', line 179

def only(record)
  from(@model.where(@model.primary_key => record.id))
end