class Spy::Constant

Attributes

base_module[R]

@!attribute [r] base_module

@return [Module] the module that is being watched

@!attribute [r] constant_name

@return [Symbol] the name of the constant that is/will be stubbed

@!attribute [r] original_value

@return [Object] the original value that was set when it was hooked
constant_name[R]

@!attribute [r] base_module

@return [Module] the module that is being watched

@!attribute [r] constant_name

@return [Symbol] the name of the constant that is/will be stubbed

@!attribute [r] original_value

@return [Object] the original value that was set when it was hooked
original_value[R]

@!attribute [r] base_module

@return [Module] the module that is being watched

@!attribute [r] constant_name

@return [Symbol] the name of the constant that is/will be stubbed

@!attribute [r] original_value

@return [Object] the original value that was set when it was hooked

Public Class Methods

get(base_module, constant_name) click to toggle source

retrieves the spy for given constnat and module or returns nil @return [Nil, Constant]

# File lib/spy/constant.rb, line 117
def get(base_module, constant_name)
  nest = Nest.get(base_module)
  if nest
    nest.get(constant_name)
  end
end
new(base_module, constant_name) click to toggle source

@param base_module [Module] the module this spy should be on @param constant_name [Symbol] the constant this spy is watching

# File lib/spy/constant.rb, line 18
def initialize(base_module, constant_name)
  raise ArgumentError, "#{base_module.inspect} is not a kind of Module" unless base_module.is_a? Module
  raise ArgumentError, "#{constant_name.inspect} is not a kind of Symbol" unless constant_name.is_a? Symbol
  @base_module, @constant_name = base_module, constant_name.to_sym
  @original_value = @new_value = @previously_defined = nil
end
off(base_module, constant_name) click to toggle source

retrieves the spy for given constant and module and unhooks the constant from the module @return [Constant]

# File lib/spy/constant.rb, line 109
def off(base_module, constant_name)
  spy = get(base_module, constant_name)
  raise NoSpyError, "#{constant_name} was not spied on #{base_module}" unless spy
  spy.unhook
end
on(base_module, constant_name) click to toggle source

finds existing spy or creates a new constant spy and hooks the constant @return [Constant]

# File lib/spy/constant.rb, line 102
def on(base_module, constant_name)
  new(base_module, constant_name).hook
end

Public Instance Methods

and_hide() click to toggle source

unsets the constant @return [self]

# File lib/spy/constant.rb, line 60
def and_hide
  base_module.send(:remove_const, constant_name) if currently_defined?
  self
end
and_return(value) click to toggle source

sets the constant to the requested value @param value [Object] @return [self]

# File lib/spy/constant.rb, line 68
def and_return(value)
  @new_value = value
  and_hide
  base_module.const_set(constant_name, @new_value)
  self
end
currently_defined?() click to toggle source

checks to see if the constant is currently defined? @return [Boolean]

# File lib/spy/constant.rb, line 89
def currently_defined?
  base_module.const_defined?(constant_name, false)
end
hidden?() click to toggle source

checks to see if the constant is hidden? @return [Boolean]

# File lib/spy/constant.rb, line 83
def hidden?
  hooked? && currently_defined?
end
hook(opts = {}) click to toggle source

stashes the original constant then overwrites it with nil @param opts [Hash{force => false}] set :force => true if you want it to ignore if the constant exists @return [self]

# File lib/spy/constant.rb, line 33
def hook(opts = {})
  opts[:force] ||= false
  Nest.fetch(base_module).add(self)
  Agency.instance.recruit(self)

  @previously_defined = currently_defined?
  if previously_defined? || !opts[:force]
    @original_value = base_module.const_get(constant_name, false)
  end
  and_return(@new_value)
  self
end
hooked?() click to toggle source

checks to see if this spy is hooked? @return [Boolean]

# File lib/spy/constant.rb, line 77
def hooked?
  self.class.get(base_module, constant_name) == self
end
name() click to toggle source

full name of spied constant

# File lib/spy/constant.rb, line 26
def name
  "#{base_module.name}::#{constant_name}"
end
previously_defined?() click to toggle source

checks to see if the constant is previously defined? @return [Boolean]

# File lib/spy/constant.rb, line 95
def previously_defined?
  @previously_defined
end
unhook() click to toggle source

restores the original value of the constant or unsets it if it was unset @return [self]

# File lib/spy/constant.rb, line 48
def unhook
  Nest.get(base_module).remove(self)
  Agency.instance.retire(self)

  and_return(@original_value) if previously_defined?

  @original_value = @previously_defined = nil
  self
end