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, operation = nil) ⇒ GroupedResult

Returns a new instance of GroupedResult.

Parameters:

  • results (Hash) —

    the grouped results hash

  • operation (String, nil) (defaults to: nil) —

    the aggregation operation (e.g. "count", "sum", "average", "min", "max", "list")



8754
8755
8756
8757
# File 'lib/parse/query.rb', line 8754

def initialize(results, operation = nil)
  @results = results
  @operation = operation
end

Instance Method Details

#each(&block) ⇒ Object

Iterate over each key-value pair



8766
8767
8768
# File 'lib/parse/query.rb', line 8766

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

#sort_by_key_asc ⇒ Array<Array>

Sort by keys (group names) in ascending order

Returns:

  • (Array<Array>) —

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



8772
8773
8774
# File 'lib/parse/query.rb', line 8772

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

#sort_by_key_desc ⇒ Array<Array>

Sort by keys (group names) in descending order

Returns:

  • (Array<Array>) —

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



8778
8779
8780
# File 'lib/parse/query.rb', line 8778

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

#sort_by_value_asc ⇒ Array<Array>

Sort by values (aggregation results) in ascending order

Returns:

  • (Array<Array>) —

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



8784
8785
8786
# File 'lib/parse/query.rb', line 8784

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

#sort_by_value_desc ⇒ Array<Array>

Sort by values (aggregation results) in descending order

Returns:

  • (Array<Array>) —

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



8790
8791
8792
# File 'lib/parse/query.rb', line 8790

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

#to_h ⇒ Hash

Return the raw hash results

Returns:

  • (Hash) —

    the grouped results



8761
8762
8763
# File 'lib/parse/query.rb', line 8761

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



8797
8798
8799
# File 'lib/parse/query.rb', line 8797

def to_sorted_hash(sorted_pairs)
  sorted_pairs.to_h
end

#to_table(format: :ascii, headers: nil) ⇒ 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>, nil) (defaults to: nil) —

    custom headers; if nil, defaults to ["Group", ] where the second header reflects the aggregation operation (e.g. "Average" for avg/average, "Sum" for sum, "Min"/"Max" for min/max, "Items" for list, "Count" otherwise).

Returns:

  • (String) —

    formatted table



8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
# File 'lib/parse/query.rb', line 8810

def to_table(format: :ascii, headers: nil)
  headers ||= ["Group", default_value_header]
  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