Class: RSpec::Core::LegacyExampleGroupHash

Inherits:
Object
  • Object
show all
Includes:
HashImitatable
Defined in:
lib/rspec/core/metadata.rb

Overview

Together with the example group metadata hash default block, provides backwards compatibility for the old `:example_group` key. In RSpec 2.x, the computed keys of a group's metadata were exposed from a nested subhash keyed by `[:example_group]`, and then the parent group's metadata was exposed by sub-subhash keyed by `[:example_group]`.

In RSpec 3, we reorganized this to that the computed keys are exposed directly of the group metadata hash (no nesting), and `:parent_example_group` returns the parent group's metadata.

Maintaining backwards compatibility was difficult: we wanted `:example_group` to return an object that:

* Exposes the top-level metadata keys that used to be nested
  under `:example_group`.
* Supports mutation (rspec-rails, for example, assigns
  `metadata[:example_group][:described_class]` when you use
  anonymous controller specs) such that changes are written
  back to the top-level metadata hash.
* Exposes the parent group metadata as
  `[:example_group][:example_group]`.

Instance Method Summary collapse

Methods included from HashImitatable

#[], #[]=, included

Constructor Details

#initialize(metadata) ⇒ LegacyExampleGroupHash

Returns a new instance of LegacyExampleGroupHash



474
475
476
477
478
# File 'lib/rspec/core/metadata.rb', line 474

def initialize()
  @metadata = 
   = .fetch(:parent_example_group) { {} }[:example_group]
  self[:example_group] =  if 
end

Instance Method Details

#to_hObject



480
481
482
# File 'lib/rspec/core/metadata.rb', line 480

def to_h
  super.merge(@metadata)
end