Sha256: 1034f4c0cab6a8442e97f93477733b126b7b1a69cc5444fb85c81d95252a9925

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Mocktail
  # The TopShelf is where we keep all the more global, dangerous state.
  # In particular, this is where Mocktail manages state related to singleton
  # method replacements carried out with Mocktail.replace(ClassOrModule)
  class TopShelf
    extend T::Sig

    def self.instance
      Thread.current[:mocktail_top_shelf] ||= new
    end

    @@type_replacements = {}
    @@type_replacements_mutex = Mutex.new

    def initialize
      @new_registrations = []
      @of_next_registrations = []
      @singleton_method_registrations = []
    end

    def type_replacement_for(type)
      @@type_replacements_mutex.synchronize {
        @@type_replacements[type] ||= TypeReplacement.new(type: type)
      }
    end

    def type_replacement_if_exists_for(type)
      @@type_replacements_mutex.synchronize {
        @@type_replacements[type]
      }
    end

    def reset_current_thread!
      Thread.current[:mocktail_top_shelf] = self.class.new
    end

    def register_new_replacement!(type)
      @new_registrations |= [type]
    end

    def new_replaced?(type)
      @new_registrations.include?(type)
    end

    def register_of_next_replacement!(type)
      @of_next_registrations |= [type]
    end

    def of_next_registered?(type)
      @of_next_registrations.include?(type)
    end

    def unregister_of_next_replacement!(type)
      @of_next_registrations -= [type]
    end

    def register_singleton_method_replacement!(type)
      @singleton_method_registrations |= [type]
    end

    def singleton_methods_replaced?(type)
      @singleton_method_registrations.include?(type)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mocktail-2.0.0 lib/mocktail/value/top_shelf.rb