class Ducalis::ModuleLikeClass

Constants

OFFENSE

Public Instance Methods

on_class(node) click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 13
def on_class(node)
  _name, inheritance, body = *node
  return if !inheritance.nil? || body.nil? || allowed_include?(body)

  matched = matched_args(body)
  return if matched.empty?

  add_offense(node, :expression,
              format(OFFENSE, args:
                  matched.map { |arg| "`#{arg}`" }.join(', ')))
end

Private Instance Methods

all_includes(body) click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 44
def all_includes(body)
  children(body).select(&method(:include_node?))
                .map(&:to_a)
                .map { |_, _, node| node.loc.expression.source }
                .to_a
end
allowed_include?(body) click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 27
def allowed_include?(body)
  return if cop_config['AllowedIncludes'].to_a.empty?

  (all_includes(body) & cop_config['AllowedIncludes']).any?
end
children(body) click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 40
def children(body)
  (body.type != :begin ? s(:begin, body) : body).children
end
matched_args(body) click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 33
def matched_args(body)
  methods_defintions = children(body).select(&public_method_definition?)
  return [] if methods_defintions.count == 1 && with_initialize?(body)

  methods_defintions.map(&method_args).inject(&:&).to_a
end
method_args() click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 55
def method_args
  lambda do |n|
    _name, args = *n
    args.children
        .select { |node| node.type == :arg }
        .map    { |node| node.loc.expression.source }
  end
end
public_method_definition?() click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 51
def public_method_definition?
  ->(node) { node.type == :def && !non_public?(node) && !initialize?(node) }
end
with_initialize?(body) click to toggle source
# File lib/ducalis/cops/module_like_class.rb, line 64
def with_initialize?(body)
  children(body).find(&method(:initialize?))
end