Module: RSpec::Core::HashImitatable

Included in:
Example::ExecutionResult, LegacyExampleGroupHash
Defined in:
lib/rspec/core/metadata.rb

Overview

Mixin that makes the including class imitate a hash for backwards compatibility. The including class should use `attr_accessor` to declare attributes.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



351
352
353
# File 'lib/rspec/core/metadata.rb', line 351

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#[](key) ⇒ Object



389
390
391
392
393
394
395
396
397
# File 'lib/rspec/core/metadata.rb', line 389

def [](key)
  issue_deprecation(:[], key)

  if directly_supports_attribute?(key)
    get_value(key)
  else
    extra_hash_attributes[key]
  end
end

#[]=(key, value) ⇒ Object



399
400
401
402
403
404
405
406
407
# File 'lib/rspec/core/metadata.rb', line 399

def []=(key, value)
  issue_deprecation(:[]=, key, value)

  if directly_supports_attribute?(key)
    set_value(key, value)
  else
    extra_hash_attributes[key] = value
  end
end

#to_hObject



355
356
357
358
359
360
361
362
363
# File 'lib/rspec/core/metadata.rb', line 355

def to_h
  hash = extra_hash_attributes.dup

  self.class.hash_attribute_names.each do |name|
    hash[name] = __send__(name)
  end

  hash
end