Module: AlwaysExecute::ShouldaExecute

Included in:
Shoulda::Context, Shoulda::Context::Context
Defined in:
lib/always_execute/shoulda_execute.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/always_execute/shoulda_execute.rb', line 3

def self.included(klass)
  klass.class_eval do
    @@shoulda_execute_class = klass
    
    def execute(&execute_block)
      setup do
        @execute_block = execute_block
      end
    end

    alias :should_without_execute :should

    def should(name = nil, options = {}, &block)
      if block.nil?
        should_without_execute(name, options)
      else
        should_without_execute(name, options) do
          if @execute_block
            self.instance_variable_set('@execute_result', @@shoulda_execute_class.shoulda_execute_call_block(self, @execute_block))
          end
          @@shoulda_execute_class.shoulda_execute_call_block(self, block)
        end
      end
    end
    
    def self.shoulda_execute_class
      @@shoulda_execute_class
    end
    
    def self.shoulda_execute_call_block(test, block)
      if should_execute_shared_should_available?
        test.call_block_with_shared_value(block)
      else
        block.bind(test).call
      end
    end

    def self.should_execute_shared_should_available?
      Test::Unit::TestCase.respond_to? :share_context
    end
  end
end