Class: RSpec::Core::Hooks::HookCollections

### Home Classes Methods

### In Files

- lib/rspec/core/hooks.rb

### Parent

Object

### Methods

- ::new
- #all_hooks_for
- #matching_hooks_for
- #processable_hooks_for
- #register
- #register_global_singleton_context_hooks
- #register_globals
- #run
- #run_owned_hooks_for

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Core
- RSpec::Core::AnonymousExampleGroup
- RSpec::Core::BacktraceFormatter
- RSpec::Core::Bisect
- RSpec::Core::Bisect::Coordinator
- RSpec::Core::Bisect::ExampleMinimizer
- RSpec::Core::Bisect::Runner
- RSpec::Core::Bisect::Server
- RSpec::Core::Bisect::SubsetEnumerator
- RSpec::Core::Configuration
- RSpec::Core::Configuration::DeprecationReporterBuffer
- RSpec::Core::Configuration::ExposeCurrentExample
- RSpec::Core::Configuration::MustBeConfiguredBeforeExampleGroupsError
- RSpec::Core::ConfigurationOptions
- RSpec::Core::DRbOptions
- RSpec::Core::DRbRunner
- RSpec::Core::DSL
- RSpec::Core::DSL::RSpec
- RSpec::Core::Example
- RSpec::Core::Example::ExecutionResult
- RSpec::Core::Example::Procsy
- RSpec::Core::ExampleGroup
- RSpec::Core::ExampleStatusDumper
- RSpec::Core::ExampleStatusMerger
- RSpec::Core::ExampleStatusParser
- RSpec::Core::ExampleStatusPersister
- RSpec::Core::ExclusionRules
- RSpec::Core::FilterManager
- RSpec::Core::FilterRules
- RSpec::Core::FilterableItemRepository
- RSpec::Core::FilterableItemRepository::QueryOptimized
- RSpec::Core::FilterableItemRepository::UpdateOptimized
- RSpec::Core::FlatMap
- RSpec::Core::Formatters
- RSpec::Core::Formatters::BaseFormatter
- RSpec::Core::Formatters::BaseTextFormatter
- RSpec::Core::Formatters::BisectDebugFormatter
- RSpec::Core::Formatters::BisectFormatter
- RSpec::Core::Formatters::BisectProgressFormatter
- RSpec::Core::Formatters::ConsoleCodes
- RSpec::Core::Formatters::DeprecationFormatter
- RSpec::Core::Formatters::DeprecationFormatter::DelayedPrinter
- RSpec::Core::Formatters::DeprecationFormatter::FileStream
- RSpec::Core::Formatters::DeprecationFormatter::ImmediatePrinter
- RSpec::Core::Formatters::DeprecationFormatter::RaiseErrorStream
- RSpec::Core::Formatters::DocumentationFormatter
- RSpec::Core::Formatters::ExceptionPresenter
- RSpec::Core::Formatters::ExceptionPresenter::Factory
- RSpec::Core::Formatters::ExceptionPresenter::Factory::CommonBacktraceTruncater
- RSpec::Core::Formatters::ExceptionPresenter::Factory::EmptyBacktraceFormatter
- RSpec::Core::Formatters::FallbackMessageFormatter
- RSpec::Core::Formatters::Helpers
- RSpec::Core::Formatters::HtmlFormatter
- RSpec::Core::Formatters::HtmlPrinter
- RSpec::Core::Formatters::JsonFormatter
- RSpec::Core::Formatters::Loader
- RSpec::Core::Formatters::ProfileFormatter
- RSpec::Core::Formatters::ProgressFormatter
- RSpec::Core::Formatters::Protocol
- RSpec::Core::Formatters::SnippetExtractor
- RSpec::Core::Formatters::SnippetExtractor::CoderayConverter
- RSpec::Core::Formatters::SnippetExtractor::NullConverter
- RSpec::Core::HashImitatable
- RSpec::Core::HashImitatable::ClassMethods
- RSpec::Core::Hooks
- RSpec::Core::Hooks::AfterContextHook
- RSpec::Core::Hooks::AfterHook
- RSpec::Core::Hooks::AroundHook
- RSpec::Core::Hooks::BeforeHook
- RSpec::Core::Hooks::Hook
- RSpec::Core::Hooks::HookCollections
- RSpec::Core::InclusionRules
- RSpec::Core::LegacyExampleGroupHash
- RSpec::Core::MemoizedHelpers
- RSpec::Core::MemoizedHelpers::ClassMethods
- RSpec::Core::MemoizedHelpers::ContextHookMemoized
- RSpec::Core::MemoizedHelpers::ContextHookMemoized::After
- RSpec::Core::MemoizedHelpers::ContextHookMemoized::Before
- RSpec::Core::MemoizedHelpers::NonThreadSafeMemoized
- RSpec::Core::MemoizedHelpers::ThreadsafeMemoized
- RSpec::Core::Metadata
- RSpec::Core::Metadata::ExampleGroupHash
- RSpec::Core::Metadata::ExampleHash
- RSpec::Core::Metadata::HashPopulator
- RSpec::Core::MetadataFilter
- RSpec::Core::MinitestAssertionsAdapter
- RSpec::Core::MockingAdapters
- RSpec::Core::MockingAdapters::Flexmock
- RSpec::Core::MockingAdapters::Mocha
- RSpec::Core::MockingAdapters::Null
- RSpec::Core::MockingAdapters::RR
- RSpec::Core::MockingAdapters::RSpec
- RSpec::Core::MultipleExceptionError
- RSpec::Core::MultipleExceptionError::InterfaceTag
- RSpec::Core::Mutex
- RSpec::Core::Notifications
- RSpec::Core::Notifications::CustomNotification
- RSpec::Core::Notifications::DeprecationNotification
- RSpec::Core::Notifications::ExampleNotification
- RSpec::Core::Notifications::ExamplesNotification
- RSpec::Core::Notifications::FailedExampleNotification
- RSpec::Core::Notifications::NullColorizer
- RSpec::Core::Notifications::NullNotification
- RSpec::Core::Notifications::PendingExampleFailedAsExpectedNotification
- RSpec::Core::Notifications::PendingExampleFixedNotification
- RSpec::Core::Notifications::ProfileNotification
- RSpec::Core::Notifications::SeedNotification
- RSpec::Core::Notifications::SkippedExampleNotification
- RSpec::Core::Notifications::SummaryNotification
- RSpec::Core::NullReporter
- RSpec::Core::Ordering
- RSpec::Core::Ordering::ConfigurationManager
- RSpec::Core::Ordering::Custom
- RSpec::Core::Ordering::Identity
- RSpec::Core::Ordering::Random
- RSpec::Core::Ordering::Registry
- RSpec::Core::Parser
- RSpec::Core::Pending
- RSpec::Core::Pending::PendingExampleFixedError
- RSpec::Core::Pending::SkipDeclaredInExample
- RSpec::Core::Profiler
- RSpec::Core::ProjectInitializer
- RSpec::Core::RakeTask
- RSpec::Core::ReentrantMutex
- RSpec::Core::Reporter
- RSpec::Core::RubyProject
- RSpec::Core::Runner
- RSpec::Core::Sandbox
- RSpec::Core::Set
- RSpec::Core::SharedContext
- RSpec::Core::SharedExampleGroup
- RSpec::Core::SharedExampleGroup::Registry
- RSpec::Core::SharedExampleGroup::TopLevelDSL
- RSpec::Core::SharedExampleGroupInclusionStackFrame
- RSpec::Core::SharedExampleGroupModule
- RSpec::Core::ShellEscape
- RSpec::Core::SuiteHookContext
- RSpec::Core::TestUnitAssertionsAdapter
- RSpec::Core::Time
- RSpec::Core::Version
- RSpec::Core::Warnings
- RSpec::Core::World
- RSpec::ExampleGroups
- Object

