aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/attribute.rb')
-rw-r--r--activerecord/lib/active_record/attribute.rb51
1 files changed, 42 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb
index 8604ccb90d..13c8bc3676 100644
--- a/activerecord/lib/active_record/attribute.rb
+++ b/activerecord/lib/active_record/attribute.rb
@@ -8,6 +8,10 @@ module ActiveRecord
def from_user(value, type)
FromUser.new(value, type)
end
+
+ def uninitialized(type)
+ Uninitialized.new(type)
+ end
end
attr_reader :value_before_type_cast, :type
@@ -37,10 +41,22 @@ module ActiveRecord
type.changed_in_place?(old_value, value)
end
+ def with_value_from_user(value)
+ self.class.from_user(value, type)
+ end
+
+ def with_value_from_database(value)
+ self.class.from_database(value, type)
+ end
+
def type_cast
raise NotImplementedError
end
+ def initialized?
+ true
+ end
+
protected
def initialize_dup(other)
@@ -49,27 +65,44 @@ module ActiveRecord
end
end
- class FromDatabase < Attribute
+ class FromDatabase < Attribute # :nodoc:
def type_cast(value)
type.type_cast_from_database(value)
end
end
- class FromUser < Attribute
+ class FromUser < Attribute # :nodoc:
def type_cast(value)
type.type_cast_from_user(value)
end
end
- class Null
- class << self
- attr_reader :value, :value_before_type_cast, :value_for_database
+ class NullAttribute < Attribute # :nodoc:
+ def initialize
+ super(nil, Type::Value.new)
+ end
- def changed_from?(*)
- false
- end
- alias changed_in_place_from? changed_from?
+ def value
+ nil
end
end
+
+ class Uninitialized < Attribute # :nodoc:
+ def initialize(type)
+ super(nil, type)
+ end
+
+ def value
+ nil
+ end
+ alias value_for_database value
+
+ def initialized?
+ false
+ end
+ end
+ private_constant :FromDatabase, :FromUser, :NullAttribute, :Uninitialized
+
+ Null = NullAttribute.new # :nodoc:
end
end