Sha256: f89dfcbd3a4f203aeb9e8f83e13afeea0f90a974db3502c01d218b6119624201

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# Just a way to keep warnings from being flagged in rename of constants during tests
=begin
module Kernel
  def silence_warnings
    orig_verbosity = $VERBOSE
    $VERBOSE = nil
    yield
    $VERBOSE = orig_verbosity
  end
end
=end

module SilentConstants
=begin
  def parse(constant)
    source, _, constant_name = constant.to_s.rpartition('::')
    [Object.const_get(source.to_sym), constant_name.to_sym]
  end

  # Examples
  # with_constants "Something" => "nothing" do 
  # end
  #
  # with_constants "Something::Foo" => "something else" do 
  # end
  #
  def with_constants(constants, &block)
    saved_constants = {}
    constants.each do |constant, val|
      source_object, const_name = parse(constant)
      saved_constants[constant] = source_object.const_get(const_name)
      Kernel::silence_warnings { source_object.const_set(const_name, val) }
    end

    block.call
  ensure
    constants.each do |constant, val|
      source_object, const_name = parse(constant)
      Kernel::silence_warnings { source_object.const_set(const_name, saved_constants[constant]) }
    end
  end

=end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
protobuf-2.0.0.rc2 spec/support/silent_constants.rb
protobuf-2.0.0.rc1 spec/support/silent_constants.rb