Sha256: cd951864be1760b77d42e6d1523369a48ad7c0005123a94155d5f1a737637a5e

Contents?: true

Size: 1.24 KB

Versions: 33

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require "active_model/attribute"

module ActiveModel
  class Attribute # :nodoc:
    class UserProvidedDefault < FromUser # :nodoc:
      def initialize(name, value, type, database_default)
        @user_provided_value = value
        super(name, value, type, database_default)
      end

      def value_before_type_cast
        if user_provided_value.is_a?(Proc)
          @memoized_value_before_type_cast ||= user_provided_value.call
        else
          @user_provided_value
        end
      end

      def with_type(type)
        self.class.new(name, user_provided_value, type, original_attribute)
      end

      def marshal_dump
        result = [
          name,
          value_before_type_cast,
          type,
          original_attribute,
        ]
        result << value if defined?(@value)
        result
      end

      def marshal_load(values)
        name, user_provided_value, type, original_attribute, value = values
        @name = name
        @user_provided_value = user_provided_value
        @type = type
        @original_attribute = original_attribute
        if values.length == 5
          @value = value
        end
      end

      protected

        attr_reader :user_provided_value
    end
  end
end

Version data entries

33 entries across 33 versions & 4 rubygems

Version Path
activemodel-5.2.8.1 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.8 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.7.1 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.7 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.6.3 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.6.2 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.6.1 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.6 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4.6 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.5 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4.5 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4.4 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4.3 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4.2 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4.1 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4 lib/active_model/attribute/user_provided_default.rb
activemodel-5.2.4.rc1 lib/active_model/attribute/user_provided_default.rb
spiral_form-0.1.1 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/attribute/user_provided_default.rb
spiral_form-0.1.0 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/attribute/user_provided_default.rb
ric-0.13.0 vendor/bundle/ruby/2.5.0/gems/activemodel-5.2.3/lib/active_model/attribute/user_provided_default.rb