Class: Ardb::Migration

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

Constant Summary collapse

NoIdentifierError =
Class.new(ArgumentError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ardb_config, identifier) ⇒ Migration

Returns a new instance of Migration.

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ardb/migration.rb', line 12

def initialize(ardb_config, identifier)
  raise NoIdentifierError if identifier.to_s.empty?

  @migrations_path = ardb_config.migrations_path
  @identifier      = identifier

  @class_name = @identifier.classify.pluralize
  @file_name  = get_file_name(@identifier)
  @file_path  = File.join(migrations_path, "#{@file_name}.rb")

  migration_version = ActiveRecord::Migration.current_version
  @source =
    "class #{@class_name} "\
    "< ActiveRecord::Migration[#{migration_version}]\n"\
    "  def change\n"\
    "  end\n"\
    "end\n"
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



10
11
12
# File 'lib/ardb/migration.rb', line 10

def class_name
  @class_name
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



10
11
12
# File 'lib/ardb/migration.rb', line 10

def file_name
  @file_name
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



10
11
12
# File 'lib/ardb/migration.rb', line 10

def file_path
  @file_path
end

#identifierObject (readonly)

Returns the value of attribute identifier.



9
10
11
# File 'lib/ardb/migration.rb', line 9

def identifier
  @identifier
end

#migrations_pathObject (readonly)

Returns the value of attribute migrations_path.



9
10
11
# File 'lib/ardb/migration.rb', line 9

def migrations_path
  @migrations_path
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/ardb/migration.rb', line 10

def source
  @source
end

Instance Method Details

#save!Object



31
32
33
34
35
# File 'lib/ardb/migration.rb', line 31

def save!
  FileUtils.mkdir_p migrations_path
  File.open(file_path, "w"){ |f| f.write(source) }
  self
end