Class: CouchbaseOrm::IndexMigrationGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase-orm/index_migration_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(path: CouchbaseOrm.config.index.migrations_path, now: Time.now) ⇒ IndexMigrationGenerator

Returns a new instance of IndexMigrationGenerator.



8
9
10
11
# File 'lib/couchbase-orm/index_migration_generator.rb', line 8

def initialize(path: CouchbaseOrm.config.index.migrations_path, now: Time.now)
  @path = path
  @now = now
end

Instance Method Details

#generate(name) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/couchbase-orm/index_migration_generator.rb', line 13

def generate(name)
  raise ArgumentError.new('Migration name is required') if name.to_s.strip.empty?

  timestamp = @now.utc.strftime('%Y%m%d%H%M%S')
  underscored = name.to_s.underscore
  class_name = name.to_s.camelize
  file_path = File.join(@path, "#{timestamp}_#{underscored}.rb")

  FileUtils.mkdir_p(@path)
  File.write(file_path, migration_template(class_name))
  file_path
end