class Cassie::Schema::VersionWriter

Attributes

down_code[RW]
io[R]
up_code[RW]
version[R]

Public Class Methods

new(version, io=nil) click to toggle source
# File lib/cassie/schema/version_writer.rb, line 11
def initialize(version, io=nil)
  @io = io
  @version = version
  @up_code = default_up_code
  @down_code = default_down_code

  ensure_dir_exist
end

Public Instance Methods

basename() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 62
def basename
  "#{version_prefix}#{description_suffix}.rb"
end
directory() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 58
def directory
  Cassie::Schema.paths[:migrations_directory]
end
existing_file() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 66
def existing_file
  Dir.glob("#{directory}/#{version_prefix}*.rb").first
end
filename() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 54
def filename
  "#{directory}/#{basename}"
end
migration_contents() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 49
def migration_contents
  return @migration_contents if defined?(@migration_contents)
  build_migration_contents
end
migration_contents=(contents) click to toggle source
# File lib/cassie/schema/version_writer.rb, line 45
def migration_contents=(contents)
  @migration_contents = contents
end
with_io() { |io| ... } click to toggle source
# File lib/cassie/schema/version_writer.rb, line 31
def with_io
  if io.respond_to?(:<<)
    yield io
  else
    ensure_unique_version
    File.open(filename, 'w'){ |file| yield file }
    # load the file, so the definition for source
    # for the methods will come from that file
    # instead of (eval)
    load File.absolute_path(filename)
    filename
  end
end
write() click to toggle source

Writes the version's migration clas code to a new ruby migraton file in the Cassie::Schema.paths[:migrations_directory] @return [String] the filename of the path written @raise [IOError] if a file with a matching version already exists

# File lib/cassie/schema/version_writer.rb, line 25
def write
  with_io do |io|
    io << migration_contents
  end
end

Protected Instance Methods

build_migration_contents() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 82
    def build_migration_contents
      return @migration_contents if defined?(@migration_contents)
      <<-EOS
class #{version.migration_class_name} < Cassie::Schema::Migration
  def up
    #{up_code}
  end

  def down
    #{down_code}
  end
end
      EOS
    end
default_down_code() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 110
def default_down_code
  "# Code to execute when rolling back this migration"
end
default_up_code() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 106
def default_up_code
  "# Code to execute when applying this migration"
end
description_suffix() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 101
def description_suffix
  return nil unless version.description
  "_" + version.description.parameterize('_')
end
ensure_dir_exist() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 72
def ensure_dir_exist
  FileUtils.makedirs(directory) unless File.directory?(directory)
end
ensure_unique_version() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 76
def ensure_unique_version
  if existing_file
    raise IOError.new("A migration already exists for #{version.parts.join('.')} in #{existing_file}. Try bumping the version.")
  end
end
version_prefix() click to toggle source
# File lib/cassie/schema/version_writer.rb, line 97
def version_prefix
  version.parts.map{|p| p.to_s.rjust(4, "0") }.join('_')
end