Class: ActiveRecord::Key::Mapping
| 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
-
EMPTY =
private
# File 'activerecord/lib/active_record/key.rb', line 447begin key = Key.for(nil) new(reference_key: key, target_key: key) end
Class Method Summary
- .empty
- .new(reference_key:, target_key:) ⇒ Mapping constructor
Instance Attribute Summary
- #empty? ⇒ Boolean readonly
- #hash readonly
- #reference_key readonly
- #target_key readonly
::Enumerable - Included
| #many? | Returns |
Instance Method Summary
- #+(other)
- #==(other) (also: #eql?)
- #each(&block)
-
#eql?(other)
Alias for #==.
-
#normalize
Returns an equivalent mapping with single-column composite keys converted to scalar keys.
::Enumerable - Included
| #compact_blank | Returns a new |
| #exclude? | The negative of the |
| #excluding | Returns a copy of the enumerable excluding the specified elements. |
| #in_order_of | Returns a new |
| #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 | Alias for Enumerable#excluding. |
| #as_json | |
::ActiveSupport::EnumerableCoreExt::Constants - Included
Constructor Details
.new(reference_key:, target_key:) ⇒ Mapping
# 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 #==.
# 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.
# 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