Sha256: d48b58addefd936f59f80389970fcc9811f10e701482a7fa5c06627c37845e43

Contents?: true

Size: 1.63 KB

Versions: 27

Compression:

Stored size: 1.63 KB

Contents

require 'fileutils'
require 'active_support/core_ext/string'
require 'ardb/runner'

class Ardb::Runner::GenerateCommand

  def initialize(args)
    @item = args.shift
    @args = args
  end

  def run
    if @item.nil?
      raise Ardb::Runner::CmdError, "specify an item to generate"
    end
    if !self.respond_to?("#{@item}_cmd")
      raise Ardb::Runner::CmdError, "can't generate #{@item}"
    end

    begin
      self.send("#{@item}_cmd")
    rescue Ardb::Runner::CmdError => e
      raise e
    rescue Exception => e
      $stderr.puts e
      $stderr.puts "error generating #{@item}."
      raise Ardb::Runner::CmdFail
    end
  end

  def migration_cmd
    MigrationCommand.new(@args.first).run
  end

  class MigrationCommand
    attr_reader :identifier, :class_name, :file_name, :template

    def initialize(identifier)
      if identifier.nil?
        raise Ardb::Runner::CmdError, "specify a name for the migration"
      end

      @identifier = identifier
      @class_name = @identifier.classify.pluralize
      @file_name  = begin
        "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{@identifier.underscore}"
      end
      @template = "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 run
      FileUtils.mkdir_p Ardb.config.migrations_path
      file_path = File.join(Ardb.config.migrations_path, "#{@file_name}.rb")
      File.open(file_path, "w"){ |f| f.write(@template) }
      $stdout.puts file_path
    end
  end

end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
ardb-0.26.0 lib/ardb/runner/generate_command.rb
ardb-0.25.0 lib/ardb/runner/generate_command.rb
ardb-0.24.0 lib/ardb/runner/generate_command.rb
ardb-0.23.0 lib/ardb/runner/generate_command.rb
ardb-0.22.1 lib/ardb/runner/generate_command.rb
ardb-0.22.0 lib/ardb/runner/generate_command.rb
ardb-0.21.0 lib/ardb/runner/generate_command.rb
ardb-0.20.0 lib/ardb/runner/generate_command.rb
ardb-0.19.0 lib/ardb/runner/generate_command.rb
ardb-0.18.0 lib/ardb/runner/generate_command.rb
ardb-0.17.0 lib/ardb/runner/generate_command.rb
ardb-0.16.0 lib/ardb/runner/generate_command.rb
ardb-0.15.0 lib/ardb/runner/generate_command.rb
ardb-0.14.0 lib/ardb/runner/generate_command.rb
ardb-0.13.0 lib/ardb/runner/generate_command.rb
ardb-0.12.0 lib/ardb/runner/generate_command.rb
ardb-0.11.0 lib/ardb/runner/generate_command.rb
ardb-0.10.0 lib/ardb/runner/generate_command.rb
ardb-0.9.0 lib/ardb/runner/generate_command.rb
ardb-0.8.0 lib/ardb/runner/generate_command.rb