1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract MySQL::TypeMetadata class to connection_adapters/mysql/type_metadata.rb

This commit is contained in:
Ryuta Kamizono 2016-01-11 05:03:31 +09:00
parent 83026cb15d
commit 560bf1c939
3 changed files with 35 additions and 29 deletions

View file

@ -3,6 +3,7 @@ require 'active_record/connection_adapters/mysql/column'
require 'active_record/connection_adapters/mysql/schema_creation'
require 'active_record/connection_adapters/mysql/schema_definitions'
require 'active_record/connection_adapters/mysql/schema_dumper'
require 'active_record/connection_adapters/mysql/type_metadata'
require 'active_support/core_ext/string/strip'
@ -20,33 +21,6 @@ module ActiveRecord
MySQL::SchemaCreation.new(self)
end
class MysqlTypeMetadata < DelegateClass(SqlTypeMetadata) # :nodoc:
attr_reader :extra, :strict
def initialize(type_metadata, extra: "", strict: false)
super(type_metadata)
@type_metadata = type_metadata
@extra = extra
@strict = strict
end
def ==(other)
other.is_a?(MysqlTypeMetadata) &&
attributes_for_hash == other.attributes_for_hash
end
alias eql? ==
def hash
attributes_for_hash.hash
end
protected
def attributes_for_hash
[self.class, @type_metadata, extra, strict]
end
end
##
# :singleton-method:
# By default, the Mysql2Adapter will consider all columns of type <tt>tinyint(1)</tt>
@ -804,7 +778,7 @@ module ActiveRecord
end
def fetch_type_metadata(sql_type, extra = "")
MysqlTypeMetadata.new(super(sql_type), extra: extra, strict: strict_mode?)
MySQL::TypeMetadata.new(super(sql_type), extra: extra, strict: strict_mode?)
end
def add_index_length(option_strings, column_names, options = {})

View file

@ -0,0 +1,32 @@
module ActiveRecord
module ConnectionAdapters
module MySQL
class TypeMetadata < DelegateClass(SqlTypeMetadata) # :nodoc:
attr_reader :extra, :strict
def initialize(type_metadata, extra: "", strict: false)
super(type_metadata)
@type_metadata = type_metadata
@extra = extra
@strict = strict
end
def ==(other)
other.is_a?(MySQL::TypeMetadata) &&
attributes_for_hash == other.attributes_for_hash
end
alias eql? ==
def hash
attributes_for_hash.hash
end
protected
def attributes_for_hash
[self.class, @type_metadata, extra, strict]
end
end
end
end
end

View file

@ -64,7 +64,7 @@ module ActiveRecord
MySQL::Column.new("title", "a", SqlTypeMetadata.new(sql_type: "blob"))
end
text_type = AbstractMysqlAdapter::MysqlTypeMetadata.new(
text_type = MySQL::TypeMetadata.new(
SqlTypeMetadata.new(type: :text))
assert_raise ArgumentError do
MySQL::Column.new("title", "Hello", text_type)