Class: Parse::GroupedResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/parse/query.rb

Overview

Wrapper class for grouped results that provides sorting capabilities. Allows sorting grouped results by keys (group names) or values (aggregation results) in ascending or descending order.

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ GroupedResult

Returns a new instance of GroupedResult.

Parameters:

  • results (Hash)

    the grouped results hash



6257
6258
6259
# File 'lib/parse/query.rb', line 6257

def initialize(results)
  @results = results
end

Instance Method Details

#each(&block) ⇒ Object

Iterate over each key-value pair



6268
6269
6270
# File 'lib/parse/query.rb', line 6268

def each(&block)
  @results.each(&block)
end

#sort_by_key_ascArray<Array>

Sort by keys (group names) in ascending order

Returns:

  • (Array<Array>)

    array of [key, value] pairs sorted by key ascending



6274
6275
6276
# File 'lib/parse/query.rb', line 6274

def sort_by_key_asc
  @results.sort_by { |k, v| k }
end

#sort_by_key_descArray<Array>

Sort by keys (group names) in descending order

Returns:

  • (Array<Array>)

    array of [key, value] pairs sorted by key descending



6280
6281
6282
# File 'lib/parse/query.rb', line 6280

def sort_by_key_desc
  @results.sort_by { |k, v| k }.reverse
end

#sort_by_value_ascArray<Array>

Sort by values (aggregation results) in ascending order

Returns:

  • (Array<Array>)

    array of [key, value] pairs sorted by value ascending



6286
6287
6288
# File 'lib/parse/query.rb', line 6286

def sort_by_value_asc
  @results.sort_by { |k, v| v }
end

#sort_by_value_descArray<Array>

Sort by values (aggregation results) in descending order

Returns:

  • (Array<Array>)

    array of [key, value] pairs sorted by value descending



6292
6293
6294
# File 'lib/parse/query.rb', line 6292

def sort_by_value_desc
  @results.sort_by { |k, v| v }.reverse
end

#to_hHash

Return the raw hash results

Returns:

  • (Hash)

    the grouped results



6263
6264
6265
# File 'lib/parse/query.rb', line 6263

def to_h
  @results
end

#to_sorted_hash(sorted_pairs) ⇒ Hash

Convert sorted results back to a hash

Parameters:

  • sorted_pairs (Array<Array>)

    array of [key, value] pairs

Returns:

  • (Hash)

    sorted results as hash



6299
6300
6301
# File 'lib/parse/query.rb', line 6299

def to_sorted_hash(sorted_pairs)
  sorted_pairs.to_h
end

#to_table(format: :ascii, headers: ["Group", "Count"]) ⇒ String

Convert grouped results to a formatted table.

Examples:

Document.group_by(:category, sortable: true).count.to_table
Document.group_by(:category).sum(:file_size).to_table(headers: ["Category", "Total Size"])

Parameters:

  • format (Symbol) (defaults to: :ascii)

    output format (:ascii, :csv, :json)

  • headers (Array<String>) (defaults to: ["Group", "Count"])

    custom headers (default: [“Group”, “Count”])

Returns:

  • (String)

    formatted table



6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
# File 'lib/parse/query.rb', line 6310

def to_table(format: :ascii, headers: ["Group", "Count"])
  pairs = @results.to_a

  # Build table data
  table_data = {
    headers: headers,
    rows: pairs.map { |key, value| [format_group_key(key), format_group_value(value)] },
  }

  # Format based on requested format
  case format
  when :ascii
    format_grouped_ascii_table(table_data)
  when :csv
    format_grouped_csv_table(table_data)
  when :json
    format_grouped_json_table(table_data)
  else
    raise ArgumentError, "Unsupported format: #{format}. Use :ascii, :csv, or :json"
  end
end