Class: Adjective::CreateTableMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/templates/create_table_migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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_classesObject (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

#modulesObject (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_templateObject



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_symObject



13
14
15
# File 'lib/templates/create_table_migration.rb', line 13

def class_name_sym
  ":#{@class_name.underscore.downcase}"
end

#columnsObject



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_nameObject



22
23
24
25
26
# File 'lib/templates/create_table_migration.rb', line 22

def file_name
  timestamp = Time.now.strftime('%Y%m%d%H%M%S')
  migration_name = migration_class.underscore
  return "#{timestamp}_#{migration_name}.rb"
end

#given_modulesObject



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_classObject



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

#renderObject



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