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

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 24

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

Class Method Details

.[](hash)

[ GitHub ]

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

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 16

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

Instance Attribute Details

#indexes (readonly)

[ GitHub ]

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

attr_reader :indexes, :row

#row (readonly)

[ GitHub ]

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

attr_reader :indexes, :row

Instance Method Details

#==(other)

[ GitHub ]

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

def ==(other)
  if other.is_a?(Hash)
    to_hash == other
  else
    super
  end
end

#[](column)

[ GitHub ]

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

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

#each_key(&block)

[ GitHub ]

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

def each_key(&block)
  @indexes.each_key(&block)
end

#eql?(other) ⇒ Boolean

[ GitHub ]

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

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

#fetch(column)

[ GitHub ]

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

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

#hash

[ GitHub ]

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

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

#key?(column) ⇒ Boolean

[ GitHub ]

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

def key?(column)
  @indexes.key?(column)
end

#keys

[ GitHub ]

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

def keys
  @indexes.keys
end

#length

Alias for #size.

[ GitHub ]

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

alias_method :length, :size

#new_empty_mutable_row

[ GitHub ]

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

def new_empty_mutable_row
  Mutable.new(@indexes)
end

#size Also known as: #length

[ GitHub ]

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

def size
  @indexes.size
end

#to_h Also known as: #to_hash

[ GitHub ]

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

def to_h
  @indexes.transform_values { |index| @row[index] }
end

#to_hash

Alias for #to_h.

[ GitHub ]

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

alias_method :to_hash, :to_h

#values

[ GitHub ]

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

def values
  @row.dup
end

#values_at(*columns)

[ GitHub ]

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

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