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.
6764 6765 6766 6767 |
# File 'lib/parse/query.rb', line 6764 def initialize(results, operation = nil) @results = results @operation = operation end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over each key-value pair
6776 6777 6778 |
# File 'lib/parse/query.rb', line 6776 def each(&block) @results.each(&block) end |
#sort_by_key_asc ⇒ Array<Array>
Sort by keys (group names) in ascending order
6782 6783 6784 |
# File 'lib/parse/query.rb', line 6782 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
6788 6789 6790 |
# File 'lib/parse/query.rb', line 6788 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
6794 6795 6796 |
# File 'lib/parse/query.rb', line 6794 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
6800 6801 6802 |
# File 'lib/parse/query.rb', line 6800 def sort_by_value_desc @results.sort_by { |k, v| v }.reverse end |
#to_h ⇒ Hash
Return the raw hash results
6771 6772 6773 |
# File 'lib/parse/query.rb', line 6771 def to_h @results end |
#to_sorted_hash(sorted_pairs) ⇒ Hash
Convert sorted results back to a hash
6807 6808 6809 |
# File 'lib/parse/query.rb', line 6807 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.
6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 |
# File 'lib/parse/query.rb', line 6820 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 |