Class: CouchbaseOrm::IndexMigration::MigrationGenerator

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

Constant Summary collapse

DEFAULT_NAME =
'InitialIndexes'

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MigrationGenerator.



11
12
13
14
# File 'lib/couchbase-orm/index_migration/migration_generator.rb', line 11

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

Instance Method Details

#generate(index_definitions, name: DEFAULT_NAME) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/couchbase-orm/index_migration/migration_generator.rb', line 16

def generate(index_definitions, name: DEFAULT_NAME)
  class_name = migration_class_name(name)
  timestamp = @now.utc.strftime('%Y%m%d%H%M%S')
  file_name = "#{timestamp}_#{class_name.underscore}.rb"
  file_path = File.join(@path, file_name)

  FileUtils.mkdir_p(@path)
  File.write(file_path, source_for(index_definitions, class_name: class_name))
  file_path
end

#source_for(index_definitions, class_name: DEFAULT_NAME) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/couchbase-orm/index_migration/migration_generator.rb', line 27

def source_for(index_definitions, class_name: DEFAULT_NAME)
  definitions = Array(index_definitions).sort_by { |d| d.name.to_s }

  <<~RUBY
    class #{migration_class_name(class_name)} < CouchbaseOrm::IndexMigration
      def up
    #{up_body(definitions)}
      end

      def down
    #{down_body(definitions)}
      end
    end
  RUBY
end