Sha256: bbb7cbd654f71c3e1b8b5081d412e25c7051dca6ba3e11a1a9382853e8e929b8

Contents?: true

Size: 1.03 KB

Versions: 164

Compression:

Stored size: 1.03 KB

Contents

# 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

      private
        attr_reader :default_types
    end
  end
end

Version data entries

164 entries across 159 versions & 16 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activemodel-7.1.3.4/lib/active_model/attribute_set/yaml_encoder.rb
activemodel-8.0.1 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-8.0.0.1 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.2.2.1 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.1.5.1 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.0.8.7 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-8.0.0 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.2.2 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.1.5 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-8.0.0.rc2 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.2.1.2 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.1.4.2 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.0.8.6 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-6.1.7.10 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-8.0.0.rc1 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-6.1.7.9 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.2.1.1 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.1.4.1 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-7.0.8.5 lib/active_model/attribute_set/yaml_encoder.rb
activemodel-8.0.0.beta1 lib/active_model/attribute_set/yaml_encoder.rb