class RuboCop::RSpec::Hook

Wrapper for RSpec hook

Public Instance Methods

example?() click to toggle source
# File lib/rubocop/rspec/hook.rb, line 24
def example?
  scope.equal?(:each)
end
knowable_scope?() click to toggle source
# File lib/rubocop/rspec/hook.rb, line 18
def knowable_scope?
  scope_argument.nil? ||
    scope_argument.sym_type? ||
    scope_argument.hash_type?
end
metadata() click to toggle source
# File lib/rubocop/rspec/hook.rb, line 38
def metadata
  (extract_metadata(node) || [])
    .map { |meta| transform_metadata(meta) }
    .flatten
    .inject(&:merge)
end
name() click to toggle source
# File lib/rubocop/rspec/hook.rb, line 14
def name
  node.method_name
end
scope() click to toggle source
# File lib/rubocop/rspec/hook.rb, line 28
def scope
  return :each if scope_argument&.hash_type?

  case scope_name
  when nil, :each, :example then :each
  when :context, :all       then :context
  when :suite               then :suite
  end
end

Private Instance Methods

scope_argument() click to toggle source
# File lib/rubocop/rspec/hook.rb, line 74
def scope_argument
  node.send_node.first_argument
end
scope_name() click to toggle source
# File lib/rubocop/rspec/hook.rb, line 70
def scope_name
  scope_argument.to_a.first
end
transform_metadata(meta) click to toggle source
# File lib/rubocop/rspec/hook.rb, line 51
def transform_metadata(meta)
  if meta.sym_type?
    { meta => true }
  else
    # This check is to be able to compare those two hooks:
    #
    #   before(:example, :special) { ... }
    #   before(:example, special: true) { ... }
    #
    # In the second case it's a node with a pair that has a value
    # of a `true_type?`.
    meta.pairs.map { |pair| { pair.key => transform_true(pair.value) } }
  end
end
transform_true(node) click to toggle source
# File lib/rubocop/rspec/hook.rb, line 66
def transform_true(node)
  node.true_type? ? true : node
end
valid_scope?(node) click to toggle source
# File lib/rubocop/rspec/hook.rb, line 47
def valid_scope?(node)
  node&.sym_type? && Language::HookScopes.all(node.value)
end