Class: Parse::AggregationResult

Inherits:
Object
  • Object
show all
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.

Examples:

result = AggregationResult.new({ "_id" => "Rock", "totalPlays" => 500 })
result["_id"]        # => "Rock"
result[:total_plays] # => 500
result.total_plays   # => 500

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ AggregationResult

Returns a new instance of AggregationResult.

Parameters:

  • data (Hash)

    the raw aggregation result hash



5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
# File 'lib/parse/query.rb', line 5882

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



5930
5931
5932
5933
5934
5935
5936
5937
# File 'lib/parse/query.rb', line 5930

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

Parameters:

Returns:

  • (Object)

    the field value



5897
5898
5899
# File 'lib/parse/query.rb', line 5897

def [](key)
  @data[key.to_s] || @data[key.to_sym]
end

#inspectObject



5943
5944
5945
# File 'lib/parse/query.rb', line 5943

def inspect
  "#<Parse::AggregationResult #{to_h.inspect}>"
end

#key?(key) ⇒ Boolean

Check if a key exists

Parameters:

Returns:

  • (Boolean)


5904
5905
5906
# File 'lib/parse/query.rb', line 5904

def key?(key)
  @data.key?(key.to_s) || @data.key?(key.to_sym)
end

#keysArray<Symbol>

Get all keys (snake_case symbols)

Returns:



5910
5911
5912
# File 'lib/parse/query.rb', line 5910

def keys
  @data.keys.select { |k| k.is_a?(Symbol) }
end

#rawHash

Get the raw data as originally received

Returns:



5925
5926
5927
# File 'lib/parse/query.rb', line 5925

def raw
  @raw_data
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


5939
5940
5941
# File 'lib/parse/query.rb', line 5939

def respond_to_missing?(method_name, include_private = false)
  @data.key?(method_name.to_sym) || super
end

#to_hHash Also known as: to_hash

Convert to hash with snake_case symbol keys

Returns:



5916
5917
5918
# File 'lib/parse/query.rb', line 5916

def to_h
  @data.select { |k, _| k.is_a?(Symbol) }
end