Class: Adjective::CreateTableMigration
- Inherits:
-
Object
- Object
- Adjective::CreateTableMigration
- Defined in:
- lib/templates/create_table_migration.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#module_classes ⇒ Object
readonly
Returns the value of attribute module_classes.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
Instance Method Summary collapse
- #base_template ⇒ Object
- #class_name_sym ⇒ Object
- #columns ⇒ Object
- #file_name ⇒ Object
- #given_modules ⇒ Object
-
#initialize(class_name, modules = []) ⇒ CreateTableMigration
constructor
A new instance of CreateTableMigration.
- #migration_class ⇒ Object
- #render ⇒ Object
Constructor Details
#initialize(class_name, modules = []) ⇒ CreateTableMigration
Returns a new instance of CreateTableMigration.
5 6 7 8 9 10 11 |
# File 'lib/templates/create_table_migration.rb', line 5 def initialize(class_name, modules = []) # raise ArgumentError, "Provided class_name of: #{class_name} was not a valid String." if (!!class_name || !class_name.is_a?(String)) @class_name = class_name @modules = modules.map{|m| m&.downcase} @version = "7.0" @module_classes = given_modules end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
3 4 5 |
# File 'lib/templates/create_table_migration.rb', line 3 def class_name @class_name end |
#module_classes ⇒ Object (readonly)
Returns the value of attribute module_classes.
3 4 5 |
# File 'lib/templates/create_table_migration.rb', line 3 def module_classes @module_classes end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
3 4 5 |
# File 'lib/templates/create_table_migration.rb', line 3 def modules @modules end |
Instance Method Details
#base_template ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/templates/create_table_migration.rb', line 40 def base_template <<~TEMPLATE class {{migration_class}} < ActiveRecord::Migration[{{version}}] def change create_table {{class_name}} do |t| {{create_columns}} end end end TEMPLATE end |
#class_name_sym ⇒ Object
13 14 15 |
# File 'lib/templates/create_table_migration.rb', line 13 def class_name_sym ":#{@class_name.underscore.downcase}" end |
#columns ⇒ Object
34 35 36 37 38 |
# File 'lib/templates/create_table_migration.rb', line 34 def columns @module_classes.map do |mod| mod.adjective_columns end.join("\n").chomp end |
#file_name ⇒ Object
22 23 24 25 26 |
# File 'lib/templates/create_table_migration.rb', line 22 def file_name = Time.now.strftime('%Y%m%d%H%M%S') migration_name = migration_class.underscore return "#{}_#{migration_name}.rb" end |
#given_modules ⇒ Object
28 29 30 31 32 |
# File 'lib/templates/create_table_migration.rb', line 28 def given_modules @modules.map do |mod| klass = "Adjective::#{mod.capitalize}".constantize end end |
#migration_class ⇒ Object
17 18 19 20 |
# File 'lib/templates/create_table_migration.rb', line 17 def migration_class modules_string = @modules.map {|m| m.capitalize}.join("And") "Create#{@class_name.camelize}With#{modules_string}" end |
#render ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/templates/create_table_migration.rb', line 52 def render base = base_template base .gsub("{{migration_class}}", migration_class.lstrip) .gsub("{{class_name}}", class_name_sym.lstrip) .gsub("{{create_columns}}", columns.lstrip) .gsub("{{version}}", @version) end |