Class: Ardb::Runner::GenerateCommand::MigrationCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ardb/runner/generate_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ MigrationCommand

Returns a new instance of MigrationCommand.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ardb/runner/generate_command.rb', line 38

def initialize(identifier)
  if identifier.nil?
    raise Ardb::Runner::CmdError, "specify a name for the migration"
  end

  @identifier = identifier
  @class_name = @identifier.classify.pluralize
  @file_name  = begin
    "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{@identifier.underscore}"
  end
  @template = "require 'ardb/migration_helpers'\n\n"\
              "class #{@class_name} < ActiveRecord::Migration\n"\
              "  include Ardb::MigrationHelpers\n\n"\
              "  def change\n"\
              "  end\n\n"\
              "end\n"
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



36
37
38
# File 'lib/ardb/runner/generate_command.rb', line 36

def class_name
  @class_name
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



36
37
38
# File 'lib/ardb/runner/generate_command.rb', line 36

def file_name
  @file_name
end

#identifierObject (readonly)

Returns the value of attribute identifier.



36
37
38
# File 'lib/ardb/runner/generate_command.rb', line 36

def identifier
  @identifier
end

#templateObject (readonly)

Returns the value of attribute template.



36
37
38
# File 'lib/ardb/runner/generate_command.rb', line 36

def template
  @template
end

Instance Method Details

#runObject



56
57
58
59
60
61
# File 'lib/ardb/runner/generate_command.rb', line 56

def run
  FileUtils.mkdir_p Ardb.config.migrations_path
  file_path = File.join(Ardb.config.migrations_path, "#{@file_name}.rb")
  File.open(file_path, "w"){ |f| f.write(@template) }
  $stdout.puts file_path
end