Class: DeclareSchema::Model::TableOptionsDefinition
- Inherits:
-
Object
- Object
- DeclareSchema::Model::TableOptionsDefinition
- 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
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
-
#table_options ⇒ Object
readonly
Returns the value of attribute table_options.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
- #alter_table_statement ⇒ Object
- #equivalent?(rhs) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(table_name, **table_options) ⇒ TableOptionsDefinition
constructor
A new instance of TableOptionsDefinition.
-
#settings ⇒ String
(also: #to_s)
Flatten out/join table options as a space-separated string.
- #to_key ⇒ Object
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_name = table_name @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_name ⇒ Object (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_options ⇒ Object (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 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 = if model.connection.class.name.match?(/mysql/i) (model.connection, table_name) else {} end new(table_name, **) 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_statement ⇒ Object
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
85 86 87 |
# File 'lib/declare_schema/model/table_options_definition.rb', line 85 def equivalent?(rhs) settings == rhs.settings end |
#hash ⇒ Object
77 78 79 |
# File 'lib/declare_schema/model/table_options_definition.rb', line 77 def hash to_key.hash end |
#settings ⇒ String Also known as: to_s
Flatten out/join table options as a space-separated string
73 74 75 |
# File 'lib/declare_schema/model/table_options_definition.rb', line 73 def settings @settings ||= .map { |name, value| "#{TABLE_OPTIONS_TO_SQL_MAPPINGS[name]} #{value}" if value }.compact.join(" ") end |
#to_key ⇒ Object
66 67 68 |
# File 'lib/declare_schema/model/table_options_definition.rb', line 66 def to_key @key ||= [table_name, ].map(&:to_s) end |