Sha256: be4a5f313955d80056af96b47ab96cbf2e0a42ceef95da2217b08fa8736d7e12

Contents?: true

Size: 1.77 KB

Versions: 23

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Utils
    module Module
      class HasOwnInstanceMethod < Support::Command
        ##
        # @!attribute [r] mod
        #   @return [Class, Module]
        #
        attr_reader :mod

        ##
        # @!attribute [r] method_name
        #   @return [String, Symbol]
        #
        attr_reader :method_name

        ##
        # @!attribute [r] private
        #   @return [Boolean]
        #
        attr_reader :private

        ##
        # @param mod [Class, Module]
        # @param method_name [String, Symbol]
        # @param private [Boolean]
        # @return [void]
        #
        def initialize(mod, method_name, private: false)
          @mod = mod
          @method_name = method_name
          @private = private
        end

        ##
        # @return [Boolean]
        #
        def call
          method = method_name.to_sym

          return true if own_instance_methods.include?(method)

          if private
            return true if private_own_instance_methods.include?(method)
          end

          false
        end

        private

        ##
        # @return [Array<Symbol>]
        #
        # @internal
        #   NOTE: `instance_methods` returns both public and protected methods.
        #   NOTE: `instance_methods(false)` returns only own methods.
        #
        def own_instance_methods
          @own_instance_methods ||= mod.instance_methods(false)
        end

        ##
        # @return [Array<Symbol>]
        #
        # @internal
        #   NOTE: `private_instance_methods(false)` returns only own methods.
        #
        def private_own_instance_methods
          @private_own_instance_methods ||= mod.private_instance_methods(false)
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
convenient_service-0.2.1 lib/convenient_service/utils/module/has_own_instance_method.rb
convenient_service-0.2.0 lib/convenient_service/utils/module/has_own_instance_method.rb
convenient_service-0.1.0 lib/convenient_service/utils/module/has_own_instance_method.rb