Module: DataMapper::Persevere::Aggregates

Defined in:
lib/persevere_adapter/aggregates.rb

Instance Method Summary collapse

Instance Method Details

#aggregate(query) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/persevere_adapter/aggregates.rb', line 4

def aggregate(query)
  records = []
  fields = query.fields
  field_size = fields.size
      
  connect if @persevere.nil?
  resources = Array.new

  query = Persevere.enhance(query)

  json_query, headers = query.to_json_query
  path = "/#{query.model.storage_name}/#{json_query}"
  DataMapper.logger.debug("--> PATH/QUERY/HEADERS: #{path} #{headers.inspect}")
  
  response = @persevere.retrieve(path, headers)

  if response.code == "200"
    results = [response.body]
    results.each do |row_of_results|
     row = query.fields.zip([row_of_results].flatten).map do |field, value|
        if field.respond_to?(:operator)
          send(field.operator, field.target, value)
        else
          field.typecast(value)
        end
      end            
      records << (field_size > 1 ? row : row[0])
    end
  end
  records
end