require 'active_support/inflector'

module WithModel
  class Dsl
    NOOP = lambda {|*|}

    def initialize(name, example_group)
      @name = name
      @example_group = example_group
      @model_initialization = NOOP
      @table_block = NOOP
      @table_options = {}
    end

    def table(options = {}, &block)
      @table_options = options
      @table_block = block
    end

    def model(&block)
      @model_initialization = block
    end

    def execute
      model_initialization = @model_initialization
      const_name = @name.to_s.camelize
      table_name = "with_model_#{@name.to_s.tableize}_#{$$}"

      model = nil

      @example_group.with_table(table_name, @table_options, &@table_block)

      @example_group.before do
        model = Class.new(WithModel::Base)
        stub_const(const_name, model)
        Object.const_get(const_name).class_eval do
          self.table_name = table_name
          self.class_eval(&model_initialization)
        end
        model.reset_column_information
      end

      @example_group.after do
        model._with_model_deconstructor if model.respond_to?(:_with_model_deconstructor)
        if defined?(ActiveSupport::Dependencies::Reference)
          ActiveSupport::Dependencies::Reference.clear!
        end
      end
    end
  end
end

require 'active_support/inflector'

module WithModel
  class Dsl
    NOOP = lambda {|*|}

    def initialize(name, example_group)
      @name = name
      @example_group = example_group
      @model_initialization = NOOP
      @table_block = NOOP
      @table_options = {}
    end

    def table(options = {}, &block)
      @table_options = options
      @table_block = block
    end

    def model(&block)
      @model_initialization = block
    end

    def execute
      model_initialization = @model_initialization
      const_name = @name.to_s.camelize
      table_name = "with_model_#{@name.to_s.tableize}_#{$$}"

      model = nil

      @example_group.with_table(table_name, @table_options, &@table_block)

      @example_group.before do
        model = Class.new(WithModel::Base)
        stub_const(const_name, model)
        model.class_eval do
          self.table_name = table_name
          self.class_eval(&model_initialization)
        end
        model.reset_column_information
      end

      @example_group.after do
        model._with_model_deconstructor if model.respond_to?(:_with_model_deconstructor)
        if defined?(ActiveSupport::Dependencies::Reference)
          ActiveSupport::Dependencies::Reference.clear!
        end
      end
    end
  end
end

Simplify Dsl class_eval block syntax
Simplify Dsl class_eval block syntax