diff options
author | Lisa Ugray <lisa.ugray@shopify.com> | 2017-10-19 12:45:07 -0400 |
---|---|---|
committer | Lisa Ugray <lisa.ugray@shopify.com> | 2017-11-09 14:29:39 -0500 |
commit | c3675f50d2e59b7fc173d7b332860c4b1a24a726 (patch) | |
tree | 736119c8ea9b683ac465c07e6a640d7e14bbc1b0 /activemodel/lib/active_model/attribute_set/yaml_encoder.rb | |
parent | dac7c8844b4d9944eaa0fca98b45ee478cdb7201 (diff) | |
download | rails-c3675f50d2e59b7fc173d7b332860c4b1a24a726.tar.gz rails-c3675f50d2e59b7fc173d7b332860c4b1a24a726.tar.bz2 rails-c3675f50d2e59b7fc173d7b332860c4b1a24a726.zip |
Move Attribute and AttributeSet to ActiveModel
Use these to back the attributes API. Stop automatically including
ActiveModel::Dirty in ActiveModel::Attributes, and make it optional.
Diffstat (limited to 'activemodel/lib/active_model/attribute_set/yaml_encoder.rb')
-rw-r--r-- | activemodel/lib/active_model/attribute_set/yaml_encoder.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/attribute_set/yaml_encoder.rb b/activemodel/lib/active_model/attribute_set/yaml_encoder.rb new file mode 100644 index 0000000000..4ea945b956 --- /dev/null +++ b/activemodel/lib/active_model/attribute_set/yaml_encoder.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module ActiveModel + class AttributeSet + # Attempts to do more intelligent YAML dumping of an + # ActiveModel::AttributeSet to reduce the size of the resulting string + class YAMLEncoder # :nodoc: + def initialize(default_types) + @default_types = default_types + end + + def encode(attribute_set, coder) + coder["concise_attributes"] = attribute_set.each_value.map do |attr| + if attr.type.equal?(default_types[attr.name]) + attr.with_type(nil) + else + attr + end + end + end + + def decode(coder) + if coder["attributes"] + coder["attributes"] + else + attributes_hash = Hash[coder["concise_attributes"].map do |attr| + if attr.type.nil? + attr = attr.with_type(default_types[attr.name]) + end + [attr.name, attr] + end] + AttributeSet.new(attributes_hash) + end + end + + protected + + attr_reader :default_types + end + end +end |