Sha256: 1c65e273da300e8624711cb7ad466314d1416644dcab751ab05f98ec5809cdd5

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 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_method?(type, 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

4 entries across 4 versions & 1 rubygems

Version Path
active_mocker-2.6.2 lib/active_mocker/mock_creator/defined_methods.rb
active_mocker-2.6.1.beta2 lib/active_mocker/mock_creator/defined_methods.rb
active_mocker-2.6.1.beta lib/active_mocker/mock_creator/defined_methods.rb
active_mocker-2.6.0 lib/active_mocker/mock_creator/defined_methods.rb