123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::IndexedRow

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activemodel/lib/active_model/indexed_row.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(indexes, row) ⇒ IndexedRow

[ GitHub ]

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

def initialize(indexes, row)
  @indexes = indexes
  @row = row
end

Class Method Details

.[](hash)

[ GitHub ]

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

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

  index = -1
  column_indexes = hash.transform_values { index += 1 }.freeze
  new(column_indexes, hash.values.freeze)
end

.build_indexes(columns)

[ GitHub ]

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

def build_indexes(columns)
  index = -1
  columns.index_with { index += 1 }
end

.key_at?(row, index) ⇒ Boolean

Whether #row, an ::Array laid out by slot, has a value at index. : (Array, ::Integer) -> bool

[ GitHub ]

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

def key_at?(row, index)
  index < row.size && !UNSET.equal?(row[index])
end

.max_slot(indexes)

The largest slot in #indexes, or -1 if it is empty. : (Hash[String, Integer] | IndexedRow::ExtendedIndexes) -> ::Integer

[ GitHub ]

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

def max_slot(indexes)
  indexes.each_value.max || -1
end

.unset_row(size)

A row of #size slots that are all unset, to be filled in by position. : (Integer) -> Array

[ GitHub ]

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

def unset_row(size)
  Array.new(size, UNSET)
end

Instance Attribute Details

#indexes (readonly)

[ GitHub ]

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

attr_reader :indexes, :row

#row (readonly)

[ GitHub ]

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

attr_reader :indexes, :row

Instance Method Details

#==(other)

: (untyped) -> bool

[ GitHub ]

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

def ==(other)
  case other
  when Hash
    to_hash == other
  when IndexedRow
    to_hash == other.to_hash
  else
    false
  end
end

#[](column)

: (String) -> untyped

[ GitHub ]

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

def [](column)
  if index = index_for(column)
    @row[index]
  end
end

#at(index)

Like #[], but looks the value up by its slot in the row rather than by name. : (Integer) -> untyped

[ GitHub ]

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

def at(index)
  value = @row[index]
  value unless UNSET.equal?(value)
end

#each_key(&block)

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

[ GitHub ]

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

def each_key(&block)
  keys.each(&block)
end

#each_value

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

[ GitHub ]

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

def each_value
  return enum_for(:each_value) { size } unless block_given?

  @indexes.each_key do |column|
    if index = index_for(column)
      yield @row[index]
    end
  end
  self
end

#eql?(other) ⇒ Boolean

[ GitHub ]

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

def eql?(other)
  other.is_a?(IndexedRow) &&
    @indexes == other.indexes &&
    @row == other.row
end

#except(*columns)

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

[ GitHub ]

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

def except(*columns)
  to_h.except(*columns)
end

#fetch(column)

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

[ GitHub ]

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

def fetch(column)
  if index = index_for(column)
    @row[index]
  elsif block_given?
    yield
  else
    raise KeyError, "key not found: #{column.inspect}"
  end
end

#fetch_at(index)

Like #fetch, but looks the value up by its slot in the row rather than by name. : (Integer) ?{ () -> untyped } -> untyped

[ GitHub ]

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

def fetch_at(index)
  value = index < @row.size ? @row[index] : UNSET

  if !UNSET.equal?(value)
    value
  elsif block_given?
    yield
  else
    raise KeyError, "index not found: #{index.inspect}"
  end
end

#hash

[ GitHub ]

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

def hash
  [IndexedRow, @indexes, @row].hash
end

#index_for(column) (private)

The position of column in the row, or nil if the row doesn't have it. A column map can include columns that weren't selected, so their slot can be past the end of the row or point at an UNSET one. : (String) -> Integer?

[ GitHub ]

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

def index_for(column)
  if index = @indexes[column]
    index if index < @row.size && !UNSET.equal?(@row[index])
  end
end

#key?(column) ⇒ Boolean

: (String) -> bool

[ GitHub ]

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

def key?(column)
  !index_for(column).nil?
end

#key_at?(index) ⇒ Boolean

Like #key?, but looks the column up by its index in the row. : (Integer) -> bool

[ GitHub ]

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

def key_at?(index)
  IndexedRow.key_at?(@row, index)
end

#keys

: () -> Array

[ GitHub ]

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

def keys
  keys = []
  @indexes.each_key { |column| keys << column if key?(column) }
  keys
end

#length

Alias for #size.

[ GitHub ]

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

alias_method :length, :size

#size Also known as: #length

: () -> ::Integer

[ GitHub ]

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

def size
  keys.size
end

#to_h Also known as: #to_hash

: () -> Hash[String, untyped]

[ GitHub ]

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

def to_h
  transform_values(&:itself)
end

#to_hash

Alias for #to_h.

[ GitHub ]

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

alias_method :to_hash, :to_h

#transform_row

Like #transform_values, but returns a IndexedRow::Mutable row with the same indexes, so the new row keeps the same layout. : () ?{ (untyped) -> untyped } -> (Mutable | Enumerator[untyped, Mutable])

[ GitHub ]

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

def transform_row
  return enum_for(:transform_row) { size } unless block_given?

  Mutable.new(@indexes, @row.map { |value| UNSET.equal?(value) ? value : yield(value) })
end

#transform_values

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

[ GitHub ]

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

def transform_values
  return enum_for(:transform_values) { size } unless block_given?

  hash = {}
  @indexes.each_key do |column|
    if index = index_for(column)
      hash[column] = yield(@row[index])
    end
  end
  hash
end

#values

: () -> Array

[ GitHub ]

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

def values
  @row.reject { |v| UNSET.equal?(v) }
end

#values_at(*columns)

: (*String) -> Array

[ GitHub ]

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

def values_at(*columns)
  columns.map! do |column|
    if index = index_for(column)
      @row[index]
    end
  end
  columns
end