Class: CouchbaseOrm::IndexMigration::MigrationGenerator
- Inherits:
-
Object
- Object
- CouchbaseOrm::IndexMigration::MigrationGenerator
- Defined in:
- lib/couchbase-orm/index_migration/migration_generator.rb
Constant Summary collapse
- DEFAULT_NAME =
'InitialIndexes'
Instance Method Summary collapse
- #generate(index_definitions, name: DEFAULT_NAME) ⇒ Object
-
#initialize(path: CouchbaseOrm.config.index.migrations_path, now: Time.now) ⇒ MigrationGenerator
constructor
A new instance of MigrationGenerator.
- #source_for(index_definitions, class_name: DEFAULT_NAME) ⇒ Object
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) = @now.utc.strftime('%Y%m%d%H%M%S') file_name = "#{}_#{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 |