class Lhm::ColumnWithSql
Abstracts the details of a table column definition when specified with a MySQL column definition string
Attributes
Public Class Methods
Returns the column's class to be used
@return [Constant]
# File lib/lhm/column_with_sql.rb, line 13 def self.column_factory ::ActiveRecord::ConnectionAdapters::PerconaMigratorAdapter::Column end
Constructor
@param name [String, Symbol] @param definition [String]
# File lib/lhm/column_with_sql.rb, line 21 def initialize(name, definition) @name = name @definition = definition end
Public Instance Methods
Returns the column data as an Array to be used with the splat operator. See Lhm::Adaper#add_column
@return [Array]
# File lib/lhm/column_with_sql.rb, line 30 def attributes [type, column_options] end
Private Instance Methods
Returns the column instance with the provided data
@return [column_factory]
# File lib/lhm/column_with_sql.rb, line 55 def column cast_type = ActiveRecord::Base.connection.lookup_cast_type(definition) @column ||= self.class.column_factory.new( name, default_value, cast_type, definition, null_value ) end
TODO: investigate
Rails doesn't take into account lenght argument of INT in the definition, as an integer it will default it to 4 not an integer
Returns the columns data as a Hash
@return [Hash]
# File lib/lhm/column_with_sql.rb, line 48 def column_options { limit: column.limit, default: column.default, null: column.null } end
Gets the DEFAULT value the column takes as specified in the definition, if any
@return [String, NilClass]
# File lib/lhm/column_with_sql.rb, line 70 def default_value match = if definition =~ /timestamp|datetime/i /default '?(.+[^'])'?/i.match(definition) else /default '?(\w+)'?/i.match(definition) end return unless match match[1].downcase != 'null' ? match[1] : nil end
Checks whether the column accepts NULL as specified in the definition
@return [Boolean]
# File lib/lhm/column_with_sql.rb, line 85 def null_value match = /((\w*) NULL)/i.match(definition) return true unless match match[2].downcase == 'not' ? false : true end