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, silent = true, &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



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

def add_context(context_name, owner = nil, namespace = nil, silent = true, &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, silent)
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



29
30
31
# File 'lib/rspec_in_context/in_context.rb', line 29

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.



45
46
47
48
49
50
51
# File 'lib/rspec_in_context/in_context.rb', line 45

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



55
56
57
58
# File 'lib/rspec_in_context/in_context.rb', line 55

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



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

def included(base)
  base.extend ClassMethods
end

.outside_define_context(context_name, namespace, silent, &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



70
71
72
# File 'lib/rspec_in_context/in_context.rb', line 70

def outside_define_context(context_name, namespace, silent, &block)
  InContext.add_context(context_name, nil, namespace, silent, &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



62
63
64
65
66
# File 'lib/rspec_in_context/in_context.rb', line 62

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