123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Core::HashImitatable Private

Do not use. This module is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: rspec-core/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.

Class Method Summary

Instance Method Summary

Class Method Details

.included(klass)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 350

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

Instance Method Details

#[](key)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 388

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

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

#[]=(key, value)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 398

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

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

#directly_supports_attribute?(name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 414

def directly_supports_attribute?(name)
  self.class.hash_attribute_names.include?(name)
end

#extra_hash_attributes (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 410

def extra_hash_attributes
  @extra_hash_attributes ||= {}
end

#get_value(name) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 418

def get_value(name)
  __send__(name)
end

#hash_for_delegation (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 426

def hash_for_delegation
  to_h
end

#issue_deprecation(_method_name, *_args) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 430

def issue_deprecation(_method_name, *_args)
  # no-op by default: subclasses can override
end

#set_value(name, value) (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 422

def set_value(name, value)
  __send__(:"#{name}=", value)
end

#to_h

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/metadata.rb', line 354

def to_h
  hash = extra_hash_attributes.dup

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

  hash
end