class Cassie::Schema::CassandraMigrations::MigrationFile

Attributes

description[R]
filename[R]
source[R]

Public Class Methods

new(filename) click to toggle source
# File lib/cassie/schema/cassandra_migrations/migration_file.rb, line 7
def initialize(filename)
  @filename = filename
  @description = parse_description
end

Public Instance Methods

build_migration_class(version) click to toggle source

Builds a Cassie::Schema::Migration from the CassandraMigrations migration file

@return [String] source string of new migration class

# File lib/cassie/schema/cassandra_migrations/migration_file.rb, line 16
def build_migration_class(version)
  @source = load_source
  redefine_class(version.migration_class_name)
  define_new_migration_class
  version.migration_class_name.constantize
  @source
ensure
  @source = nil #free for GC
end

Protected Instance Methods

define_new_migration_class() click to toggle source
# File lib/cassie/schema/cassandra_migrations/migration_file.rb, line 43
def define_new_migration_class
  # define class in context of Object
  # as a migration file does, rather
  # than in the current context
  Object.class_eval(@source)
end
load_source() click to toggle source
# File lib/cassie/schema/cassandra_migrations/migration_file.rb, line 33
def load_source
  File.read(filename)
end
parse_description() click to toggle source
# File lib/cassie/schema/cassandra_migrations/migration_file.rb, line 28
def parse_description
  matches = File.basename(filename).match(/[0-9]*_(.*).rb$/).captures
  matches.first
end
redefine_class(name) click to toggle source
# File lib/cassie/schema/cassandra_migrations/migration_file.rb, line 37
def redefine_class(name)
  class_def = "class #{name} < Cassie::Schema::Migration"
  # class UserMigration < CassandraMigrations::Migration
  @source.sub!(/class\s.*<\s*CassandraMigrations::Migration/, class_def)
end