Sha256: ca49e87de2c9fc8c7967d09a6fe46e712659b7641e455544c1717a3e9ce76185

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

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
        if defined?(ActiveSupport::Dependencies::Reference)
          ActiveSupport::Dependencies::Reference.clear!
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
with_model-0.3.1 lib/with_model/dsl.rb
with_model-0.3 lib/with_model/dsl.rb