class Spy::Nest

This class manages all the Constant Mutations for a given Module

Attributes

base_module[R]

@!attribute [r] base_module

@return [Module] The module that the Nest is managing

@!attribute [r] constant_spies

@return [Hash<Symbol, Constant>] The module that the Nest is managing

Public Class Methods

all() click to toggle source

returns all the hooked constants @return [Hash<String, Constant>]

# File lib/spy/nest.rb, line 81
def all
  @all ||= {}
end
fetch(base_module) click to toggle source

retrieves the nest for a given module or creates it @param base_module [Module] @return [Nest]

# File lib/spy/nest.rb, line 75
def fetch(base_module)
  all[base_module.name] ||= self.new(base_module)
end
get(base_module) click to toggle source

retrieves the nest for a given module @param base_module [Module] @return [Nil, Nest]

# File lib/spy/nest.rb, line 68
def get(base_module)
  all[base_module.name]
end
new(base_module) click to toggle source
# 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

Public Instance Methods

add(spy) click to toggle source

records that the spy is hooked @param spy [Constant] @return [self]

# 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) click to toggle source

returns a spy if the constant was added @param constant_name [Symbol] @return [Constant, nil]

# File lib/spy/nest.rb, line 47
def get(constant_name)
  @constant_spies[constant_name]
end
hooked?(constant_name) click to toggle source

checks to see if a given constant is hooked @param constant_name [Symbol] @return [Boolean]

# File lib/spy/nest.rb, line 54
def hooked?(constant_name)
  !!get(constant_name)
end
hooked_constants() click to toggle source

list all the constants that are being stubbed @return [Array]

# File lib/spy/nest.rb, line 60
def hooked_constants
  @constant_spies.keys
end
remove(spy) click to toggle source

removes the spy from the records @param spy [Constant] @return [self]

# 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