Class: Spy::Nest

Inherits:
Object
  • Object
show all
Defined in:
lib/spy/nest.rb

Overview

This class manages all the Constant Mutations for a given Module

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_module) ⇒ Nest

Returns a new instance of Nest.

Raises:

  • (ArgumentError)


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_moduleModule (readonly)

Returns The module that the Nest is managing.

Returns:

  • (Module)

    The module that the Nest is managing



# File 'lib/spy/nest.rb', line 5


#constant_spiesHash<Symbol, Constant> (readonly)

Returns The module that the Nest is managing.

Returns:

  • (Hash<Symbol, Constant>)

    The module that the Nest is managing



# File 'lib/spy/nest.rb', line 5


Class Method Details

.allHash<String, Constant>

returns all the hooked constants

Returns:



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

Parameters:

  • base_module (Module)

Returns:



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

Parameters:

  • base_module (Module)

Returns:



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

Parameters:

Returns:

  • (self)


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

Parameters:

  • constant_name (Symbol)

Returns:



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

Parameters:

  • constant_name (Symbol)

Returns:

  • (Boolean)


54
55
56
# File 'lib/spy/nest.rb', line 54

def hooked?(constant_name)
  !!get(constant_name)
end

#hooked_constantsArray

list all the constants that are being stubbed

Returns:

  • (Array)


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

Parameters:

Returns:

  • (self)


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