Class: Mocktail::TopShelf

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mocktail/value/top_shelf.rb,
lib/mocktail/sorbet/mocktail/value/top_shelf.rb

Overview

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)

Constant Summary collapse

@@type_replacements =
T.let({}, T::Hash[T.any(Module, T::Class[T.anything]), TypeReplacement])
@@type_replacements_mutex =
T.let(Mutex.new, Mutex)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTopShelf

Returns a new instance of TopShelf.



15
16
17
18
19
# File 'lib/mocktail/value/top_shelf.rb', line 15

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

Class Method Details

.instanceObject



8
9
10
# File 'lib/mocktail/value/top_shelf.rb', line 8

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

Instance Method Details

#new_replaced?(type) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mocktail/value/top_shelf.rb', line 41

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

#of_next_registered?(type) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mocktail/value/top_shelf.rb', line 49

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

#register_new_replacement!(type) ⇒ Object



37
38
39
# File 'lib/mocktail/value/top_shelf.rb', line 37

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

#register_of_next_replacement!(type) ⇒ Object



45
46
47
# File 'lib/mocktail/value/top_shelf.rb', line 45

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

#register_singleton_method_replacement!(type) ⇒ Object



57
58
59
# File 'lib/mocktail/value/top_shelf.rb', line 57

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

#reset_current_thread!Object



33
34
35
# File 'lib/mocktail/value/top_shelf.rb', line 33

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

#singleton_methods_replaced?(type) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/mocktail/value/top_shelf.rb', line 61

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

#type_replacement_for(type) ⇒ Object



21
22
23
24
25
# File 'lib/mocktail/value/top_shelf.rb', line 21

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

#type_replacement_if_exists_for(type) ⇒ Object



27
28
29
30
31
# File 'lib/mocktail/value/top_shelf.rb', line 27

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

#unregister_of_next_replacement!(type) ⇒ Object



53
54
55
# File 'lib/mocktail/value/top_shelf.rb', line 53

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