Sha256: ad5f5df762bf9fc4b335730da4456efe02de0788bd22f8a9f361f60cc0c72c51

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module RspecInContext
  module InContext
    class << self
      def included(base)
        base.extend ClassMethods
      end

      def contexts
        @contexts ||= ActiveSupport::HashWithIndifferentAccess.new
      end

      def add_context(context_name, &block)
        contexts[context_name] = block
      end

      def find_context(context_name)
        contexts[context_name] || raise("No context found with name #{context_name}")
      end

      def define_context(context_name, &block)
        InContext.add_context(context_name, &block)
      end
    end

    module ClassMethods
      def in_context(context_name, *args, &block)
        Thread.current[:test_block] = block
        instance_exec(*args, &InContext.find_context(context_name))
      end

      def execute_tests
        instance_exec(&Thread.current[:test_block]) if Thread.current[:test_block]
      end

      def define_context(context_name, &block)
        InContext.add_context(context_name, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec_in_context-0.1.1 lib/rspec_in_context/in_context.rb