module RspecInContext::InContext

Main module containing almost every methods

Constants

GLOBAL_CONTEXT

Name of the Global context

Public Class Methods

add_context(context_name, owner = nil, namespace = nil, silent = true, &block) click to toggle source

Meta method to add a new context @api private

@note Will warn if a context is overriden

# 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
contexts() click to toggle source

Contexts container + creation @api private

# 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) click to toggle source

Find a context. @api private

# 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) click to toggle source

Look into every namespace to find the context @api private

# 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) click to toggle source

Hook for easier inclusion of the gem in RSpec @api private

# 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) click to toggle source

@api private Define a context from outside a RSpec.describe block

# 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) click to toggle source

@api private Delete a context

# 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