aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/value.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-27 10:59:22 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-27 19:44:29 -0700
commit728fa69839d2c3b839391f9077896c06df80bddf (patch)
treedab29e5d69868c8abf9a00491ca8cc0acd5d04c3 /activerecord/lib/active_record/type/value.rb
parent7f73b9152cfc3f218cfc862e971ba56b94f6be10 (diff)
downloadrails-728fa69839d2c3b839391f9077896c06df80bddf.tar.gz
rails-728fa69839d2c3b839391f9077896c06df80bddf.tar.bz2
rails-728fa69839d2c3b839391f9077896c06df80bddf.zip
Move types to the top level `ActiveRecord` namespace
`ActiveRecord::ConnectionAdapters::Type::Value` => `ActiveRecord::Type::Value`
Diffstat (limited to 'activerecord/lib/active_record/type/value.rb')
-rw-r--r--activerecord/lib/active_record/type/value.rb59
1 files changed, 59 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb
new file mode 100644
index 0000000000..72d27197d5
--- /dev/null
+++ b/activerecord/lib/active_record/type/value.rb
@@ -0,0 +1,59 @@
+module ActiveRecord
+ module Type
+ class Value # :nodoc:
+ attr_reader :precision, :scale, :limit
+
+ # Valid options are +precision+, +scale+, and +limit+.
+ # They are only used when dumping schema.
+ def initialize(options = {})
+ options.assert_valid_keys(:precision, :scale, :limit)
+ @precision = options[:precision]
+ @scale = options[:scale]
+ @limit = options[:limit]
+ end
+
+ # The simplified that this object represents. Subclasses
+ # should override this method.
+ def type; end
+
+ # Takes an input from the database, or from attribute setters,
+ # and casts it to a type appropriate for this object. This method
+ # should not be overriden by subclasses. Instead, override `cast_value`.
+ def type_cast(value)
+ cast_value(value) unless value.nil?
+ end
+
+ def type_cast_for_write(value)
+ value
+ end
+
+ def type_cast_for_database(value)
+ type_cast_for_write(value)
+ end
+
+ def text?
+ false
+ end
+
+ def number?
+ false
+ end
+
+ def binary?
+ false
+ end
+
+ def klass
+ ::Object
+ end
+
+ private
+
+ # Responsible for casting values from external sources to the appropriate
+ # type. Called by `type_cast` for all values except `nil`.
+ def cast_value(value) # :api: public
+ value
+ end
+ end
+ end
+end