123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Key::Mapping

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: activerecord/lib/active_record/key.rb

Overview

An ordered correspondence between two database keys.

Constant Summary

Class Method Summary

Instance Attribute Summary

::Enumerable - Included

#many?

Returns true if the enumerable has more than 1 element.

Instance Method Summary

::Enumerable - Included

#compact_blank

Returns a new ::Array without the blank items.

#exclude?

The negative of the Enumerable#include?.

#excluding

Returns a copy of the enumerable excluding the specified elements.

#in_order_of

Returns a new ::Array where the order has been set to that provided in the series, based on the key of the objects in the original enumerable.

#including

Returns a new array that includes the passed elements.

#index_by

Convert an enumerable to a hash, using the block result as the key and the element as the value.

#index_with

Convert an enumerable to a hash, using the element as the key and the block result as the value.

#maximum

Calculates the maximum from the extracted elements.

#minimum

Calculates the minimum from the extracted elements.

#pick

Extract the given key from the first element in the enumerable.

#pluck

Extract the given key from each element in the enumerable.

#sole

Returns the sole item in the enumerable.

#without
#as_json

::ActiveSupport::EnumerableCoreExt::Constants - Included

Constructor Details

.new(reference_key:, target_key:) ⇒ Mapping

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 398

def initialize(reference_key:, target_key:)
  @reference_key = reference_key
  @target_key = target_key
  if @reference_key.length != @target_key.length
    raise ArgumentError, "Key mappings must have the same number of columns"
  end
  @pairs = @reference_key.zip(@target_key).map!(&:freeze).freeze
  @hash = [@reference_key, @target_key].hash
  freeze
end

Class Method Details

.empty

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 394

def self.empty
  EMPTY
end

Instance Attribute Details

#empty? ⇒ Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 413

def empty?
  @pairs.empty?
end

#hash (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 445

attr_reader :hash

#reference_key (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 392

attr_reader :reference_key, :target_key

#target_key (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 392

attr_reader :reference_key, :target_key

Instance Method Details

#+(other)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 428

def +(other)
  return other if empty?
  return self if other.empty?

  self.class.new(
    reference_key: Key.for([*@reference_key, *other.reference_key]),
    target_key: Key.for([*@target_key, *other.target_key])
  )
end

#==(other) Also known as: #eql?

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 438

def ==(other)
  other.is_a?(Mapping) &&
    reference_key == other.reference_key &&
    target_key == other.target_key
end

#each(&block)

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 409

def each(&block)
  @pairs.each(&block)
end

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 443

alias_method :eql?, :==

#normalize

Returns an equivalent mapping with single-column composite keys converted to scalar keys.

[ GitHub ]

  
# File 'activerecord/lib/active_record/key.rb', line 419

def normalize
  return self unless @reference_key.length == 1 && (@reference_key.composite? || @target_key.composite?)

  self.class.new(
    reference_key: @reference_key.composite? ? Key.for(@reference_key.columns.first) : @reference_key,
    target_key: @target_key.composite? ? Key.for(@target_key.columns.first) : @target_key
  )
end