Class: ActiveModel::Attribute::UserProvidedDefault

Inherits:
FromUser show all
Defined in:
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, #original_value_for_database, #serializable?, #type_cast, uninitialized, #value, #value_for_database, #with_cast_value, with_cast_value, #with_user_default, #with_value_from_database, #with_value_from_user

Constructor Details

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

Returns a new instance of UserProvidedDefault.



12
13
14
15
# File 'lib/active_model/attribute/user_provided_default.rb', line 12

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

Instance Method Details

#marshal_dumpObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/active_model/attribute/user_provided_default.rb', line 29

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

#marshal_load(values) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/active_model/attribute/user_provided_default.rb', line 40

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



17
18
19
20
21
22
23
# File 'lib/active_model/attribute/user_provided_default.rb', line 17

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



25
26
27
# File 'lib/active_model/attribute/user_provided_default.rb', line 25

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