Sha256: 75f43a3496a7e97224dac749a955531a0def66284ba340cb3249df8f19e01545

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

class MigrationGenerator < RubiGen::Base

  default_options :author => nil

  attr_reader :name

  def initialize(runtime_args, runtime_options = {})
    super
    usage if args.empty?
    @name = args.shift
    extract_options
  end

  def manifest
    record do |m|
      # Ensure appropriate folder(s) exists
      m.directory "db/"
      m.directory "db/migrate/"
      m.template  "db/migrate/migration.rb", 
        "db/migrate/#{next_migration_num}_#{name.underscore}.rb"

    end
  end

  protected
    def banner
      <<-EOS
Creates a ...

USAGE: #{$0} #{spec.name} name
EOS
    end

    def add_options!(opts)
      # opts.separator ''
      # opts.separator 'Options:'
      # For each option below, place the default
      # at the top of the file next to "default_options"
      # opts.on("-a", "--author=\"Your Name\"", String,
      #         "Some comment about this option",
      #         "Default: none") { |options[:author]| }
      # opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
    end

    def extract_options
      # for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
      # Templates can access these value via the attr_reader-generated methods, but not the
      # raw instance variable value.
      # @author = options[:author]
    end

    def next_migration_num
      #blegh, catalog existing migrations, find next
      current = Dir.glob(
        "#{@destination_root}/db/migrate/[0-9][0-9][0-9]_*.rb").map{|x|
          /(\d{3})_.*\.rb/.match(x)[1].to_i
      }.max
      current ||= 0
      "%03d" % current.succ
    end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
michaelbarton-gigantron-0.1.4 gigantron_generators/migration/migration_generator.rb
gigantron-0.1.0 gigantron_generators/migration/migration_generator.rb
gigantron-0.1.3 gigantron_generators/migration/migration_generator.rb
gigantron-0.1.1 gigantron_generators/migration/migration_generator.rb