Sha256: 6045871fa6de30fb7f3792ba1e448174e21f75c69d91b2dd778c41ea71f70aec

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

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

      def scope_methods
        class_introspector.class_macros.select { |h| h.keys.first == :scope }.map do |h|
          name, args = h.values.first.first
          arguments  = ReverseParameters.new(args)
          body       = scope_body(arguments, name)
          Method.new(name, arguments, body)
        end
      end

      def scope_body(arguments, name)
        if safe_methods[:scopes].include?(name)
          find_scope_body_from_ast(name)
        else
          "#{class_name}.send(:call_mock_method, " \
                           "method: '#{name}', " \
                           "caller: Kernel.caller, " \
                           "arguments: [#{arguments.arguments}])"
        end
      end

      def ast_scopes
        @ast_scopes ||= class_introspector
                          .parsed_source
                          .class_begin
                          .children
                          .select { |n| n.try(:type) == :send && n.try { children[1] == :scope } }
      end

      def find_scope_body_from_ast(name)
        scope = ast_scopes.detect { |n| n.children[2].children.first == name }
        DissociatedIntrospection::RubyCode.build_from_ast(scope.children[3].children[2]).source
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_mocker-2.5.1 lib/active_mocker/mock_creator/scopes.rb
active_mocker-2.5.1.pre lib/active_mocker/mock_creator/scopes.rb