class RSpec::Core::Metadata::HashPopulator

@private Used internally to populate metadata hashes with computed keys managed by RSpec.

Attributes

block[R]
description_args[R]
metadata[R]
user_metadata[R]

Public Class Methods

new(metadata, user_metadata, index_provider, description_args, block) click to toggle source
# File lib/rspec/core/metadata.rb, line 120
def initialize(metadata, user_metadata, index_provider, description_args, block)
  @metadata         = metadata
  @user_metadata    = user_metadata
  @index_provider   = index_provider
  @description_args = description_args
  @block            = block
end

Public Instance Methods

populate() click to toggle source
# File lib/rspec/core/metadata.rb, line 128
def populate
  ensure_valid_user_keys

  metadata[:block]            = block
  metadata[:description_args] = description_args
  metadata[:description]      = build_description_from(*metadata[:description_args])
  metadata[:full_description] = full_description
  metadata[:described_class]  = described_class

  populate_location_attributes
  metadata.update(user_metadata)
end

Private Instance Methods

build_description_from(parent_description=nil, my_description=nil) click to toggle source
# File lib/rspec/core/metadata.rb, line 178
def build_description_from(parent_description=nil, my_description=nil)
  return parent_description.to_s unless my_description
  return my_description.to_s if parent_description.to_s == ''
  separator = description_separator(parent_description, my_description)
  (parent_description.to_s + separator) << my_description.to_s
end
build_scoped_id_for(file_path) click to toggle source
# File lib/rspec/core/metadata.rb, line 185
def build_scoped_id_for(file_path)
  index = @index_provider.call(file_path).to_s
  parent_scoped_id = metadata.fetch(:scoped_id) { return index }
  "#{parent_scoped_id}:#{index}"
end
description_separator(parent_part, child_part) click to toggle source
# File lib/rspec/core/metadata.rb, line 170
def description_separator(parent_part, child_part)
  if parent_part.is_a?(Module) && /^(?:#|::|\.)/.match(child_part.to_s)
    ''.freeze
  else
    ' '.freeze
  end
end
ensure_valid_user_keys() click to toggle source
# File lib/rspec/core/metadata.rb, line 191
        def ensure_valid_user_keys
          RESERVED_KEYS.each do |key|
            next unless user_metadata.key?(key)
            raise <<-EOM.gsub(/^\s+\|/, '')
              |#{"*" * 50}
              |:#{key} is not allowed
              |
              |RSpec reserves some hash keys for its own internal use,
              |including :#{key}, which is used on:
              |
              |  #{CallerFilter.first_non_rspec_line}.
              |
              |Here are all of RSpec's reserved hash keys:
              |
              |  #{RESERVED_KEYS.join("\n  ")}
              |#{"*" * 50}
            EOM
          end
        end
file_path_and_line_number_from(backtrace) click to toggle source
# File lib/rspec/core/metadata.rb, line 164
def file_path_and_line_number_from(backtrace)
  first_caller_from_outside_rspec = backtrace.find { |l| l !~ CallerFilter::LIB_REGEX }
  first_caller_from_outside_rspec ||= backtrace.first
  /(.+?):(\d+)(?:|:\d+)/.match(first_caller_from_outside_rspec).captures
end
populate_location_attributes() click to toggle source
# File lib/rspec/core/metadata.rb, line 143
def populate_location_attributes
  backtrace = user_metadata.delete(:caller)

  file_path, line_number = if backtrace
                             file_path_and_line_number_from(backtrace)
                           elsif block.respond_to?(:source_location)
                             block.source_location
                           else
                             file_path_and_line_number_from(caller)
                           end

  relative_file_path            = Metadata.relative_path(file_path)
  absolute_file_path            = File.expand_path(relative_file_path)
  metadata[:file_path]          = relative_file_path
  metadata[:line_number]        = line_number.to_i
  metadata[:location]           = "#{relative_file_path}:#{line_number}"
  metadata[:absolute_file_path] = absolute_file_path
  metadata[:rerun_file_path]  ||= relative_file_path
  metadata[:scoped_id]          = build_scoped_id_for(absolute_file_path)
end