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
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/style/object_then.rb, line 35 def on_block(node) return unless RESTRICT_ON_SEND.include?(node.method_name) check_method_node(node.send_node) end
Also aliased as: on_numblock, on_itblock
Source
# File lib/rubocop/cop/style/object_then.rb, line 43 def on_send(node) return unless node.arguments.one? && node.first_argument.block_pass_type? check_method_node(node) end
Also aliased as: on_csend
Private Instance Methods
Source
# File lib/rubocop/cop/style/object_then.rb, line 52 def check_method_node(node) if preferred_method?(node) correct_style_detected else opposite_style_detected 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 end
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
Source
# File lib/rubocop/cop/style/object_then.rb, line 66 def preferred_method?(node) node.method?(style) end