Class: Ridgepole::DSLParser::TableDefinition
- Inherits:
-
Object
- Object
- Ridgepole::DSLParser::TableDefinition
- Defined in:
- lib/ridgepole/dsl_parser/table_definition.rb
Constant Summary collapse
- DEFAULT_PRIMARY_KEY_TYPE =
:bigint
- TYPES =
{ # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L274 string: {}, text: {}, integer: {}, bigint: {}, float: {}, decimal: {}, datetime: {}, timestamp: {}, time: {}, date: {}, binary: {}, boolean: {}, # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L79 serial: { null: false }, bigserial: { null: false }, daterange: {}, numrange: {}, tsrange: {}, tstzrange: {}, int4range: {}, int8range: {}, # binary: {}, # dup key # boolean: {}, # dup key # bigint: {}, # dup key xml: {}, tsvector: {}, hstore: {}, inet: {}, cidr: {}, macaddr: {}, uuid: {}, json: {}, jsonb: {}, ltree: {}, citext: {}, point: {}, bit: {}, bit_varying: {}, money: {}, # https://github.com/rails/rails/blob/v5.1.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L184 virtual: {}, # https://github.com/rails/rails/blob/v5.0.4/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L53 # json: {}, # dup key }.freeze
- ALIAS_TYPES =
{ # https://github.com/rails/rails/blob/v5.0.0.rc1/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb tinyblob: [:blob, { limit: 255 }], mediumblob: [:binary, { limit: 16_777_215 }], longblob: [:binary, { limit: 4_294_967_295 }], tinytext: [:text, { limit: 255 }], mediumtext: [:text, { limit: 16_777_215 }], longtext: [:text, { limit: 4_294_967_295 }], unsigned_integer: [:integer, { unsigned: true }], unsigned_bigint: [:bigint, { unsigned: true }], unsigned_float: [:float, { limit: 24, unsigned: true }], unsigned_decimal: [:decimal, { precision: 10, unsigned: true }], }.freeze
Instance Attribute Summary collapse
-
#__definition ⇒ Object
readonly
Returns the value of attribute __definition.
Instance Method Summary collapse
-
#blob(*args) ⇒ Object
XXX:.
- #column(name, type, options = {}) ⇒ Object
- #foreign_key(name, options = {}) ⇒ Object
- #index(name, options = {}) ⇒ Object
-
#initialize(table_name, base) ⇒ TableDefinition
constructor
A new instance of TableDefinition.
- #references(*args) ⇒ Object (also: #belongs_to)
- #timestamps(*args) ⇒ Object
Constructor Details
#initialize(table_name, base) ⇒ TableDefinition
Returns a new instance of TableDefinition.
8 9 10 11 12 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 8 def initialize(table_name, base) @__definition = {} @table_name = table_name @base = base end |
Instance Attribute Details
#__definition ⇒ Object (readonly)
Returns the value of attribute __definition.
6 7 8 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 6 def __definition @__definition end |
Instance Method Details
#blob(*args) ⇒ Object
XXX:
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 102 def blob(*args) = args. = { limit: 65_535 }.merge() column_names = args column_names.each do |name| column_type = (0..0xff).cover?([:limit]) ? :blob : :binary column(name, column_type, ) end end |
#column(name, type, options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 14 def column(name, type, = {}) name = name.to_s = .key?(:index) ? .delete(:index) : false @__definition[name] = { type: type, options: , } index(name, .is_a?(Hash) ? : {}) if end |
#foreign_key(name, options = {}) ⇒ Object
126 127 128 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 126 def foreign_key(name, = {}) @base.add_foreign_key(@table_name, name, ) end |
#index(name, options = {}) ⇒ Object
122 123 124 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 122 def index(name, = {}) @base.add_index(@table_name, name, ) end |
#references(*args) ⇒ Object Also known as: belongs_to
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 136 def references(*args) = args. polymorphic = .delete(:polymorphic) = polymorphic.is_a?(Hash) ? polymorphic : {} # https://github.com/rails/rails/blob/5-2-1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L167 .merge!(.slice(:null, :first, :after)) = .key?(:index) ? .delete(:index) : true type = .delete(:type) || DEFAULT_PRIMARY_KEY_TYPE = .delete(:foreign_key) args.each do |col| column("#{col}_id", type, ) column("#{col}_type", :string, ) if polymorphic if columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"] index(columns, .is_a?(Hash) ? : {}) end if # rubocop:disable Style/Next fk_opts = .is_a?(Hash) ? .dup : {} fk_opts.update(column: "#{col}_id") if col.to_s.singularize != col.to_s to_table = fk_opts.delete(:to_table) || col.to_s.pluralize @base.add_foreign_key(@table_name, to_table, fk_opts) end end end |
#timestamps(*args) ⇒ Object
130 131 132 133 134 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 130 def (*args) = { null: false }.merge(args.) column(:created_at, :datetime, ) column(:updated_at, :datetime, ) end |