No matching classes.

# RSpec::Core::Hooks::HookCollections

@private

This provides the primary API used by other parts of rspec-core. By hiding all implementation details behind this facade, it's allowed us to heavily optimize this, so that, for example, hook collection objects are only instantiated when a hook is added. This allows us to avoid many object allocations for the common case of a group having no hooks.

This is only possible because this interface provides a "tell, don't ask"-style API, so that callers tell this class what to do with the hooks, rather than asking this class for a list of hooks, and then doing something with them.

### Constants

EMPTY_HOOK_ARRAY
HOOK_TYPES
SCOPES
SCOPE_ALIASES

### Public Class Methods

new(owner, filterable_item_repo_class) click to toggle source

    # File lib/rspec/core/hooks.rb, line 420
    def initialize(owner, filterable_item_repo_class)
      @owner                      = owner
      @filterable_item_repo_class = filterable_item_repo_class
      @before_example_hooks       = nil
      @after_example_hooks        = nil
      @before_context_hooks       = nil
      @after_context_hooks        = nil
      @around_example_hooks       = nil
    end

### Public Instance Methods

register(prepend_or_append, position, *args, &block) click to toggle source

    # File lib/rspec/core/hooks.rb, line 448
    def register(prepend_or_append, position, *args, &block)
      scope, options = scope_and_options_from(*args)

      if scope == :suite
        # TODO: consider making this an error in RSpec 4. For SemVer reasons,
        # we are only warning in RSpec 3.
        RSpec.warn_with "WARNING: `#{position}(:suite)` hooks are only supported on "                              "the RSpec configuration object. This "                              "`#{position}(:suite)` hook, registered on an example "                              "group, will be ignored."
        return
      end

      hook = HOOK_TYPES[position][scope].new(block, options)
      ensure_hooks_initialized_for(position, scope).__send__(prepend_or_append, hook, options)
    end

