Class: ActiveModel::AttributeSet::Builder
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Inherits: | Object |
| Defined in: | activemodel/lib/active_model/attribute_set/builder.rb |
Constant Summary
Class Method Summary
-
.new(types, default_attributes, default_keys) ⇒ Builder
constructor
#default_keys limits which of #default_attributes records loaded from the database fall back to (anything responding to #include?).
Instance Attribute Summary
- #default_attributes readonly
- #default_keys readonly
- #indexes readonly
- #types rw
- #from_db_context=(value) writeonly protected
- #types=(value) rw protected
Instance Method Summary
-
#build_from_database(values = {}, additional_types = {})
Only for backwards compatibility with gems that call this with Hashes (like paquito).
-
#build_from_indexed_row(values)
#values is an
::ActiveModel::IndexedRowof raw values from the database. - #build_from_initialize
-
#build_from_row(row, indexes)
Like #build_from_indexed_row, but takes the row
::Arrayand the indexes it's laid out with, which can type the names after the ones in #indexes, such as aliases (see #typed_indexes). -
#relayout(row)
Returns
rowif it's laid out with #indexes (possibly with extra names, such as aliases, after them). -
#relayout_attributes(attribute_set)
Copies the attributes of
attribute_set, which may be laid out some other way (as when decoded from YAML), into a set laid out with #indexes, so it can be read by index. -
#row_from_hash(hash)
Lays
hashout like a row loaded from the database: each name at its index in #indexes, and names it doesn't have after the last one. -
#typed_indexes(indexes, column_types)
Returns #indexes with the types in
column_types(a::Hashof types by name) for the names it has after the ones in #indexes, such as aliases (see #build_from_row). - #with_types(types)
-
#indexes_for(types, indexes)
private
: (Hash[String, Type::Value], Hash[String, Integer] |
::ActiveModel::IndexedRow::ExtendedIndexes) -> (Hash[String, Integer] |::ActiveModel::IndexedRow::ExtendedIndexes). -
#names_for(indexes)
private
: (Hash[String, Integer] |
::ActiveModel::IndexedRow::ExtendedIndexes) -> Array.
Constructor Details
.new(types, default_attributes, default_keys) ⇒ Builder
#default_keys limits which of #default_attributes records loaded from
the database fall back to (anything responding to ActiveModel::AttributeSet#include?).
: (Hash[String, Type::Value], ::ActiveModel::AttributeSet, untyped) -> void
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 93
def initialize(types, default_attributes, default_keys) @types = types @default_attributes = default_attributes @default_keys = default_keys # Rows loaded from the database are laid out with these, so a row has # an index for every attribute of the model: the columns first, then # the other attributes, then any typed names the defaults lack. @indexes = indexes_for(types, default_attributes.indexes) @names = names_for(@indexes) @defaults = @names.map { |name| default_attributes.fetch(name) { nil } }.freeze type_table = TypeTable.new(@names, types) @from_db_context = FromDBContext.new(@names, type_table, @defaults, default_keys) @from_init_context = FromInitContext.new(@names, type_table, @defaults) freeze end
Instance Attribute Details
#default_attributes (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 88
attr_reader :types, :default_attributes, :default_keys, :indexes
#default_keys (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 88
attr_reader :types, :default_attributes, :default_keys, :indexes
#from_db_context=(value) (writeonly, protected)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 208
attr_writer :types, :from_db_context
#indexes (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 88
attr_reader :types, :default_attributes, :default_keys, :indexes
#types (rw)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 88
attr_reader :types, :default_attributes, :default_keys, :indexes
#types=(value) (rw, protected)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 208
attr_writer :types, :from_db_context
Instance Method Details
#build_from_database(values = {}, additional_types = {})
Only for backwards compatibility with gems that call this with
Hashes (like paquito). Don't use it; use #build_from_indexed_row.
: (?Hash[String, untyped], ?Hash[String, Type::Value]) -> ::ActiveModel::LazyIndexedAttributeSet
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 114
def build_from_database(values = {}, additional_types = {}) row = row_from_hash(values) return build_from_indexed_row(row) if additional_types.empty? builder = with_types(additional_types) builder.build_from_row(row.row, builder.typed_indexes(row.indexes, additional_types)) end
#build_from_indexed_row(values)
ActiveModel::AttributeSet#values is an ::ActiveModel::IndexedRow of raw values from the database. Use
#row_from_hash to build one from a ::Hash.
: (IndexedRow) -> ::ActiveModel::LazyIndexedAttributeSet
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 125
def build_from_indexed_row(values) build_from_row(values.row, values.indexes) end
#build_from_initialize
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 138
def build_from_initialize LazyIndexedAttributeSet.new(EMPTY_ROW, @indexes, @from_init_context) end
#build_from_row(row, indexes)
Like #build_from_indexed_row, but takes the row ::Array and the indexes
it's laid out with, which can type the names after the ones in
#indexes, such as aliases (see #typed_indexes).
: (Array, Hash[String, Integer] | ::ActiveModel::IndexedRow::ExtendedIndexes) -> ::ActiveModel::LazyIndexedAttributeSet
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 133
def build_from_row(row, indexes) LazyIndexedAttributeSet.new(row, indexes, @from_db_context) end
#indexes_for(types, indexes) (private)
: (Hash[String, Type::Value], Hash[String, Integer] | ::ActiveModel::IndexedRow::ExtendedIndexes) -> (Hash[String, Integer] | ::ActiveModel::IndexedRow::ExtendedIndexes)
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 219
def indexes_for(types, indexes) missing = types.each_key.reject { |name| indexes.key?(name) } return indexes if missing.empty? last_slot = IndexedRow.max_slot(indexes) IndexedRow::ExtendedIndexes.new(indexes, missing.index_with { last_slot += 1 }.freeze) end
#names_for(indexes) (private)
: (Hash[String, Integer] | ::ActiveModel::IndexedRow::ExtendedIndexes) -> Array
#relayout(row)
Returns row if it's laid out with #indexes (possibly with extra
names, such as aliases, after them). Otherwise copies it into that
layout, as when a row laid out for an STI parent is loaded as a
subclass that declares attributes of its own.
: (IndexedRow) -> ::ActiveModel::IndexedRow
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 156
def relayout(row) layout = row.indexes layout = layout.indexes if layout.is_a?(IndexedRow::ExtendedIndexes) return row if layout.equal?(@indexes) new_row = IndexedRow::Mutable.new(@indexes) row.indexes.each_key { |name| new_row[name] = row.fetch(name) if row.key?(name) } new_row.freeze end
#relayout_attributes(attribute_set)
Copies the attributes of attribute_set, which may be laid out some
other way (as when decoded from YAML), into a set laid out with
#indexes, so it can be read by index.
: (AttributeSet) -> ::ActiveModel::AttributeSet
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 170
def relayout_attributes(attribute_set) attributes = IndexedRow::Mutable.new(@indexes) attribute_set.each_value { |attribute| attributes[attribute.name] = attribute } AttributeSet.new(attributes) end
#row_from_hash(hash)
Lays hash out like a row loaded from the database: each name at
its index in #indexes, and names it doesn't have after the last one.
: (Hash[String, untyped]) -> ::ActiveModel::IndexedRow::Mutable
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 145
def row_from_hash(hash) row = IndexedRow::Mutable.new(@indexes) hash.each { |name, value| row[name] = value } row.freeze end
#typed_indexes(indexes, column_types)
Returns #indexes with the types in column_types (a ::Hash of types
by name) for the names it has after the ones in #indexes, such as
aliases (see #build_from_row).
: (Hash[String, Integer] | ::ActiveModel::IndexedRow::ExtendedIndexes, Hash[String, Type::Value]) -> (Hash[String, Integer] | ::ActiveModel::IndexedRow::ExtendedIndexes)
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 195
def typed_indexes(indexes, column_types) return indexes if column_types.empty? || !indexes.is_a?(IndexedRow::ExtendedIndexes) types = {} indexes.extra.each do |name, slot| types[slot] = column_types[name] if slot >= @names.size && column_types[name] end return indexes if types.empty? IndexedRow::ExtendedIndexes.new(indexes.indexes, indexes.extra, types.freeze) end
#with_types(types)
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 181
def with_types(types) overrides = types.select { |name, _| @indexes.key?(name) } return self if overrides.empty? copy = dup copy.types = @types.merge(overrides) copy.from_db_context = FromDBContext.new(@names, TypeTable.new(@names, copy.types), @defaults, @default_keys) copy.freeze end