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
-
UNSET =
private
# File 'activemodel/lib/active_model/indexed_row.rb', line 121Module.new.freeze
Class Method Summary
- .new(indexes, row = nil) ⇒ Mutable constructor
::ActiveModel::IndexedRow - Inherited
Instance Attribute Summary
::ActiveModel::IndexedRow - Inherited
Instance Method Summary
- #[](column)
- #[]=(column, value)
- #fetch(column)
- #key?(column) ⇒ Boolean
- #keys
-
#length
Alias for #size.
- #size (also: #length)
- #to_h (also: #to_hash)
-
#to_hash
Alias for #to_h.
- #values
- #values_at(*columns)
::ActiveModel::IndexedRow - Inherited
Constructor Details
.new(indexes, row = nil) ⇒ Mutable
Instance Method Details
#[](column)
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 157
def [](column) if index = @indexes[column] value = @row[index] return if UNSET.equal?(value) value end end
#[]=(column, value)
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 165
def []=(column, value) if index = @indexes[column] @row[index] = value else raise KeyError, "key not found: #{column.inspect}" end end
#fetch(column)
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 173
def fetch(column) if index = @indexes[column] value = @row[index] if UNSET.equal?(value) if block_given? yield else raise KeyError, "key not found: #{column.inspect}" end else value end elsif block_given? yield else raise KeyError, "key not found: #{column.inspect}" end end
#key?(column) ⇒ Boolean
# File 'activemodel/lib/active_model/indexed_row.rb', line 138
def key?(column) index = @indexes[column] index && !UNSET.equal?(@row[index]) end
#keys
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 134
def keys @indexes.reject { |k, v| UNSET.equal?(@row[v]) }.keys end
#length
Alias for #size.
# File 'activemodel/lib/active_model/indexed_row.rb', line 132
alias_method :length, :size
#size Also known as: #length
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 129
def size @indexes.size - @row.count(UNSET) end
#to_h Also known as: #to_hash
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 192
def to_h @indexes.transform_values { |index| @row[index] }.reject { |k, v| UNSET.equal?(v) } end
#to_hash
Alias for #to_h.
#values
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 143
def values @row.reject { |v| UNSET.equal?(v) } end
#values_at(*columns)
[ GitHub ]# File 'activemodel/lib/active_model/indexed_row.rb', line 147
def values_at(*columns) columns.map! do |column| if index = @indexes[column] value = @row[index] UNSET.equal?(value) ? nil : value end end columns end