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.
7019 7020 7021 7022 |
# File 'lib/parse/query.rb', line 7019 def initialize(results, operation = nil) @results = results @operation = operation end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over each key-value pair
7031 7032 7033 |
# File 'lib/parse/query.rb', line 7031 def each(&block) @results.each(&block) end |
#sort_by_key_asc ⇒ Array<Array>
Sort by keys (group names) in ascending order
7037 7038 7039 |
# File 'lib/parse/query.rb', line 7037 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
7043 7044 7045 |
# File 'lib/parse/query.rb', line 7043 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
7049 7050 7051 |
# File 'lib/parse/query.rb', line 7049 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
7055 7056 7057 |
# File 'lib/parse/query.rb', line 7055 def sort_by_value_desc @results.sort_by { |k, v| v }.reverse end |
#to_h ⇒ Hash
Return the raw hash results
7026 7027 7028 |
# File 'lib/parse/query.rb', line 7026 def to_h @results end |
#to_sorted_hash(sorted_pairs) ⇒ Hash
Convert sorted results back to a hash
7062 7063 7064 |
# File 'lib/parse/query.rb', line 7062 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.
7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 |
# File 'lib/parse/query.rb', line 7075 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 |