Class: Shaf::Generator::Migration::AddColumn

Inherits:
Base
  • Object
show all
Defined in:
lib/shaf/generator/migration/add_column.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#add_change, #call, #column_def, identified_by, identifier, inherited, #initialize, #table_name, #target, usage

Constructor Details

This class inherits a constructor from Shaf::Generator::Migration::Base

Instance Method Details

#add_columns_changeObject



32
33
34
35
36
37
38
39
# File 'lib/shaf/generator/migration/add_column.rb', line 32

def add_columns_change
  cols = columns.map { |s| column_def(s, create: false) }
  [
    "alter_table(:#{table_name}) do",
    *cols.map { |col| col.prepend("  ") }, # indent body with 2 spaces
    "end\n"
  ]
end

#columnsObject



28
29
30
# File 'lib/shaf/generator/migration/add_column.rb', line 28

def columns
  args[1..-1]
end

#compile_changesObject



24
25
26
# File 'lib/shaf/generator/migration/add_column.rb', line 24

def compile_changes
  add_change add_columns_change
end

#compile_migration_nameObject



19
20
21
22
# File 'lib/shaf/generator/migration/add_column.rb', line 19

def compile_migration_name
  cols = columns.map { |c| c.split(':').first }
  "add_#{cols.join('_')}_to_#{table_name}"
end

#validate_argsObject



9
10
11
12
13
14
15
16
17
# File 'lib/shaf/generator/migration/add_column.rb', line 9

def validate_args
  if (table_name || "").empty?
    raise "Please provide a table and at least " \
      "one column when generation add column migration"
  elsif args.size < 2 || (args[1] || "").empty?
    raise "Please provide at least one column when " \
      "generation add column migration"
  end
end