Sha256: 5a57afd587038b4e0d15acfbbb17e64ee994dcebda9e2bd1157d63bda0f41cc3

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require 'fileutils'

module Ardb

  class Migration

    attr_reader :migrations_path, :identifier
    attr_reader :class_name, :file_name, :file_path, :source

    def initialize(ardb_config, identifier)
      raise NoIdentifierError if identifier.to_s.empty?

      @migrations_path = ardb_config.migrations_path
      @identifier      = identifier

      @class_name = @identifier.classify.pluralize
      @file_name  = get_file_name(@identifier)
      @file_path  = File.join(self.migrations_path, "#{@file_name}.rb")

      @source = "require 'ardb/migration_helpers'\n\n" \
                "class #{@class_name} < ActiveRecord::Migration\n" \
                "  include Ardb::MigrationHelpers\n\n" \
                "  def change\n" \
                "  end\n\n" \
                "end\n"
    end

    def save!
      FileUtils.mkdir_p self.migrations_path
      File.open(self.file_path, 'w'){ |f| f.write(self.source) }
      self
    end

    private

    def get_file_name(identifier)
      "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{identifier.underscore}"
    end

    NoIdentifierError = Class.new(ArgumentError)

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ardb-0.28.3 lib/ardb/migration.rb
ardb-0.28.2 lib/ardb/migration.rb
ardb-0.28.1 lib/ardb/migration.rb
ardb-0.28.0 lib/ardb/migration.rb