register_global_singleton_context_hooks(example, globals) click to toggle source

    # File lib/rspec/core/hooks.rb, line 441
    def register_global_singleton_context_hooks(example, globals)
      parent_groups = example.example_group.parent_groups

      process(example, parent_groups, globals, :before, :context) { {} }
      process(example, parent_groups, globals, :after,  :context) { {} }
    end

register_globals(host, globals) click to toggle source

    # File lib/rspec/core/hooks.rb, line 430
    def register_globals(host, globals)
      parent_groups = host.parent_groups

      process(host, parent_groups, globals, :before, :example, &:options)
      process(host, parent_groups, globals, :after,  :example, &:options)
      process(host, parent_groups, globals, :around, :example, &:options)

      process(host, parent_groups, globals, :before, :context, &:options)
      process(host, parent_groups, globals, :after,  :context, &:options)
    end

run(position, scope, example_or_group) 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 469
    def run(position, scope, example_or_group)
      return if RSpec.configuration.dry_run?

      if scope == :context
        run_owned_hooks_for(position, :context, example_or_group)
      else
        case position
        when :before then run_example_hooks_for(example_or_group, :before, :reverse_each)
        when :after  then run_example_hooks_for(example_or_group, :after,  :each)
        when :around then run_around_example_hooks_for(example_or_group) { yield }
        end
      end
    end

### Protected Instance Methods

all_hooks_for(position, scope) click to toggle source

    # File lib/rspec/core/hooks.rb, line 515
    def all_hooks_for(position, scope)
      hooks_for(position, scope) { return EMPTY_HOOK_ARRAY }.items_and_filters.map(&:first)
    end

matching_hooks_for(position, scope, example_or_group) click to toggle source

    # File lib/rspec/core/hooks.rb, line 499
    def matching_hooks_for(position, scope, example_or_group)
      repository = hooks_for(position, scope) { return EMPTY_HOOK_ARRAY }

      # It would be nice to not have to switch on type here, but
      # we don't want to define `ExampleGroup#metadata` because then
      # `metadata` from within an individual example would return the
      # group's metadata but the user would probably expect it to be
      # the example's metadata.
      metadata = case example_or_group
                 when ExampleGroup then example_or_group.class.metadata
                 else example_or_group.metadata
                 end

      repository.items_for(metadata)
    end

processable_hooks_for(position, scope, host) click to toggle source

    # File lib/rspec/core/hooks.rb, line 525
    def processable_hooks_for(position, scope, host)
      if scope == :example
        all_hooks_for(position, scope)
      else
        matching_hooks_for(position, scope, host)
      end
    end

run_owned_hooks_for(position, scope, example_or_group) click to toggle source

    # File lib/rspec/core/hooks.rb, line 519
    def run_owned_hooks_for(position, scope, example_or_group)
      matching_hooks_for(position, scope, example_or_group).each do |hook|
        hook.run(example_or_group)
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.