Class: ActiveModel::Attribute
Do not use. This class is for internal use only.
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | activemodel/lib/active_model/attribute.rb, activemodel/lib/active_model/attribute/user_provided_default.rb |
Class Method Summary
- .from_database(name, value_before_type_cast, type, value = nil)
- .from_user(name, value_before_type_cast, type, original_attribute = nil)
-
.new(name, value_before_type_cast, type, original_attribute = nil, value = nil) ⇒ Attribute
constructor
This method should not be called directly.
- .null(name)
- .uninitialized(name, type)
- .with_cast_value(name, value_before_type_cast, type)
Instance Attribute Summary
- #came_from_user? ⇒ Boolean readonly
- #changed? ⇒ Boolean readonly
- #changed_in_place? ⇒ Boolean readonly
- #has_been_read? ⇒ Boolean readonly
- #initialized? ⇒ Boolean readonly
- #name readonly
- #type readonly
- #value_before_type_cast readonly
-
#assigned?
readonly
private
Alias for #original_attribute.
- #changed_from_assignment? ⇒ Boolean readonly private
- #original_attribute (also: #assigned?) readonly private
Instance Method Summary
- #==(other) (also: #eql?)
- #encode_with(coder)
-
#eql?(other)
Alias for #==.
- #forgetting_assignment
- #hash
- #init_with(coder)
- #original_value
- #original_value_for_database
- #serializable?(&block) ⇒ Boolean
- #type_cast
- #value
- #value_for_database
- #with_cast_value(value)
- #with_type(type)
- #with_user_default(value)
- #with_value_from_database(value)
- #with_value_from_user(value)
- #_original_value_for_database private
- #_value_for_database private
- #initialize_dup(other) private
Constructor Details
.new(name, value_before_type_cast, type, original_attribute = nil, value = nil) ⇒ Attribute
This method should not be called directly. Use #from_database
or #from_user
# File 'activemodel/lib/active_model/attribute.rb', line 33
def initialize(name, value_before_type_cast, type, original_attribute = nil, value = nil) @name = name @value_before_type_cast = value_before_type_cast @type = type @original_attribute = original_attribute @value = value unless value.nil? end
Class Method Details
.from_database(name, value_before_type_cast, type, value = nil)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 8
def from_database(name, value_before_type_cast, type, value = nil) FromDatabase.new(name, value_before_type_cast, type, nil, value) end
.from_user(name, value_before_type_cast, type, original_attribute = nil)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 12
def from_user(name, value_before_type_cast, type, original_attribute = nil) FromUser.new(name, value_before_type_cast, type, original_attribute) end
.null(name)
[ GitHub ].uninitialized(name, type)
[ GitHub ].with_cast_value(name, value_before_type_cast, type)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 16
def with_cast_value(name, value_before_type_cast, type) WithCastValue.new(name, value_before_type_cast, type) end
Instance Attribute Details
#assigned? (readonly, private)
Alias for #original_attribute.
# File 'activemodel/lib/active_model/attribute.rb', line 153
alias :assigned? :original_attribute
#came_from_user? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute.rb', line 107
def came_from_user? false end
#changed? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute.rb', line 66
def changed? changed_from_assignment? || changed_in_place? end
#changed_from_assignment? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute.rb', line 161
def changed_from_assignment? assigned? && type.changed?(original_value, value, value_before_type_cast) end
#changed_in_place? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute.rb', line 70
def changed_in_place? has_been_read? && type.changed_in_place?(original_value_for_database, value) end
#has_been_read? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute.rb', line 111
def has_been_read? defined?(@value) end
#initialized? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute.rb', line 103
def initialized? true end
#name (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 29
attr_reader :name, :value_before_type_cast, :type
#original_attribute (readonly, private) Also known as: #assigned?
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 152
attr_reader :original_attribute
#type (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 29
attr_reader :name, :value_before_type_cast, :type
#value_before_type_cast (readonly)
[ GitHub ]Instance Method Details
#==(other) Also known as: #eql?
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 115
def ==(other) self.class == other.class && name == other.name && value_before_type_cast == other.value_before_type_cast && type == other.type end
#_original_value_for_database (private)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 169
def _original_value_for_database type.serialize(original_value) end
#_value_for_database (private)
[ GitHub ]#encode_with(coder)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 135
def encode_with(coder) coder["name"] = name coder["value_before_type_cast"] = value_before_type_cast unless value_before_type_cast.nil? coder["type"] = type if type coder["original_attribute"] = original_attribute if original_attribute coder["value"] = value if defined?(@value) end
#eql?(other)
Alias for #==.
# File 'activemodel/lib/active_model/attribute.rb', line 121
alias eql? ==
#forgetting_assignment
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 74
def forgetting_assignment with_value_from_database(value_for_database) end
#hash
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 123
def hash [self.class, name, value_before_type_cast, type].hash end
#init_with(coder)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 127
def init_with(coder) @name = coder["name"] @value_before_type_cast = coder["value_before_type_cast"] @type = coder["type"] @original_attribute = coder["original_attribute"] @value = coder["value"] if coder.map.key?("value") end
#initialize_dup(other) (private)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 155
def initialize_dup(other) if @value&.duplicable? @value = @value.dup end end
#original_value
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 47
def original_value if assigned? original_attribute.original_value else type_cast(value_before_type_cast) end end
#original_value_for_database
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 143
def original_value_for_database if assigned? original_attribute.original_value_for_database else _original_value_for_database end end
#serializable?(&block) ⇒ Boolean
#type_cast
# File 'activemodel/lib/active_model/attribute.rb', line 99
def type_cast(*) raise NotImplementedError end
#value
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 41
def value # `defined?` is cheaper than `||=` when we get back falsy values @value = type_cast(value_before_type_cast) unless defined?(@value) @value end
#value_for_database
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 55
def value_for_database if !defined?(@value_for_database) || type.changed_in_place?(@value_for_database, value) @value_for_database = _value_for_database end @value_for_database end
#with_cast_value(value)
[ GitHub ]#with_type(type)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 91
def with_type(type) if changed_in_place? with_value_from_user(value).with_type(type) else self.class.new(name, value_before_type_cast, type, original_attribute) end end
#with_user_default(value)
[ GitHub ]# File 'activemodel/lib/active_model/attribute/user_provided_default.rb', line 7
def with_user_default(value) UserProvidedDefault.new(name, value, type, self.is_a?(FromDatabase) ? self : original_attribute) end
#with_value_from_database(value)
[ GitHub ]# File 'activemodel/lib/active_model/attribute.rb', line 83
def with_value_from_database(value) self.class.from_database(name, value, type) end