Module: RspecInContext::InContext

Defined in:
lib/rspec_in_context/in_context.rb

Overview

Main module containing almost every methods

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

GLOBAL_CONTEXT =

Name of the Global context

:global_context

Class Method Summary collapse

Class Method Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Will warn if a context is overriden

Meta method to add a new context



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

def add_context(context_name, owner = nil, namespace = nil, &block)
  namespace ||= GLOBAL_CONTEXT
  warn("Overriding an existing context: #{context_name}@#{namespace}") if contexts[namespace][context_name]
  contexts[namespace][context_name] = Context.new(block, owner, context_name, namespace)
end

.contextsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Contexts container + creation



24
25
26
# File 'lib/rspec_in_context/in_context.rb', line 24

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

.find_context(context_name, namespace = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find a context.



40
41
42
43
44
45
46
47
# File 'lib/rspec_in_context/in_context.rb', line 40

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Look into every namespace to find the context



51
52
53
54
# File 'lib/rspec_in_context/in_context.rb', line 51

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hook for easier inclusion of the gem in RSpec



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

def included(base)
  base.extend ClassMethods
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define a context from outside a RSpec.describe block



66
67
68
# File 'lib/rspec_in_context/in_context.rb', line 66

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

.remove_context(current_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete a context



58
59
60
61
62
# File 'lib/rspec_in_context/in_context.rb', line 58

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