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.

Two naming modes:

  • :default (the default): field names are converted from camelCase to snake_case symbols. #to_h returns those symbol keys, and the original string keys stay readable through #[] and #raw.
  • :server: field names are kept exactly as the aggregation returned them. #to_h and #keys use the original String keys ("totalPlays", "ExternalID"), nested values are untouched, and distinct keys whose snake_case forms collide ("totalPlays" and "total_plays") both survive. Method-style access still accepts a snake_case name when it identifies exactly one key, and raises when it is ambiguous.

Examples:

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

server = AggregationResult.new({ "_id" => "Rock", "totalPlays" => 500 }, field_names: :server)
server.to_h          # => { "_id" => "Rock", "totalPlays" => 500 }

Constant Summary collapse

FIELD_NAME_MODES =

Naming modes accepted by field_names: across the SDK's result APIs.

%i[default server].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, field_names: nil) ⇒ AggregationResult

Returns a new instance of AggregationResult.

Parameters:

  • data (Hash) —

    the raw aggregation result hash

  • field_names (Symbol, nil) (defaults to: nil) —

    :default (snake_case symbol keys) or :server (keys exactly as returned).



7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
# File 'lib/parse/query.rb', line 7725

def initialize(data, field_names: nil)
  @field_names = self.class.normalize_field_names!(field_names)
  @data = {}
  @raw_data = data

  if @field_names == :server
    data.each { |key, value| @data[key.to_s] = value }
    return
  end

  # 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



7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
# File 'lib/parse/query.rb', line 7790

def method_missing(method_name, *args, &block)
  if server?
    key = server_key_for(method_name, strict: true)
    return @data[key] if key
    return super
  end
  key = method_name.to_sym
  if @data.key?(key)
    @data[key]
  else
    super
  end
end

Instance Attribute Details

#field_names ⇒ Symbol (readonly)

Returns :default or :server.

Returns:

  • (Symbol) —

    :default or :server.



7720
7721
7722
# File 'lib/parse/query.rb', line 7720

def field_names
  @field_names
end

Class Method Details

.normalize_field_names!(value) ⇒ Symbol

Normalize a field_names: option. nil means :default.

Parameters:

Returns:

  • (Symbol) —

    :default or :server

Raises:

  • (ArgumentError) —

    for any other value.



7711
7712
7713
7714
7715
7716
7717
# File 'lib/parse/query.rb', line 7711

def self.normalize_field_names!(value)
  return :default if value.nil?
  mode = value.respond_to?(:to_sym) ? value.to_sym : value
  return mode if FIELD_NAME_MODES.include?(mode)
  raise ArgumentError,
        "field_names: must be one of #{FIELD_NAME_MODES.inspect} (got #{value.inspect})."
end

Instance Method Details

#[](key) ⇒ Object

Hash-style access with string or symbol keys

Parameters:

Returns:

  • (Object) —

    the field value



7746
7747
7748
7749
# File 'lib/parse/query.rb', line 7746

def [](key)
  return @data[server_key_for(key)] if server?
  @data[key.to_s] || @data[key.to_sym]
end

#inspect ⇒ Object



7809
7810
7811
# File 'lib/parse/query.rb', line 7809

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

#key?(key) ⇒ Boolean

Check if a key exists

Parameters:

Returns:

  • (Boolean)


7754
7755
7756
7757
# File 'lib/parse/query.rb', line 7754

def key?(key)
  return @data.key?(server_key_for(key)) if server?
  @data.key?(key.to_s) || @data.key?(key.to_sym)
end

#keys ⇒ Array<Symbol>, Array<String>

Get all keys: snake_case symbols by default, or the original String keys in :server mode.

Returns:



7762
7763
7764
7765
# File 'lib/parse/query.rb', line 7762

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

#raw ⇒ Hash

Get the raw data as originally received

Returns:



7780
7781
7782
# File 'lib/parse/query.rb', line 7780

def raw
  @raw_data
end

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

Returns:

  • (Boolean)


7804
7805
7806
7807
# File 'lib/parse/query.rb', line 7804

def respond_to_missing?(method_name, include_private = false)
  return !server_key_for(method_name).nil? || super if server?
  @data.key?(method_name.to_sym) || super
end

#server? ⇒ Boolean

Returns true in :server naming mode.

Returns:

  • (Boolean) —

    true in :server naming mode.



7785
7786
7787
# File 'lib/parse/query.rb', line 7785

def server?
  @field_names == :server
end

#to_h ⇒ Hash Also known as: to_hash

Convert to hash: snake_case symbol keys by default, or the original String keys (a copy, nested values untouched) in :server mode.

Returns:



7770
7771
7772
7773
# File 'lib/parse/query.rb', line 7770

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