Class: ActiveRecord::Result::RowLayout
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | activerecord/lib/active_record/result.rb |
Overview
Lays rows out the way a column map expects. Each column's slot is the index generated in the reader/writer methods, so the values in a row need to line up with those indexes.
"Pending" columns are columns the schema didn't know about, so no methods were generated for them, for example column aliases. They are given indexes after the last one in the column map, and rows are laid out with a proxy that wraps the column map with those extra indexes (and the types the database gave them), so the column map itself is never modified.
The LUT is only built once a column is found out of place, so in the common case (selected columns are in schema order) rows are used as-is.
Class Method Summary
-
.new(columns, column_map, column_types = nil) ⇒ RowLayout
constructor
#column_types are the types of #columns, by position, if the database gave them.
Instance Attribute Summary
- #indexes readonly
Instance Method Summary
Constructor Details
.new(columns, column_map, column_types = nil) ⇒ RowLayout
ActiveRecord::Result#column_types are the types of ActiveRecord::Result#columns, by position, if the
database gave them.
: (Array, Hash[String, Integer] | ::ActiveModel::IndexedRow::ExtendedIndexes, ?(Array | Hash[Integer, ActiveModel::Type::Value])?) -> void
# File 'activerecord/lib/active_record/result.rb', line 269
def initialize(columns, column_map, column_types = nil) shuffle_lut = nil pending = nil pending_types = nil last_slot = nil columns.each_with_index do |name, i| unless slot = column_map[name] pending ||= {} last_slot ||= ActiveModel::IndexedRow.max_slot(column_map) slot = pending[name] = (last_slot += 1) if type = column_types&.[](i) (pending_types ||= {})[slot] = type end end if shuffle_lut shuffle_lut << slot elsif slot != i shuffle_lut = (0...i).to_a << slot end end @indexes = if pending_types ActiveModel::IndexedRow::ExtendedIndexes.new(column_map, pending.freeze, pending_types.freeze) elsif pending ActiveModel::IndexedRow::ExtendedIndexes.new(column_map, pending.freeze) else column_map end @shuffle_lut = shuffle_lut @width = shuffle_lut.max + 1 if shuffle_lut end
Instance Attribute Details
#indexes (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/result.rb', line 264
attr_reader :indexes
Instance Method Details
#build(row)
[ GitHub ]# File 'activerecord/lib/active_record/result.rb', line 305
def build(row) return row unless @shuffle_lut shuffled = ActiveModel::IndexedRow.unset_row(@width) @shuffle_lut.each_with_index { |slot, i| shuffled[slot] = row[i] } shuffled.freeze end