module RSpec::Core::MetadataFilter

Contains metadata filtering logic. This has been extracted from the metadata classes because it operates ON a metadata hash but does not manage any of the state in the hash. We’re moving towards having metadata be a raw hash (not a custom subclass), so externalizing this filtering logic helps us move in that direction.

Public Class Methods

apply?(predicate, filters, metadata) click to toggle source

@private

# File lib/rspec/core/metadata_filter.rb, line 11
def apply?(predicate, filters, metadata)
  filters.__send__(predicate) { |k, v| filter_applies?(k, v, metadata) }
end
filter_applies?(key, filter_value, metadata) click to toggle source

@private

# File lib/rspec/core/metadata_filter.rb, line 16
def filter_applies?(key, filter_value, metadata)
  silence_metadata_example_group_deprecations do
    return location_filter_applies?(filter_value, metadata) if key == :locations
    return id_filter_applies?(filter_value, metadata)       if key == :ids
    return filters_apply?(key, filter_value, metadata)      if Hash === filter_value

    meta_value = metadata.fetch(key) { return false }

    return true if TrueClass === filter_value && meta_value
    return proc_filter_applies?(key, filter_value, metadata) if Proc === filter_value
    return filter_applies_to_any_value?(key, filter_value, metadata) if Array === meta_value

    filter_value === meta_value || filter_value.to_s == meta_value.to_s
  end
end
silence_metadata_example_group_deprecations() { || ... } click to toggle source

@private

# File lib/rspec/core/metadata_filter.rb, line 33
def silence_metadata_example_group_deprecations
  RSpec::Support.thread_local_data[:silence_metadata_example_group_deprecations] = true
  yield
ensure
  RSpec::Support.thread_local_data.delete(:silence_metadata_example_group_deprecations)
end

Private Class Methods

filter_applies_to_any_value?(key, value, metadata) click to toggle source
# File lib/rspec/core/metadata_filter.rb, line 42
def filter_applies_to_any_value?(key, value, metadata)
  metadata[key].any? { |v| filter_applies?(key, v,  key => value) }
end
filters_apply?(key, value, metadata) click to toggle source
# File lib/rspec/core/metadata_filter.rb, line 73
def filters_apply?(key, value, metadata)
  subhash = metadata[key]
  return false unless Hash === subhash || HashImitatable === subhash
  value.all? { |k, v| filter_applies?(k, v, subhash) }
end
id_filter_applies?(rerun_paths_to_scoped_ids, metadata) click to toggle source
# File lib/rspec/core/metadata_filter.rb, line 46
def id_filter_applies?(rerun_paths_to_scoped_ids, metadata)
  scoped_ids = rerun_paths_to_scoped_ids.fetch(metadata[:rerun_file_path]) { return false }

  Metadata.ascend(metadata).any? do |meta|
    scoped_ids.include?(meta[:scoped_id])
  end
end
location_filter_applies?(locations, metadata) click to toggle source
# File lib/rspec/core/metadata_filter.rb, line 54
def location_filter_applies?(locations, metadata)
  Metadata.ascend(metadata).any? do |meta|
    file_path = meta[:absolute_file_path]
    line_num  = meta[:line_number]

    locations[file_path].any? do |filter_line_num|
      line_num == RSpec.world.preceding_declaration_line(file_path, filter_line_num)
    end
  end
end
proc_filter_applies?(key, proc, metadata) click to toggle source
# File lib/rspec/core/metadata_filter.rb, line 65
def proc_filter_applies?(key, proc, metadata)
  case proc.arity
  when 0 then proc.call
  when 2 then proc.call(metadata[key], metadata)
  else proc.call(metadata[key])
  end
end