Sha256: 870c6982a21c8b1acb11c705c94c8ef23f1017d8dd4ee58b0621710a64feeff7

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 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 model.superclass.respond_to?(:direct_descendants)
          model.superclass.direct_descendants.delete(model)
        end
        if defined?(ActiveSupport::Dependencies::Reference)
          ActiveSupport::Dependencies::Reference.clear!
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
with_model-0.3.2 lib/with_model/dsl.rb