Class: DeclareSchema::Model::TableOptionsDefinition

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/declare_schema/model/table_options_definition.rb

Constant Summary collapse

TABLE_OPTIONS_TO_SQL_MAPPINGS =
{
  charset:   'CHARACTER SET',
  collation: 'COLLATE'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, **table_options) ⇒ TableOptionsDefinition

Returns a new instance of TableOptionsDefinition.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/declare_schema/model/table_options_definition.rb', line 51

def initialize(table_name, **table_options)
  @table_name    = table_name
  @table_options = table_options.each_with_object({}) do |(k, v),result|
    result[k] =
      case k
      when :charset
        DeclareSchema.normalize_charset(v)
      when :collation
        DeclareSchema.normalize_collation(v)
      else
        v
      end
  end
end

Instance Attribute Details

#table_nameObject (readonly)

Returns the value of attribute table_name.



49
50
51
# File 'lib/declare_schema/model/table_options_definition.rb', line 49

def table_name
  @table_name
end

#table_optionsObject (readonly)

Returns the value of attribute table_options.



49
50
51
# File 'lib/declare_schema/model/table_options_definition.rb', line 49

def table_options
  @table_options
end

Class Method Details

.for_model(model, old_table_name = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/declare_schema/model/table_options_definition.rb', line 14

def for_model(model, old_table_name = nil)
  table_name    = old_table_name || model.table_name
  table_options = if model.connection.class.name.match?(/mysql/i)
                    mysql_table_options(model.connection, table_name)
                  else
                    {}
                  end

  new(table_name, **table_options)
end

Instance Method Details

#<=>(rhs) ⇒ Object



81
82
83
# File 'lib/declare_schema/model/table_options_definition.rb', line 81

def <=>(rhs)
  to_key <=> rhs.to_key
end

#alter_table_statementObject



92
93
94
95
# File 'lib/declare_schema/model/table_options_definition.rb', line 92

def alter_table_statement
  statement = "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} #{to_s}"
  "execute #{statement.inspect}"
end

#equivalent?(rhs) ⇒ Boolean

Returns:



85
86
87
# File 'lib/declare_schema/model/table_options_definition.rb', line 85

def equivalent?(rhs)
  settings == rhs.settings
end

#hashObject



77
78
79
# File 'lib/declare_schema/model/table_options_definition.rb', line 77

def hash
  to_key.hash
end

#settingsString Also known as: to_s

Flatten out/join table options as a space-separated string

Returns:

  • (String)

    Table options as a string



73
74
75
# File 'lib/declare_schema/model/table_options_definition.rb', line 73

def settings
  @settings ||= table_options.map { |name, value| "#{TABLE_OPTIONS_TO_SQL_MAPPINGS[name]} #{value}" if value }.compact.join(" ")
end

#to_keyObject



66
67
68
# File 'lib/declare_schema/model/table_options_definition.rb', line 66

def to_key
  @key ||= [table_name, table_options].map(&:to_s)
end