1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/lib/active_model/attribute_set.rb

115 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "active_support/core_ext/object/deep_dup"
require "active_model/attribute_set/builder"
require "active_model/attribute_set/yaml_encoder"
module ActiveModel
class AttributeSet # :nodoc:
delegate :each_value, :fetch, :except, to: :attributes
Make Active Record emit significantly smaller YAML This reduces the size of a YAML encoded Active Record object by ~80% depending on the number of columns. There were a number of wasteful things that occurred when we encoded the objects before that have resulted in numerous wins - We were emitting the result of `attributes_before_type_cast` as a hack to work around some laziness issues - The name of an attribute was emitted multiple times, since the attribute objects were in a hash keyed by the name. We now store them in an array instead, and reconstruct the hash using the name - The types were included for every attribute. This would use backrefs if multiple objects were encoded, but really we don't need to include it at all unless it differs from the type at the class level. (The only time that will occur is if the field is the result of a custom select clause) - `original_attribute:` was included over and over and over again since the ivar is almost always `nil`. We've added a custom implementation of `encode_with` on the attribute objects to ensure we don't write the key when the field is `nil`. This isn't without a cost though. Since we're no longer including the types, an object can find itself in an invalid state if the type changes on the class after serialization. This is the same as 4.1 and earlier, but I think it's worth noting. I was worried that I'd introduce some new state bugs as a result of doing this, so I've added an additional test that asserts mutation not being lost as the result of YAML round tripping. Fixes #25145
2016-05-31 14:44:38 -04:00
def initialize(attributes)
@attributes = attributes
end
def [](name)
attributes[name] || Attribute.null(name)
end
Attribute assignment and type casting has nothing to do with columns It's finally finished!!!!!!! The reason the Attributes API was kept private in 4.2 was due to some publicly visible implementation details. It was previously implemented by overloading `columns` and `columns_hash`, to make them return column objects which were modified with the attribute information. This meant that those methods LIED! We didn't change the database schema. We changed the attribute information on the class. That is wrong! It should be the other way around, where schema loading just calls the attributes API for you. And now it does! Yes, this means that there is nothing that happens in automatic schema loading that you couldn't manually do yourself. (There's still some funky cases where we hit the connection adapter that I need to handle, before we can turn off automatic schema detection entirely.) There were a few weird test failures caused by this that had to be fixed. The main source came from the fact that the attribute methods are now defined in terms of `attribute_names`, which has a clause like `return [] unless table_exists?`. I don't *think* this is an issue, since the only place this caused failures were in a fake adapter which didn't override `table_exists?`. Additionally, there were a few cases where tests were failing because a migration was run, but the model was not reloaded. I'm not sure why these started failing from this change, I might need to clear an additional cache in `reload_schema_from_cache`. Again, since this is not normal usage, and it's expected that `reset_column_information` will be called after the table is modified, I don't think it's a problem. Still, test failures that were unrelated to the change are worrying, and I need to dig into them further. Finally, I spent a lot of time debugging issues with the mutex used in `define_attribute_methods`. I think we can just remove that method entirely, and define the attribute methods *manually* in the call to `define_attribute`, which would simplify the code *tremendously*. Ok. now to make this damn thing public, and work on moving it up to Active Model.
2015-01-30 16:03:36 -05:00
def []=(name, value)
attributes[name] = value
end
def values_before_type_cast
attributes.transform_values(&:value_before_type_cast)
end
def to_hash
initialized_attributes.transform_values(&:value)
end
alias_method :to_h, :to_hash
def key?(name)
attributes.key?(name) && self[name].initialized?
end
def keys
attributes.each_key.select { |name| self[name].initialized? }
end
if defined?(JRUBY_VERSION)
# This form is significantly faster on JRuby, and this is one of our biggest hotspots.
# https://github.com/jruby/jruby/pull/2562
def fetch_value(name, &block)
self[name].value(&block)
end
else
def fetch_value(name)
self[name].value { |n| yield n if block_given? }
end
end
def write_from_database(name, value)
attributes[name] = self[name].with_value_from_database(value)
end
def write_from_user(name, value)
attributes[name] = self[name].with_value_from_user(value)
end
def write_cast_value(name, value)
attributes[name] = self[name].with_cast_value(value)
end
def freeze
@attributes.freeze
super
end
def deep_dup
self.class.allocate.tap do |copy|
copy.instance_variable_set(:@attributes, attributes.deep_dup)
end
end
def initialize_dup(_)
@attributes = attributes.dup
super
end
def initialize_clone(_)
@attributes = attributes.clone
super
end
def reset(key)
if key?(key)
write_from_database(key, nil)
end
end
def accessed
attributes.select { |_, attr| attr.has_been_read? }.keys
end
def map(&block)
new_attributes = attributes.transform_values(&block)
AttributeSet.new(new_attributes)
end
def ==(other)
attributes == other.attributes
end
protected
attr_reader :attributes
private
def initialized_attributes
attributes.select { |_, attr| attr.initialized? }
end
end
end