class RSpec::Core::Hooks::HookCollections

@private

Constants

HOOK_TYPES
SCOPES
SCOPE_ALIASES

Public Class Methods

new(owner, data) click to toggle source
# File lib/rspec/core/hooks.rb, line 450
def initialize(owner, data)
  @owner = owner
  @data  = data
end

Public Instance Methods

[](key) click to toggle source
# File lib/rspec/core/hooks.rb, line 455
def [](key)
  @data[key]
end
around_example_hooks_for(example, initial_procsy=nil) click to toggle source
# File lib/rspec/core/hooks.rb, line 468
def around_example_hooks_for(example, initial_procsy=nil)
  AroundHookCollection.new(FlatMap.flat_map(@owner.parent_groups) do |a|
    a.hooks[:around][:example]
  end).for(example, initial_procsy)
end
register(prepend_or_append, hook, *args, &block) click to toggle source
# File lib/rspec/core/hooks.rb, line 474
def register(prepend_or_append, hook, *args, &block)
  scope, options = scope_and_options_from(*args)
  self[hook][scope].__send__(prepend_or_append, HOOK_TYPES[hook][scope].new(block, options))
end
register_globals(host, globals) click to toggle source
# File lib/rspec/core/hooks.rb, line 459
def register_globals(host, globals)
  process(host, globals, :before, :example)
  process(host, globals, :after,  :example)
  process(host, globals, :around, :example)

  process(host, globals, :before, :context)
  process(host, globals, :after,  :context)
end
run(hook, scope, example_or_group, initial_procsy=nil) click to toggle source

@private

Runs all of the blocks stored with the hook in the context of the example. If no example is provided, just calls the hook directly.

# File lib/rspec/core/hooks.rb, line 483
def run(hook, scope, example_or_group, initial_procsy=nil)
  return if RSpec.configuration.dry_run?
  find_hook(hook, scope, example_or_group, initial_procsy).run
end

Private Instance Methods

after_context_hooks_for(group) click to toggle source
# File lib/rspec/core/hooks.rb, line 556
def after_context_hooks_for(group)
  GroupHookCollection.new(self[:after][:context]).for(group)
end
after_example_hooks_for(example) click to toggle source
# File lib/rspec/core/hooks.rb, line 566
def after_example_hooks_for(example)
  HookCollection.new(FlatMap.flat_map(@owner.parent_groups) do |a|
    a.hooks[:after][:example]
  end).for(example)
end
before_context_hooks_for(group) click to toggle source
# File lib/rspec/core/hooks.rb, line 552
def before_context_hooks_for(group)
  GroupHookCollection.new(self[:before][:context]).for(group)
end
before_example_hooks_for(example) click to toggle source
# File lib/rspec/core/hooks.rb, line 560
def before_example_hooks_for(example)
  HookCollection.new(FlatMap.flat_map(@owner.parent_groups.reverse) do |a|
    a.hooks[:before][:example]
  end).for(example)
end
extract_scope_from(args) click to toggle source
# File lib/rspec/core/hooks.rb, line 514
def extract_scope_from(args)
  if known_scope?(args.first)
    normalized_scope_for(args.shift)
  elsif args.any? { |a| a.is_a?(Symbol) }
    error_message = "You must explicitly give a scope (#{SCOPES.join(", ")}) or scope alias (#{SCOPE_ALIASES.keys.join(", ")}) when using symbols as metadata for a hook."
    raise ArgumentError.new error_message
  else
    :example
  end
end
find_hook(hook, scope, example_or_group, initial_procsy) click to toggle source
# File lib/rspec/core/hooks.rb, line 535
def find_hook(hook, scope, example_or_group, initial_procsy)
  case [hook, scope]
  when [:before, :context]
    before_context_hooks_for(example_or_group)
  when [:after, :context]
    after_context_hooks_for(example_or_group)
  when [:around, :example]
    around_example_hooks_for(example_or_group, initial_procsy)
  when [:before, :example]
    before_example_hooks_for(example_or_group)
  when [:after, :example]
    after_example_hooks_for(example_or_group)
  when [:before, :suite], [:after, :suite]
    self[hook][:suite].with(example_or_group)
  end
end
known_scope?(scope) click to toggle source

@api private

# File lib/rspec/core/hooks.rb, line 526
def known_scope?(scope)
  SCOPES.include?(scope) || SCOPE_ALIASES.keys.include?(scope)
end
normalized_scope_for(scope) click to toggle source

@api private

# File lib/rspec/core/hooks.rb, line 531
def normalized_scope_for(scope)
  SCOPE_ALIASES[scope] || scope
end
process(host, globals, position, scope) click to toggle source
# File lib/rspec/core/hooks.rb, line 502
def process(host, globals, position, scope)
  globals[position][scope].each do |hook|
    next unless scope == :example || hook.options_apply?(host)
    next if host.parent_groups.any? {|a| a.hooks[position][scope].include?(hook)}
    self[position][scope] << hook
  end
end
scope_and_options_from(*args) click to toggle source
# File lib/rspec/core/hooks.rb, line 510
def scope_and_options_from(*args)
  return extract_scope_from(args), Metadata.build_hash_from(args)
end