Module: RailsBestPractices::Core::Check::Exceptable

Defined in:
lib/rails_best_practices/core/check.rb

Overview

Helper to check except methods.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/rails_best_practices/core/check.rb', line 358

def self.included(base)
  base.class_eval do
    def except_methods
      @except_methods + internal_except_methods
    end

    # check if the method is in the except methods list.
    def excepted?(method)
      is_ignored?(method.file) ||
        except_methods.any? { |except_method| Exceptable.matches method, except_method }
    end

    def internal_except_methods
      raise NoMethodError, 'no method internal_except_methods'
    end
  end
end

.matches(method, except_method) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/rails_best_practices/core/check.rb', line 376

def self.matches(method, except_method)
  class_name, method_name = except_method.split('#')

  method_name = '.*' if method_name == '*'
  method_expression = Regexp.new method_name
  matched = method.method_name =~ method_expression

  if matched
    class_name = '.*' if class_name == '*'
    class_expression = Regexp.new class_name

    class_names =
      Prepares.klasses.select { |klass| klass.class_name == method.class_name }.map(&:extend_class_name).compact

    class_names.unshift method.class_name
    matched = class_names.any? { |name| name =~ class_expression }
  end

  !!matched
end