Sha256: c926471f887ba728ea631ed4d994a241f27e970d6e856572bdaccf1e0b5065fa

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 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)
          raise "ActiveMocker.safe_methods(class_methods: []) is currently unsupported." if type == :method
          def_method = class_introspector.parsed_source.defs.detect { |meth| meth.name == m }
          Method.new(
            m,
            def_method.arguments,
            def_method.body
          )
        else
          Method.new(
            m,
            ReverseParameters.new(
              class_introspector.get_class.send(type, m).parameters,
              blocks_as_values: true
            ).parameters,
            "call_mock_method(method: __method__, caller: Kernel.caller, arguments: [])"
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_mocker-2.5.0 lib/active_mocker/mock_creator/defined_methods.rb