Sha256: 99e91b979c0ba13bc5fc4c19090b0a152b9e925c41d9dca33470ceff87ba07dc

Contents?: true

Size: 1.14 KB

Versions: 20

Compression:

Stored size: 1.14 KB

Contents

module ActiveRecord
  class AttributeSet
    # Attempts to do more intelligent YAML dumping of an
    # ActiveRecord::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

      # TODO Change this to private once we've dropped Ruby 2.2 support.
      # Workaround for Ruby 2.2 "private attribute?" warning.
      protected

        attr_reader :default_types
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
activerecord-5.1.7 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.7.rc1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.6.2 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.6.1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.6 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.5 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.5.rc1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.4 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.4.rc1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.3 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.3.rc3 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.3.rc2 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.3.rc1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.2 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.2.rc1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.0 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.0.rc2 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.0.rc1 lib/active_record/attribute_set/yaml_encoder.rb
activerecord-5.1.0.beta1 lib/active_record/attribute_set/yaml_encoder.rb