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, description_args, block) click to toggle source
# File lib/rspec/core/metadata.rb, line 68
def initialize(metadata, user_metadata, description_args, block)
  @metadata         = metadata
  @user_metadata    = user_metadata
  @description_args = description_args
  @block            = block
end

Public Instance Methods

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

  metadata[:execution_result] = Example::ExecutionResult.new
  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)
  RSpec.configuration.apply_derived_metadata_to(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 121
def build_description_from(parent_description=nil, my_description=nil)
  return parent_description.to_s unless my_description
  separator = description_separator(parent_description, my_description)
  parent_description.to_s + separator + my_description
end
description_separator(parent_part, child_part) click to toggle source
# File lib/rspec/core/metadata.rb, line 113
def description_separator(parent_part, child_part)
  if parent_part.is_a?(Module) && child_part =~ /^(#|::|\.)/
    ''
  else
    ' '
  end
end
ensure_valid_user_keys() click to toggle source
# File lib/rspec/core/metadata.rb, line 127
        def ensure_valid_user_keys
          RESERVED_KEYS.each do |key|
            if user_metadata.has_key?(key)
              raise "                |#{"*"*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}
".gsub(/^\s+\|/, '')
            end
          end
        end
file_path_and_line_number_from(backtrace) click to toggle source
# File lib/rspec/core/metadata.rb, line 107
def file_path_and_line_number_from(backtrace)
  first_caller_from_outside_rspec = backtrace.detect { |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 92
def populate_location_attributes
  file_path, line_number = if backtrace = user_metadata.delete(:caller)
    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

  file_path              = Metadata.relative_path(file_path)
  metadata[:file_path]   = file_path
  metadata[:line_number] = line_number.to_i
  metadata[:location]    = "#{file_path}:#{line_number}"
end