Sha256: 3a883c7b05288505ae8cd3ffe9208050763c9290b77400f28d1868a6e793e355

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/patches/module/common_concern'

module EacRubyUtils
  # Support to abstract methods.
  #
  # Usage:
  #
  #   require 'eac_ruby_utils/abstract_methods'
  #
  #   class BaseClass
  #     include EacRubyUtils::AbstractMethods
  #
  #     abstract_methods :mymethod
  #   end
  #
  #   BaseClass.new.mymethod # raise "Abstract method: mymethod"
  #
  #   class SubClass
  #     def mymethod
  #       "Implemented"
  #     end
  #   end
  #
  #   SubClass.new.mymethod # return "Implemented"
  module AbstractMethods
    common_concern

    module ClassMethods
      def abstract_methods(*methods_names)
        @abstract_methods ||= Set.new
        @abstract_methods += methods_names.map(&:to_sym)

        @abstract_methods.to_enum
      end
    end

    module InstanceMethods
      def respond_to_missing?(method_name, include_private = false)
        super || abstract_method?(method_name)
      end

      def method_missing(method_name, *arguments, &block)
        raise_abstract_method(method_name) if abstract_method?(method_name)

        super
      end

      def abstract_method?(method_name)
        self.class.abstract_methods.include?(method_name.to_sym)
      end

      def raise_abstract_method(method_name)
        raise ::NoMethodError, "Abstract method \"#{method_name}\" hit in \"#{self}\""
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
avm-tools-0.77.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/abstract_methods.rb
eac_ruby_utils-0.52.0 lib/eac_ruby_utils/abstract_methods.rb