Sha256: bc1533ccae11860063b378810407a84c8ecac954c6978b937ac8d0480f8d101f

Contents?: true

Size: 1.24 KB

Versions: 121

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

      private
        attr_reader :user_provided_value
    end
  end
end

Version data entries

121 entries across 117 versions & 12 rubygems

Version Path
activemodel-7.0.8.6 lib/active_model/attribute/user_provided_default.rb
activemodel-6.1.7.10 lib/active_model/attribute/user_provided_default.rb
activemodel-6.1.7.9 lib/active_model/attribute/user_provided_default.rb
activemodel-7.0.8.5 lib/active_model/attribute/user_provided_default.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activemodel-7.0.8.4/lib/active_model/attribute/user_provided_default.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/attribute/user_provided_default.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/attribute/user_provided_default.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/attribute/user_provided_default.rb
activemodel-7.0.8.4 lib/active_model/attribute/user_provided_default.rb
activemodel-6.1.7.8 lib/active_model/attribute/user_provided_default.rb
activemodel-7.0.8.1 lib/active_model/attribute/user_provided_default.rb
activemodel-6.1.7.7 lib/active_model/attribute/user_provided_default.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-6.1.6.1/lib/active_model/attribute/user_provided_default.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-7.0.3.1/lib/active_model/attribute/user_provided_default.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-7.0.2.3/lib/active_model/attribute/user_provided_default.rb
activemodel-7.0.8 lib/active_model/attribute/user_provided_default.rb
activemodel-7.0.7.2 lib/active_model/attribute/user_provided_default.rb
activemodel-6.1.7.6 lib/active_model/attribute/user_provided_default.rb
activemodel-7.0.7.1 lib/active_model/attribute/user_provided_default.rb
activemodel-6.1.7.5 lib/active_model/attribute/user_provided_default.rb