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) ⇒ 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: ["Group", "Count"]) ⇒ String
Convert grouped results to a formatted table.
Constructor Details
#initialize(results) ⇒ GroupedResult
Returns a new instance of GroupedResult.
6184 6185 6186 |
# File 'lib/parse/query.rb', line 6184 def initialize(results) @results = results end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over each key-value pair
6195 6196 6197 |
# File 'lib/parse/query.rb', line 6195 def each(&block) @results.each(&block) end |
#sort_by_key_asc ⇒ Array<Array>
Sort by keys (group names) in ascending order
6201 6202 6203 |
# File 'lib/parse/query.rb', line 6201 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
6207 6208 6209 |
# File 'lib/parse/query.rb', line 6207 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
6213 6214 6215 |
# File 'lib/parse/query.rb', line 6213 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
6219 6220 6221 |
# File 'lib/parse/query.rb', line 6219 def sort_by_value_desc @results.sort_by { |k, v| v }.reverse end |
#to_h ⇒ Hash
Return the raw hash results
6190 6191 6192 |
# File 'lib/parse/query.rb', line 6190 def to_h @results end |
#to_sorted_hash(sorted_pairs) ⇒ Hash
Convert sorted results back to a hash
6226 6227 6228 |
# File 'lib/parse/query.rb', line 6226 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.
6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 |
# File 'lib/parse/query.rb', line 6237 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 |