Class: Spy::Nest
- Inherits:
-
Object
- Object
- Spy::Nest
- Defined in:
- lib/spy/nest.rb
Overview
This class manages all the Constant Mutations for a given Module
Instance Attribute Summary collapse
-
#base_module ⇒ Module
readonly
The module that the Nest is managing.
-
#constant_spies ⇒ Hash<Symbol, Constant>
readonly
The module that the Nest is managing.
Class Method Summary collapse
-
.all ⇒ Hash<String, Constant>
returns all the hooked constants.
-
.fetch(base_module) ⇒ Nest
retrieves the nest for a given module or creates it.
-
.get(base_module) ⇒ Nil, Nest
retrieves the nest for a given module.
Instance Method Summary collapse
-
#add(spy) ⇒ self
records that the spy is hooked.
-
#get(constant_name) ⇒ Constant?
returns a spy if the constant was added.
-
#hooked?(constant_name) ⇒ Boolean
checks to see if a given constant is hooked.
-
#hooked_constants ⇒ Array
list all the constants that are being stubbed.
-
#initialize(base_module) ⇒ Nest
constructor
A new instance of Nest.
-
#remove(spy) ⇒ self
removes the spy from the records.
Constructor Details
#initialize(base_module) ⇒ Nest
Returns a new instance of Nest.
14 15 16 17 18 |
# File 'lib/spy/nest.rb', line 14 def initialize(base_module) raise ArgumentError, "#{base_module} is not a kind of Module" unless base_module.is_a?(Module) @base_module = base_module @constant_spies = {} end |
Instance Attribute Details
#base_module ⇒ Module (readonly)
Returns The module that the Nest is managing.
|
# File 'lib/spy/nest.rb', line 5
|
#constant_spies ⇒ Hash<Symbol, Constant> (readonly)
Returns The module that the Nest is managing.
|
# File 'lib/spy/nest.rb', line 5
|
Class Method Details
.all ⇒ Hash<String, Constant>
returns all the hooked constants
81 82 83 |
# File 'lib/spy/nest.rb', line 81 def all @all ||= {} end |
.fetch(base_module) ⇒ Nest
retrieves the nest for a given module or creates it
75 76 77 |
# File 'lib/spy/nest.rb', line 75 def fetch(base_module) all[base_module.name] ||= self.new(base_module) end |
.get(base_module) ⇒ Nil, Nest
retrieves the nest for a given module
68 69 70 |
# File 'lib/spy/nest.rb', line 68 def get(base_module) all[base_module.name] end |
Instance Method Details
#add(spy) ⇒ self
records that the spy is hooked
23 24 25 26 27 28 29 30 |
# File 'lib/spy/nest.rb', line 23 def add(spy) if @constant_spies[spy.constant_name] raise AlreadyStubbedError, "#{spy.constant_name} has already been stubbed" else @constant_spies[spy.constant_name] = spy end self end |
#get(constant_name) ⇒ Constant?
returns a spy if the constant was added
47 48 49 |
# File 'lib/spy/nest.rb', line 47 def get(constant_name) @constant_spies[constant_name] end |
#hooked?(constant_name) ⇒ Boolean
checks to see if a given constant is hooked
54 55 56 |
# File 'lib/spy/nest.rb', line 54 def hooked?(constant_name) !!get(constant_name) end |
#hooked_constants ⇒ Array
list all the constants that are being stubbed
60 61 62 |
# File 'lib/spy/nest.rb', line 60 def hooked_constants @constant_spies.keys end |
#remove(spy) ⇒ self
removes the spy from the records
35 36 37 38 39 40 41 42 |
# File 'lib/spy/nest.rb', line 35 def remove(spy) if @constant_spies[spy.constant_name] == spy @constant_spies.delete(spy.constant_name) else raise NoSpyError, "#{spy.constant_name} was not stubbed on #{base_module.name}" end self end |