Permalink
Browse files

refactor, introduce `Type#type_cast_for_schema` to cast for schema.rb

This removes the case statement in `SchemaDumper` and gives every `Type`
the possibility to control the SchemaDumper default value output.

/cc @sgrif
  • Loading branch information...
1 parent b9eeb03 commit 4321cd09a5b81fee278e39564d4729784732611c @senny senny committed May 30, 2014
@@ -25,39 +25,14 @@ def prepare_column_options(column, types)
spec[:precision] = column.precision.inspect if column.precision
spec[:scale] = column.scale.inspect if column.scale
spec[:null] = 'false' unless column.null
- spec[:default] = default_string(column.default) if column.has_default?
+ spec[:default] = column.type_cast_for_schema(column.default) if column.has_default?
spec
end
# Lists the valid migration options
def migration_keys
[:name, :limit, :precision, :scale, :default, :null]
end
-
- private
-
- def default_string(value)
- case value
- when BigDecimal
- value.to_s
- when Date, DateTime, Time
- "'#{value.to_s(:db)}'"
- when Range
- # infinity dumps as Infinity, which causes uninitialized constant error
- value.inspect.gsub('Infinity', '::Float::INFINITY')
- when IPAddr
- subnet_mask = value.instance_variable_get(:@mask_addr)
-
- # If the subnet mask is equal to /32, don't output it
- if subnet_mask == (2**32 - 1)
- "\"#{value.to_s}\""
- else
- "\"#{value.to_s}/#{subnet_mask.to_s(2).count('1')}\""
- end
- else
- value.inspect
- end
- end
end
end
end
@@ -18,6 +18,7 @@ module Format
delegate :type, :precision, :scale, :limit, :klass, :accessor,
:text?, :number?, :binary?, :serialized?,
:type_cast, :type_cast_for_write, :raw_type_cast_for_write, :type_cast_for_database,
+ :type_cast_for_schema,
to: :cast_type
# Instantiates a new column in the table.
@@ -7,6 +7,17 @@ def type
:cidr
end
+ def type_cast_for_schema(value)
+ subnet_mask = value.instance_variable_get(:@mask_addr)
+
+ # If the subnet mask is equal to /32, don't output it
+ if subnet_mask == (2**32 - 1)
+ "\"#{value.to_s}\""
+ else
+ "\"#{value.to_s}/#{subnet_mask.to_s(2).count('1')}\""
+ end
+ end
+
def cast_value(value)
ConnectionAdapters::PostgreSQLColumn.string_to_cidr value
end
@@ -24,6 +24,10 @@ def infinity?(value)
value.respond_to?(:infinite?) && value.infinite?
end
+ def type_cast_for_schema(value)
+ value.inspect.gsub('Infinity', '::Float::INFINITY')
+ end
+
def type_cast_single(value)
infinity?(value) ? value : @subtype.type_cast(value)
end
@@ -9,6 +9,10 @@ def klass
::Date
end
+ def type_cast_for_schema(value)
+ "'#{value.to_s(:db)}'"
+ end
+
private
def cast_value(value)
@@ -11,6 +11,10 @@ def klass
::BigDecimal
end
+ def type_cast_for_schema(value)
+ value.to_s
+ end
+
private
def cast_value(value)
@@ -5,6 +5,10 @@ def klass
::Time
end
+ def type_cast_for_schema(value)
+ "'#{value.to_s(:db)}'"
+ end
+
private
def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
@@ -27,6 +27,10 @@ def type_cast_for_database(value)
type_cast_for_write(value)
end
+ def type_cast_for_schema(value)
@sgrif

sgrif May 30, 2014

Member

Let's # :nodoc: this, it doesn't seem a method that we want to document. The interface is starting to get a little scary big on these objects.

@senny

senny May 30, 2014

Member

It's something that you'd want to implement when you have your own types...

+ value.inspect
+ end
+
def text?
false
end

0 comments on commit 4321cd0

Please sign in to comment.