Class: Parse::GroupedResult
- Inherits:
-
Object
- Object
- Parse::GroupedResult
- 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
-
#each(&block) ⇒ Object
Iterate over each key-value pair.
-
#initialize(results, operation = nil) ⇒ GroupedResult
constructor
A new instance of GroupedResult.
-
#sort_by_key_asc ⇒ Array<Array>
Sort by keys (group names) in ascending order.
-
#sort_by_key_desc ⇒ Array<Array>
Sort by keys (group names) in descending order.
-
#sort_by_value_asc ⇒ Array<Array>
Sort by values (aggregation results) in ascending order.
-
#sort_by_value_desc ⇒ Array<Array>
Sort by values (aggregation results) in descending order.
-
#to_h ⇒ Hash
Return the raw hash results.
-
#to_sorted_hash(sorted_pairs) ⇒ Hash
Convert sorted results back to a hash.
-
#to_table(format: :ascii, headers: nil) ⇒ String
Convert grouped results to a formatted table.
Constructor Details
#initialize(results, operation = nil) ⇒ GroupedResult
Returns a new instance of GroupedResult.
6930 6931 6932 6933 |
# File 'lib/parse/query.rb', line 6930 def initialize(results, operation = nil) @results = results @operation = operation end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over each key-value pair
6942 6943 6944 |
# File 'lib/parse/query.rb', line 6942 def each(&block) @results.each(&block) end |
#sort_by_key_asc ⇒ Array<Array>
Sort by keys (group names) in ascending order
6948 6949 6950 |
# File 'lib/parse/query.rb', line 6948 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
6954 6955 6956 |
# File 'lib/parse/query.rb', line 6954 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
6960 6961 6962 |
# File 'lib/parse/query.rb', line 6960 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
6966 6967 6968 |
# File 'lib/parse/query.rb', line 6966 def sort_by_value_desc @results.sort_by { |k, v| v }.reverse end |
#to_h ⇒ Hash
Return the raw hash results
6937 6938 6939 |
# File 'lib/parse/query.rb', line 6937 def to_h @results end |
#to_sorted_hash(sorted_pairs) ⇒ Hash
Convert sorted results back to a hash
6973 6974 6975 |
# File 'lib/parse/query.rb', line 6973 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.
6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 |
# File 'lib/parse/query.rb', line 6986 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 |