Sha256: 5f99c3c1013199291e75ae6163230f83b65af225fa2baa5e8bc83052e43bc6ca

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module Spy
  # This class manages all the Constant Mutations for a given Module
  class Nest

    # @!attribute [r] base_module
    #   @return [Module] The module that the Nest is managing
    #
    # @!attribute [r] hooked_constants
    #   @return [Hash<Symbol, Constant>] The module that the Nest is managing


    attr_reader :base_module, :hooked_constants

    def initialize(base_module)
      raise "#{base_module} is not a kind of Module" unless base_module.is_a?(Module)
      @base_module = base_module
      @hooked_constants = {}
    end

    # records that the spy is hooked
    # @param spy [Constant]
    # @return [self]
    def add(spy)
      if @hooked_constants[spy.constant_name]
        raise "#{spy.constant_name} has already been stubbed"
      else
        @hooked_constants[spy.constant_name] = spy
      end
      self
    end

    # removes the spy from the records
    # @param spy [Constant]
    # @return [self]
    def remove(spy)
      if @hooked_constants[spy.constant_name] == spy
        @hooked_constants.delete(spy.constant_name)
      else
        raise "#{spy.constant_name} was never added"
      end
      self
    end

    # checks to see if a given constant is hooked
    # @param constant_name [Symbol]
    # @return [Boolean]
    def hooked?(constant_name)
      !!@hooked_constants[constant_name]
    end

    class << self
      # retrieves the nest for a given module
      # @param base_module [Module]
      # @return [Nil, Nest]
      def get(base_module)
        all[base_module.name]
      end


      # retrieves the nest for a given module or creates it
      # @param base_module [Module]
      # @return [Nest]
      def fetch(base_module)
        all[base_module.name] ||= self.new(base_module)
      end

      # returns all the hooked constants
      # @return [Hash<String, Constant>]
      def all
        @all ||= {}
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spy-0.2.4 lib/spy/nest.rb
spy-0.2.3 lib/spy/nest.rb
spy-0.2.2 lib/spy/nest.rb