Class: ActiveModel::IndexedRow
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Inherits: | Object |
| Defined in: | activemodel/lib/active_model/indexed_row.rb |
Constant Summary
-
UNSET =
private
# File 'activemodel/lib/active_model/indexed_row.rb', line 5Module.new.freeze
Class Method Summary
- .[](hash)
- .build_indexes(columns)
- .key_at?(row, index) ⇒ Boolean
-
.max_slot(indexes)
The largest slot in #indexes, or -1 if it is empty.
- .new(indexes, row) ⇒ IndexedRow constructor
-
.unset_row(size)
A row of #size slots that are all unset, to be filled in by position.
Instance Attribute Summary
Instance Method Summary
-
#==(other)
: (untyped) -> bool.
-
#[](column)
: (String) -> untyped.
-
#at(index)
Like #[], but looks the value up by its slot in the row rather than by name.
-
#each_key(&block)
: () ?{ (String) -> void } -> (Array | Enumerator[String, Array[String]]).
-
#each_value
: () ?{ (untyped) -> void } -> (self | Enumerator[untyped, self]).
- #eql?(other) ⇒ Boolean
-
#except(*columns)
: (*String) -> Hash[String, untyped].
-
#fetch(column)
: (String) ?{ () -> untyped } -> untyped.
-
#fetch_at(index)
Like #fetch, but looks the value up by its slot in the row rather than by name.
- #hash
-
#key?(column) ⇒ Boolean
: (String) -> bool.
-
#key_at?(index) ⇒ Boolean
Like #key?, but looks the column up by its index in the row.
-
#keys
: () -> Array.
-
#length
Alias for #size.
-
#size
(also: #length)
: () ->
::Integer. -
#to_h
(also: #to_hash)
: () -> Hash[String, untyped].
-
#to_hash
Alias for #to_h.
-
#transform_row
Like #transform_values, but returns a
Mutablerow 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(*columns)
: (*String) -> Array.
-
#index_for(column)
private
The position of
columnin the row, ornilif the row doesn't have it.
Constructor Details
.new(indexes, row) ⇒ IndexedRow
Class Method Details
.[](hash)
[ GitHub ].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
[ GitHub ]
.max_slot(indexes)
The largest slot in #indexes, or -1 if it is empty.
: (Hash[String, Integer] | IndexedRow::ExtendedIndexes) -> ::Integer
# File 'activemodel/lib/active_model/indexed_row.rb', line 17
def max_slot(indexes) indexes.each_value.max || -1 end
.unset_row(size)
[ GitHub ]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
#[](column)
: (String) -> untyped
# 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
# 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]])
# 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])
#eql?(other) ⇒ Boolean
#except(*columns)
: (*String) -> Hash[String, untyped]
# File 'activemodel/lib/active_model/indexed_row.rb', line 196
def except(*columns) to_h.except(*columns) end
#fetch(column)
: (String) ?{ () -> untyped } -> untyped
# 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
#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?
#key?(column) ⇒ Boolean
: (String) -> bool
# 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
# File 'activemodel/lib/active_model/indexed_row.rb', line 151
def key_at?(index) IndexedRow.key_at?(@row, index) end
#keys
: () -> Array
#length
Alias for #size.
# File 'activemodel/lib/active_model/indexed_row.rb', line 52
alias_method :length, :size
#size Also known as: #length
: () -> ::Integer
# 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]
# File 'activemodel/lib/active_model/indexed_row.rb', line 156
def to_h transform_values(&:itself) end
#to_hash
Alias for #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])
#transform_values
: () ?{ (untyped) -> untyped } -> (Hash[String, untyped] | Enumerator[untyped, Hash[String, untyped]])
#values
: () -> Array
# File 'activemodel/lib/active_model/indexed_row.rb', line 67
def values @row.reject { |v| UNSET.equal?(v) } end
#values_at(*columns)
: (*String) -> Array
# 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