class RSpec::Core::MemoizedHelpers::BeforeAllMemoizedHash

Used internally to customize the behavior of the memoized hash when used in a `before(:all)` hook.

@private

Public Class Methods

isolate_for_before_all(example_group_instance) { || ... } click to toggle source
# File lib/rspec/core/memoized_helpers.rb, line 102
def self.isolate_for_before_all(example_group_instance)
  example_group_instance.instance_eval do
    @__memoized = BeforeAllMemoizedHash.new(self)

    begin
      yield
    ensure
      @__memoized.preserve_accessed_lets
      @__memoized = nil
    end
  end
end
new(example_group_instance) click to toggle source
# File lib/rspec/core/memoized_helpers.rb, line 97
def initialize(example_group_instance)
  @example_group_instance = example_group_instance
  @hash = {}
end

Public Instance Methods

[]=(key, value) click to toggle source
# File lib/rspec/core/memoized_helpers.rb, line 137
def []=(key, value)
  @hash[key] = value
end
fetch(key, &block) click to toggle source
# File lib/rspec/core/memoized_helpers.rb, line 115
        def fetch(key, &block)
          description = if key == :subject
            "subject"
          else
            "let declaration `#{key}`"
          end

          ::RSpec.warn_deprecation <<-EOS
WARNING: #{description} accessed in a `before(:all)` hook at:
  #{caller[1]}

This is deprecated behavior that will not be supported in RSpec 3.

`let` and `subject` declarations are not intended to be called
in a `before(:all)` hook, as they exist to define state that
is reset between each example, while `before(:all)` exists to
define state that is shared across examples in an example group.
EOS

          @hash.fetch(key, &block)
        end
preserve_accessed_lets() click to toggle source
# File lib/rspec/core/memoized_helpers.rb, line 141
def preserve_accessed_lets
  hash = @hash

  @example_group_instance.class.class_eval do
    hash.each do |key, value|
      undef_method(key) if method_defined?(key)
      define_method(key) { value }
    end
  end
end