Class: Crspec::ExecutionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/execution_context.rb

Constant Summary collapse

STORAGE_KEY =
:crspec_execution_context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example_id, metadata = {}, parent = nil) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.



30
31
32
33
34
35
36
# File 'lib/crspec/execution_context.rb', line 30

def initialize(example_id,  = {}, parent = nil)
  @example_id = example_id
  @metadata = .freeze
  @parent = parent
  @memoized_values = {}
  @monitor = Monitor.new
end

Instance Attribute Details

#example_idObject (readonly)

Returns the value of attribute example_id.



10
11
12
# File 'lib/crspec/execution_context.rb', line 10

def example_id
  @example_id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



10
11
12
# File 'lib/crspec/execution_context.rb', line 10

def 
  @metadata
end

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/crspec/execution_context.rb', line 10

def parent
  @parent
end

Class Method Details

.currentObject



12
13
14
# File 'lib/crspec/execution_context.rb', line 12

def self.current
  Fiber[STORAGE_KEY] ||= new(SecureRandom.uuid)
end

.isolate(example_id, metadata = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/crspec/execution_context.rb', line 16

def self.isolate(example_id,  = {})
  new_context = new(example_id, , Fiber[STORAGE_KEY])
  old_context = Fiber[STORAGE_KEY]
  old_space = Fiber[Mock::Space::STORAGE_KEY]
  Fiber[STORAGE_KEY] = new_context
  Fiber[Mock::Space::STORAGE_KEY] = nil
  begin
    yield new_context
  ensure
    Fiber[STORAGE_KEY] = old_context
    Fiber[Mock::Space::STORAGE_KEY] = old_space
  end
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
51
52
# File 'lib/crspec/execution_context.rb', line 48

def [](key)
  @monitor.synchronize do
    @memoized_values[key]
  end
end

#[]=(key, value) ⇒ Object



54
55
56
57
58
# File 'lib/crspec/execution_context.rb', line 54

def []=(key, value)
  @monitor.synchronize do
    @memoized_values[key] = value
  end
end

#fetch_memoized(key, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/crspec/execution_context.rb', line 38

def fetch_memoized(key, &block)
  return @memoized_values[key] if @memoized_values.key?(key)

  @monitor.synchronize do
    return @memoized_values[key] if @memoized_values.key?(key)

    @memoized_values[key] = yield
  end
end

#reset!Object



60
61
62
63
64
# File 'lib/crspec/execution_context.rb', line 60

def reset!
  @monitor.synchronize do
    @memoized_values.clear
  end
end