Class: Napa::CLI::Migration

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/napa/cli/migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#join_tablesObject (readonly)

Returns the value of attribute join_tables.



13
14
15
# File 'lib/napa/cli/migration.rb', line 13

def join_tables
  @join_tables
end

#migration_actionObject (readonly)

Returns the value of attribute migration_action.



13
14
15
# File 'lib/napa/cli/migration.rb', line 13

def migration_action
  @migration_action
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



13
14
15
# File 'lib/napa/cli/migration.rb', line 13

def table_name
  @table_name
end

Instance Method Details

#migrationObject



53
54
55
56
57
58
# File 'lib/napa/cli/migration.rb', line 53

def migration
  self.class.source_root "#{File.dirname(__FILE__)}/templates/#{@migration_template}"
  say 'Generating migration...'
  directory '.', output_directory
  say 'Done!', :green
end

#migration_filenameObject



19
20
21
# File 'lib/napa/cli/migration.rb', line 19

def migration_filename
  "#{version}_#{migration_name.underscore}"
end

#output_directoryObject



23
24
25
# File 'lib/napa/cli/migration.rb', line 23

def output_directory
  './db/migrate'
end

#parse_attributes!Object



27
28
29
30
31
# File 'lib/napa/cli/migration.rb', line 27

def parse_attributes!
  self.attributes = (attributes || []).map do |attr|
    GeneratedAttribute.parse(attr)
  end
end

#set_local_assigns!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/napa/cli/migration.rb', line 33

def set_local_assigns!
  @migration_template = "migration"
  filename = migration_name.underscore
  case filename
  when /^(add|remove)_.*_(?:to|from)_(.*)/
    @migration_action = $1
    @table_name       = $2.pluralize
  when /join_table/
    if attributes.length == 2
      @migration_action = 'join'
      @join_tables      = attributes.map(&:plural_name)

      set_index_names
    end
  when /^create_(.+)/
    @table_name = $1.pluralize
    @migration_template = "create_table_migration"
  end
end

#versionObject



15
16
17
# File 'lib/napa/cli/migration.rb', line 15

def version
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end