123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::IndexedRow::Mutable

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveModel::IndexedRow
Defined in: activemodel/lib/active_model/indexed_row.rb

Constant Summary

::ActiveModel::IndexedRow - Inherited

UNSET

Class Method Summary

::ActiveModel::IndexedRow - Inherited

.[], .build_indexes,
.key_at?

Whether #row, an ::Array laid out by slot, has a value at index.

.max_slot

The largest slot in #indexes, or -1 if it is empty.

.new,
.unset_row

A row of #size slots that are all unset, to be filled in by position.

Instance Attribute Summary

Instance Method Summary

::ActiveModel::IndexedRow - Inherited

#==

: (untyped) -> bool.

#[]

: (String) -> untyped.

#at

Like #[], but looks the value up by its slot in the row rather than by name.

#each_key

: () ?{ (String) -> void } -> (Array | Enumerator[String, Array[String]]).

#each_value

: () ?{ (untyped) -> void } -> (self | Enumerator[untyped, self]).

#eql?,
#except

: (*String) -> Hash[String, untyped].

#fetch

: (String) ?{ () -> untyped } -> untyped.

#fetch_at

Like #fetch, but looks the value up by its slot in the row rather than by name.

#hash,
#key?

: (String) -> bool.

#key_at?

Like #key?, but looks the column up by its index in the row.

#keys

: () -> Array.

#length

Alias for #size.

#size

: () -> ::Integer.

#to_h

: () -> Hash[String, untyped].

#to_hash

Alias for #to_h.

#transform_row

Like #transform_values, but returns a Mutable row with the same indexes, so the new row keeps the same layout.

#transform_values

: () ?{ (untyped) -> untyped } -> (Hash[String, untyped] | Enumerator[untyped, Hash[String, untyped]]).

#values

: () -> Array.

#values_at

: (*String) -> Array.

#index_for

The position of column in the row, or nil if the row doesn't have it.

Constructor Details

.new(indexes, row = nil, size: indexes.size) ⇒ Mutable

: (Hash[String, Integer] | ExtendedIndexes, ?Array?, ?size: ::Integer) -> void

[ GitHub ]

  
# File 'activemodel/lib/active_model/indexed_row.rb', line 366

def initialize(indexes, row = nil, size: indexes.size)
  @indexes = indexes
  @row = row || IndexedRow.unset_row(size)
end

Class Method Details

.[](hash)

Builds a Mutable row from a ::Hash, indexed in the Hash's order. : (Hash[String, untyped] | Mutable) -> Mutable

[ GitHub ]

  
# File 'activemodel/lib/active_model/indexed_row.rb', line 359

def self.[](hash)
  return hash if hash.is_a?(self)

  new(IndexedRow.build_indexes(hash.keys).freeze, hash.values)
end

Instance Method Details

#[]=(column, value)

Columns that aren't in the indexes are added after the last index. The indexes are replaced rather than modified, since they're usually shared with other rows. : (String, untyped) -> untyped

[ GitHub ]

  
# File 'activemodel/lib/active_model/indexed_row.rb', line 392

def []=(column, value)
  unless index = @indexes[column]
    index = [@row.size, IndexedRow.max_slot(@indexes) + 1].max
    @indexes = ExtendedIndexes.add(@indexes, column, index)
  end

  if index >= @row.size
    @row.concat(IndexedRow.unset_row(index - @row.size + 1))
  end

  @row[index] = value
end

#freeze

: () -> self

[ GitHub ]

  
# File 'activemodel/lib/active_model/indexed_row.rb', line 378

def freeze
  @row.freeze
  super
end

#initialize_copy(other)

: (Mutable) -> void

[ GitHub ]

  
# File 'activemodel/lib/active_model/indexed_row.rb', line 372

def initialize_copy(other)
  super
  @row = @row.dup
end

#length

Alias for #size.

[ GitHub ]

  
# File 'activemodel/lib/active_model/indexed_row.rb', line 386

alias_method :length, :size

#size Also known as: #length

[ GitHub ]

  
# File 'activemodel/lib/active_model/indexed_row.rb', line 383

def size
  @row.size - @row.count(UNSET)
end