Class: SQL::TableCreator
- Inherits:
-
Object
- Object
- SQL::TableCreator
- Extended by:
- DataMapper::Property::Lookup
- Defined in:
- lib/dm-migrations/sql/table_creator.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #column(name, type, opts = {}) ⇒ Object
-
#initialize(adapter, table_name, opts = {}, &block) ⇒ TableCreator
constructor
A new instance of TableCreator.
-
#now ⇒ Object
A helper for using the native NOW() SQL function in a default.
- #quoted_table_name ⇒ Object
- #to_sql ⇒ Object
-
#uuid ⇒ Object
A helper for using the native UUID() SQL function in a default.
Constructor Details
#initialize(adapter, table_name, opts = {}, &block) ⇒ TableCreator
Returns a new instance of TableCreator.
10 11 12 13 14 15 16 17 18 |
# File 'lib/dm-migrations/sql/table_creator.rb', line 10 def initialize(adapter, table_name, opts = {}, &block) @adapter = adapter @table_name = table_name.to_s @opts = opts @columns = [] self.instance_eval &block end |
Instance Attribute Details
#opts ⇒ Object
Returns the value of attribute opts.
8 9 10 |
# File 'lib/dm-migrations/sql/table_creator.rb', line 8 def opts @opts end |
#table_name ⇒ Object
Returns the value of attribute table_name.
8 9 10 |
# File 'lib/dm-migrations/sql/table_creator.rb', line 8 def table_name @table_name end |
Instance Method Details
#column(name, type, opts = {}) ⇒ Object
24 25 26 |
# File 'lib/dm-migrations/sql/table_creator.rb', line 24 def column(name, type, opts = {}) @columns << Column.new(@adapter, name, type, opts) end |
#now ⇒ Object
A helper for using the native NOW() SQL function in a default
33 34 35 |
# File 'lib/dm-migrations/sql/table_creator.rb', line 33 def now SqlExpr.new('NOW()') end |
#quoted_table_name ⇒ Object
20 21 22 |
# File 'lib/dm-migrations/sql/table_creator.rb', line 20 def quoted_table_name @adapter.send(:quote_name, table_name) end |
#to_sql ⇒ Object
28 29 30 |
# File 'lib/dm-migrations/sql/table_creator.rb', line 28 def to_sql "CREATE TABLE #{quoted_table_name} (#{@columns.map{ |c| c.to_sql }.join(', ')})#{@adapter.(@opts)}" end |