Sha256: 23f016ac07ac85efa32305f09826f66ad281186fe6908f7f654d9d0ad3ed27dd

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require 'singleton'

module Spy
  class Agency
    include Singleton

    attr_reader :subroutines, :constants, :doubles

    def initialize
      clear!
    end

    def recruit(spy)
      case spy
      when Subroutine
        subroutines << spy
      when Constant
        constants << spy
      when Double
        doubles << spy
      else
        raise "Not a spy"
      end
      spy
    end

    def retire(spy)
      case spy
      when Subroutine
        subroutines.delete(spy)
      when Constant
        constants.delete(spy)
      when Double
        doubles.delete(spy)
      else
        raise "Not a spy"
      end
      spy
    end

    def active?(spy)
      case spy
      when Subroutine
        subroutines.include?(spy)
      when Constant
        constants.include?(spy)
      when Double
        doubles.include?(spy)
      end
    end

    def dissolve!
      subroutines.each(&:unhook)
      constants.each(&:unhook)
      clear!
    end

    def clear!
      @subroutines = []
      @constants = []
      @doubles = []
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spy-0.1.0 lib/spy/agency.rb