Sha256: e47143958a12faf8f2111c0162b53b71d2bd6c03681d3bd2ed4f0d4880b59a5c

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
require_relative "safe_methods"
module ActiveMocker
  class MockCreator
    module DefinedMethods
      include SafeMethods
      Method = Struct.new(:name, :arguments, :body)

      def instance_methods
        meths = class_introspector.get_class.public_instance_methods(false).sort
        meths << :initialize if safe_methods[:instance_methods].include?(:initialize)
        meths.map { |m| create_method(m, :instance_method) }
      end

      def class_methods
        class_introspector
          .get_class
          .methods(false)
          .sort
          .map { |m| create_method(m, :method) }
      end

      private

      def create_method(m, type)
        plural_type = (type.to_s + "s").to_sym
        if safe_methods[plural_type].include?(m)
          def_type   = type == :method ? :class_defs : :defs
          def_method = class_introspector.parsed_source.public_send(def_type).detect { |meth| meth.name == m }
          raise "ActiveMocker.safe_methods is unable to find #{plural_type}: #{m}" unless def_method
          Method.new(
            m,
            def_method.arguments,
            def_method.body
          )
        else
          type_symbol = type == :method ? "::" : "#"
          Method.new(
            m,
            ReverseParameters.new(
              class_introspector.get_class.send(type, m).parameters
            ).parameters,
            "__am_raise_not_mocked_error(method: __method__, caller: Kernel.caller, type: '#{type_symbol}')"
          )
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_mocker-2.5.4 lib/active_mocker/mock_creator/defined_methods.rb
active_mocker-2.5.3 lib/active_mocker/mock_creator/defined_methods.rb