Class: Necropsy::ConventionRules

Inherits:
Object
  • Object
show all
Defined in:
lib/necropsy/convention_rules.rb

Defined Under Namespace

Classes: Rule

Constant Summary collapse

BUILT_IN =
[
  Rule.new(
    id: 'rubocop.event_callback', family: :ancestor_scoped_hook,
    ancestor_patterns: ['RuboCop::Cop', 'RuboCop::Cop::Base'], frameworks: ['rubocop']
  ),
  Rule.new(
    id: 'rails.callback_symbol', family: :symbol_argument_method_reference,
    methods: %w[after_action after_save before_action before_save validate],
    owner_patterns: ['*Controller', '*Job', '*Mailer', '*Record', '*Validator'], frameworks: ['rails']
  ),
  Rule.new(
    id: 'rails.application_base', family: :ancestor_scoped_hook,
    methods: %w[after_action after_save before_action before_save validate],
    ancestor_patterns: %w[ApplicationController ApplicationJob ApplicationMailer ApplicationRecord],
    frameworks: ['rails']
  ),
  Rule.new(
    id: 'rails.action_cable', family: :framework_runtime_hook,
    methods: %w[receive subscribed unsubscribed], owner_patterns: ['*Channel'], frameworks: ['rails']
  ),
  Rule.new(
    id: 'rails.active_job', family: :framework_runtime_hook,
    methods: %w[deserialize perform serialize], owner_patterns: ['*Job'], ancestor_patterns: ['ApplicationJob'],
    frameworks: ['rails']
  ),
  Rule.new(
    id: 'sidekiq.worker', family: :framework_runtime_hook,
    methods: ['perform'], ancestor_patterns: %w[Sidekiq::Job Sidekiq::Worker], frameworks: ['sidekiq']
  ),
  Rule.new(
    id: 'graphql.runtime', family: :framework_runtime_hook,
    methods: %w[authorized? ready? resolve subscribed update],
    owner_patterns: %w[*Mutation *Resolver *Subscription *Type],
    ancestor_patterns: %w[GraphQL::Schema::Mutation GraphQL::Schema::Resolver GraphQL::Schema::Subscription],
    frameworks: ['graphql']
  ),
  Rule.new(
    id: 'active_model_serializers.public_surface', family: :declarative_public_surface,
    owner_patterns: ['*Serializer'], frameworks: ['active_model_serializers']
  ),
  Rule.new(
    id: 'blueprinter.public_surface', family: :declarative_public_surface,
    owner_patterns: ['*Blueprint'], frameworks: ['blueprinter']
  ),
  Rule.new(
    id: 'view_component.runtime', family: :framework_runtime_hook,
    methods: %w[before_render call render?], owner_patterns: ['*Component'], frameworks: ['view_component']
  )
].freeze
MAX_RULES =
32

Instance Method Summary collapse

Constructor Details

#initialize(rules: BUILT_IN) ⇒ ConventionRules

Returns a new instance of ConventionRules.



88
89
90
91
# File 'lib/necropsy/convention_rules.rb', line 88

def initialize(rules: BUILT_IN)
  @rules = Array(rules).freeze
  validate_rules!
end

Instance Method Details

#method_hit(owner:, method_name:, ancestors:, frameworks: []) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/necropsy/convention_rules.rb', line 93

def method_hit(owner:, method_name:, ancestors:, frameworks: [])
  candidate = @rules.find do |rule|
    next false unless rule.method_names.empty? || rule.method_names.include?(method_name.to_s)
    next false unless framework_enabled?(rule, frameworks)

    scoped_match?(rule, owner, ancestors)
  end
  hit(candidate, owner: owner, method_name: method_name) if candidate
end