Class: ActiveModel::Attribute::UserProvidedDefault

Inherits:
FromUser show all
Defined in:
activemodel/lib/active_model/attribute/user_provided_default.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from ActiveModel::Attribute

#name, #type

Instance Method Summary collapse

Methods inherited from ActiveModel::Attribute

#==, #came_from_user?, #changed?, #changed_in_place?, #encode_with, #forgetting_assignment, from_database, from_user, #has_been_read?, #hash, #init_with, #initialized?, null, #original_value, #type_cast, uninitialized, #value, #value_for_database, #with_cast_value, with_cast_value, #with_value_from_database, #with_value_from_user

Constructor Details

#initialize(name, value, type, database_default) ⇒ UserProvidedDefault

Returns a new instance of UserProvidedDefault.



8
9
10
11
# File 'activemodel/lib/active_model/attribute/user_provided_default.rb', line 8

def initialize(name, value, type, database_default)
  @user_provided_value = value
  super(name, value, type, database_default)
end

Instance Method Details

#marshal_dumpObject



25
26
27
28
29
30
31
32
33
34
# File 'activemodel/lib/active_model/attribute/user_provided_default.rb', line 25

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

#marshal_load(values) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'activemodel/lib/active_model/attribute/user_provided_default.rb', line 36

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

#value_before_type_castObject



13
14
15
16
17
18
19
# File 'activemodel/lib/active_model/attribute/user_provided_default.rb', line 13

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

#with_type(type) ⇒ Object



21
22
23
# File 'activemodel/lib/active_model/attribute/user_provided_default.rb', line 21

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