class RuboCop::Cop::Style::ObjectThen

Enforces the use of consistent method names ‘Object#yield_self` or `Object#then`.

@example EnforcedStyle: then (default)

# bad
obj.yield_self { |x| x.do_something }

# good
obj.then { |x| x.do_something }

@example EnforcedStyle: yield_self

# bad
obj.then { |x| x.do_something }

# good
obj.yield_self { |x| x.do_something }

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/style/object_then.rb, line 34
def on_block(node)
  check_method_node(node.send_node)
end
Also aliased as: on_numblock
on_numblock(node)
Alias for: on_block
on_send(node) click to toggle source
# File lib/rubocop/cop/style/object_then.rb, line 40
def on_send(node)
  return unless node.arguments.one? && node.first_argument.block_pass_type?

  check_method_node(node)
end

Private Instance Methods

check_method_node(node) click to toggle source
# File lib/rubocop/cop/style/object_then.rb, line 48
def check_method_node(node)
  return unless preferred_method?(node)

  message = message(node)
  add_offense(node.loc.selector, message: message) do |corrector|
    prefer = style == :then && node.receiver.nil? ? 'self.then' : style

    corrector.replace(node.loc.selector, prefer)
  end
end
message(node) click to toggle source
# File lib/rubocop/cop/style/object_then.rb, line 70
def message(node)
  format(MSG, prefer: style.to_s, current: node.method_name)
end
preferred_method?(node) click to toggle source
# File lib/rubocop/cop/style/object_then.rb, line 59
def preferred_method?(node)
  case style
  when :then
    node.method?(:yield_self)
  when :yield_self
    node.method?(:then)
  else
    false
  end
end