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 overridden

Meta method to add a new context

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rspec_in_context/in_context.rb', line 67

def add_context(
  context_name,
  owner = nil,
  namespace = nil,
  silent = true,
  &block
)
  if context_name.nil? ||
       (context_name.respond_to?(:empty?) && context_name.empty?)
    raise InvalidContextName, "context_name cannot be nil or empty"
  end
  unless block
    raise MissingDefinitionBlock, "define_context requires a block"
  end

  namespace ||= GLOBAL_CONTEXT
  ns_key = namespace.to_s
  name_key = context_name.to_s
  if contexts.dig(ns_key, name_key)
    warn("Overriding an existing context: #{context_name}@#{namespace}")
  end
  contexts[ns_key][name_key] = Context.new(
    block,
    owner,
    context_name,
    namespace,
    silent,
  )
end

.clear_all_contexts!Object

Remove all stored contexts (both scoped and global). Useful for memory cleanup in long-running test suites with dynamically generated contexts.



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

def clear_all_contexts!
  @contexts_mutex.synchronize { @contexts = nil }
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 Keys are normalized to strings so symbols and strings are interchangeable.



48
49
50
51
52
# File 'lib/rspec_in_context/in_context.rb', line 48

def contexts
  @contexts_mutex.synchronize do
    @contexts ||= Hash.new { |hash, key| hash[key.to_s] = {} }
  end
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.

Raises:



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rspec_in_context/in_context.rb', line 101

def find_context(context_name, namespace = nil)
  name_key = context_name.to_s
  result =
    if namespace && !namespace.to_s.empty?
      contexts.dig(namespace.to_s, name_key)
    else
      find_context_across_all_namespaces(name_key)
    end
  result ||
    (raise NoContextFound, "No context found with name #{context_name}")
end

.find_context_across_all_namespaces(name_key) ⇒ 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 Uses dig to avoid auto-vivifying empty namespace entries

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rspec_in_context/in_context.rb', line 117

def find_context_across_all_namespaces(name_key)
  matching_namespaces =
    contexts.select do |_, namespaced_contexts|
      namespaced_contexts[name_key]
    end
  if matching_namespaces.size > 1
    namespace_names = matching_namespaces.keys.join(", ")
    raise AmbiguousContextName,
          "Context '#{name_key}' exists in multiple namespaces (#{namespace_names}). " \
            "Please specify a namespace."
  end
  matching_namespaces.values.first&.[](name_key)
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



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

def included(base)
  base.extend ClassMethods
end

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



143
144
145
# File 'lib/rspec_in_context/in_context.rb', line 143

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



133
134
135
136
137
138
139
# File 'lib/rspec_in_context/in_context.rb', line 133

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