提交 4321cd09 编写于 作者: Y Yves Senn

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
上级 b9eeb033
......@@ -25,7 +25,7 @@ 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
......@@ -33,31 +33,6 @@ def prepare_column_options(column, types)
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)
value.inspect
end
def text?
false
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册