Sha256: 35a41952c361dc377661a1142dc094e7f84d70112094c77d360e352fcc6ac0e2

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require 'rails/generators'

module Erd
  class GenaratorRunner
    class << self
      # runs `rails g model [name]`
      # @return generated migration filename
      def execute_generate_model(name, options = nil)
        result = execute_generator 'model', name, options
        result.flatten.grep(%r(/db/migrate/.*\.rb))
      end

      # runs `rails g migration [name]`
      # @return generated migration filename
      def execute_generate_migration(name, options = nil)
        result = execute_generator 'migration', name, options
        result.last.last
      end

      private
      # a dirty workaround to make rspec-rails run
      def overwriting_argv(value, &block)
        original_argv = ARGV.dup
        ARGV.clear
        ARGV.concat value
        block.call
      ensure
        ARGV.clear
        ARGV.concat original_argv
      end

      def execute_generator(type, name, options = nil)
        overwriting_argv([name, options]) do
          Rails::Generators.configure! Rails.application.config.generators
          result = Rails::Generators.invoke type, [name, options], :behavior => :invoke, :destination_root => Rails.root
          raise ::Erd::MigrationError, "#{name}#{"(#{options})" if options}" unless result
          result
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
erd-0.8.2 lib/erd/generator_runner.rb
erd-0.8.1 lib/erd/generator_runner.rb
erd-0.8.0 lib/erd/generator_runner.rb
erd-0.7.0 lib/erd/generator_runner.rb
erd-0.6.4 lib/erd/generator_runner.rb
erd-0.6.3 lib/erd/generator_runner.rb
erd-0.6.2 lib/erd/generator_runner.rb
erd-0.6.1 lib/erd/generator_runner.rb
erd-0.6.0 lib/erd/generator_runner.rb
erd-0.5.0 lib/erd/generator_runner.rb