module Wallaby::Baseable

Abstract related class methods

Constants

ATTR_NAME
SUFFIX

Public Class Methods

guess_associated_class_of(class_name, attr_name: ATTR_NAME, suffix: EMPTY_STRING, plural: false) click to toggle source

@param class_name [String] @param attr_name [String] @param suffix [String] @param plural [String] @return [Class] found associated class @raise [Wallaby::ClassNotFound] if associated class isn't found

# File lib/concerns/wallaby/baseable.rb, line 15
    def self.guess_associated_class_of(class_name, attr_name: ATTR_NAME, suffix: EMPTY_STRING, plural: false)
      base_name = class_name.gsub(SUFFIX, EMPTY_STRING).try(plural ? :pluralize : :singularize) << suffix
      parts = base_name.split(COLONS)
      parts.each_with_index do |_, index|
        begin
          # NOTE: DO NOT try to use const_defined? and const_get EVER.
          # This is Rails, use constantize
          return parts[index..-1].join(COLONS).constantize
        rescue NameError # rubocop:disable Lint/SuppressedException
        end
      end

      raise ClassNotFound, <<~INSTRUCTION
        The `#{attr_name}` hasn't been provided for Class `#{class_name}` and Wallaby cannot guess it right.
        If `#{class_name}` is supposed to be a base class, add the following line to its class declaration:

          class #{class_name}
            base_class!
          end

        Otherwise, please specify the `#{attr_name}` in `#{class_name}`'s declaration as follows:

          class #{class_name}
            self.#{attr_name} = CorrectClass
          end
      INSTRUCTION
    end