Class: Parse::AggregationResult
- Inherits:
-
Object
- Object
- Parse::AggregationResult
- Defined in:
- lib/parse/query.rb
Overview
Wrapper class for custom aggregation results (from $group, $project, etc.) Provides both hash-style access and method-style access to fields. Field names are automatically converted from camelCase to snake_case.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hash-style access with string or symbol keys.
-
#initialize(data) ⇒ AggregationResult
constructor
A new instance of AggregationResult.
- #inspect ⇒ Object
-
#key?(key) ⇒ Boolean
Check if a key exists.
-
#keys ⇒ Array<Symbol>
Get all keys (snake_case symbols).
-
#method_missing(method_name, *args, &block) ⇒ Object
Method-style access to fields.
-
#raw ⇒ Hash
Get the raw data as originally received.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Hash
(also: #to_hash)
Convert to hash with snake_case symbol keys.
Constructor Details
#initialize(data) ⇒ AggregationResult
Returns a new instance of AggregationResult.
5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 |
# File 'lib/parse/query.rb', line 5354 def initialize(data) @data = {} @raw_data = data # Convert keys to snake_case and store data.each do |key, value| snake_key = Parse::Query.to_snake_case(key.to_s) @data[snake_key.to_sym] = value @data[key.to_s] = value # Also keep original key for hash access end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Method-style access to fields
5402 5403 5404 5405 5406 5407 5408 5409 |
# File 'lib/parse/query.rb', line 5402 def method_missing(method_name, *args, &block) key = method_name.to_sym if @data.key?(key) @data[key] else super end end |
Instance Method Details
#[](key) ⇒ Object
Hash-style access with string or symbol keys
5369 5370 5371 |
# File 'lib/parse/query.rb', line 5369 def [](key) @data[key.to_s] || @data[key.to_sym] end |
#inspect ⇒ Object
5415 5416 5417 |
# File 'lib/parse/query.rb', line 5415 def inspect "#<Parse::AggregationResult #{to_h.inspect}>" end |
#key?(key) ⇒ Boolean
Check if a key exists
5376 5377 5378 |
# File 'lib/parse/query.rb', line 5376 def key?(key) @data.key?(key.to_s) || @data.key?(key.to_sym) end |
#keys ⇒ Array<Symbol>
Get all keys (snake_case symbols)
5382 5383 5384 |
# File 'lib/parse/query.rb', line 5382 def keys @data.keys.select { |k| k.is_a?(Symbol) } end |
#raw ⇒ Hash
Get the raw data as originally received
5397 5398 5399 |
# File 'lib/parse/query.rb', line 5397 def raw @raw_data end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
5411 5412 5413 |
# File 'lib/parse/query.rb', line 5411 def respond_to_missing?(method_name, include_private = false) @data.key?(method_name.to_sym) || super end |