Module: RspecInContext::InContext

Defined in:
lib/rspec_in_context/in_context.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

GLOBAL_CONTEXT =
:global_context

Class Method Summary collapse

Class Method Details

.add_context(context_name, owner = nil, namespace = nil, &block) ⇒ Object



17
18
19
20
# File 'lib/rspec_in_context/in_context.rb', line 17

def add_context(context_name, owner = nil, namespace = nil, &block)
  namespace ||= GLOBAL_CONTEXT
  contexts[namespace][context_name] = Context.new(block, owner, context_name, namespace)
end

.contextsObject



13
14
15
# File 'lib/rspec_in_context/in_context.rb', line 13

def contexts
  @contexts ||= HashWithIndifferentAccess.new { |hash, key| hash[key] = HashWithIndifferentAccess.new }
end

.find_context(context_name, namespace = nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rspec_in_context/in_context.rb', line 22

def find_context(context_name, namespace = nil)
  if namespace&.present?
    contexts[namespace][context_name]
  else
    contexts[GLOBAL_CONTEXT][context_name] || find_context_in_any_namespace(context_name)
  end ||
    (raise NoContextFound, "No context found with name #{context_name}")
end

.find_context_in_any_namespace(context_name) ⇒ Object



31
32
33
34
# File 'lib/rspec_in_context/in_context.rb', line 31

def find_context_in_any_namespace(context_name)
  valid_namespace = contexts.find{ |_, namespaced_contexts| namespaced_contexts[context_name] }&.last
  valid_namespace[context_name] if valid_namespace
end

.included(base) ⇒ Object



9
10
11
# File 'lib/rspec_in_context/in_context.rb', line 9

def included(base)
  base.extend ClassMethods
end

.outside_define_context(context_name, namespace, &block) ⇒ Object



42
43
44
# File 'lib/rspec_in_context/in_context.rb', line 42

def outside_define_context(context_name, namespace, &block)
  InContext.add_context(context_name, nil, namespace, &block)
end

.remove_context(current_class) ⇒ Object



36
37
38
39
40
# File 'lib/rspec_in_context/in_context.rb', line 36

def remove_context(current_class)
  contexts.each_value do |namespaced_contexts|
    namespaced_contexts.delete_if{ |_, context| context.owner == current_class }
  end
end