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 =
Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.1') ? :bigint : :integer
- 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 :daterange, :numrange, :tsrange, :tstzrange, :int4range, :int8range, :binary, :boolean, :bigint, :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, ].uniq
- 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: 16777215}], longblob: [:binary, {limit: 4294967295}], tinytext: [:text, {limit: 255}], mediumtext: [:text, {limit: 16777215}], longtext: [:text, {limit: 4294967295}], unsigned_integer: [:integer, {unsigned: true}], unsigned_bigint: [:bigint, {unsigned: true}], unsigned_float: [:float, {limit: 24, unsigned: true}], unsigned_decimal: [:decimal, {precision: 10, unsigned: true}], }
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
- #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.
5 6 7 8 9 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 5 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.
3 4 5 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 3 def __definition @__definition end |
Instance Method Details
#blob(*args) ⇒ Object
XXX:
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 93 def blob(*args) = args. = {limit: 65535}.merge() column_names = args column_names.each do |name| column_type = (0..0xff).include?([:limit]) ? :blob : :binary column(name, column_type, ) end end |
#column(name, type, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 11 def column(name, type, = {}) name = name.to_s @__definition[name] = { :type => type, :options => , } end |
#index(name, options = {}) ⇒ Object
113 114 115 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 113 def index(name, = {}) @base.add_index(@table_name, name, ) end |
#references(*args) ⇒ Object Also known as: belongs_to
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 123 def references(*args) = args. polymorphic = .delete(:polymorphic) = .has_key?(:index) ? .delete(:index) : true type = .delete(:type) || DEFAULT_PRIMARY_KEY_TYPE args.each do |col| column("#{col}_id", type, ) column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : ) if polymorphic if columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"] index(columns, .is_a?(Hash) ? : {}) end end end |
#timestamps(*args) ⇒ Object
117 118 119 120 121 |
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 117 def (*args) = {:null => false}.merge(args.) column(:created_at, :datetime, ) column(:updated_at, :datetime, ) end |