Sha256: 5fd0c3beff5f0d6d8a0e2b050509ea2407a59799ec72ba40b29d2b10e910dfa3

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'rails'

class MiGenerator < Rails::Generators::Base
  Methods = {
    '+' => 'add_column',
    '-' => 'remove_column',
    '%' => 'change_column',
  }.freeze

  source_root File.expand_path('../templates', __FILE__)

  def doing
    template 'migration.rb.erb', 'db/migrate/hogehoge.rb'
  end


  private

  def arg_groups
    @arg_groups ||= (
      args = @_initializer[0..1].flatten

      current = nil
      res = args.group_by do |a|
        if %w[+ - %].include? a[0]
          current
        else
          current = a
          nil
        end
      end
      res.delete(nil)
      res
    )
  end

  def migration_class_name
    'TODO'
  end

  # TODO: parse options
  # @param [String] col +COL_NAME:TYPE:{OPTIONS}
  def parse_column(col)
    name, type, options = *col.split(':')
    method = name[0]
    name = name[1..-1]
    {name: name, type: type, options: options, method: method}
  end

  # @param [String] col +COL_NAME:TYPE:{OPTIONS}
  def to_method(table, col)
    info = parse_column(col)
    res = "#{Methods[info[:method]]} :#{table}, :#{info[:name]}, :#{info[:type]}"
    res << info[:options] if info[:options]
    res
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mi-0.0.1 lib/generators/mi/mi_generator.rb