Sha256: 2533d63a457ef9dba902365641bf784539a87d8d9e9730ef7044d5cd3ba7bd03

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

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

module SilentConstants
  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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
protobuf-1.4.2 spec/helper/silent_constants.rb
protobuf-1.4.1 spec/helper/silent_constants.rb
protobuf-1.4.0 spec/helper/silent_constants.rb
protobuf-1.3.0 spec/helper/silent_constants.rb
protobuf-1.1.3 spec/helper/silent_constants.rb
protobuf-1.1.2 spec/helper/silent_constants.rb
protobuf-1.1.1 spec/helper/silent_constants.rb
protobuf-1.1.0.beta2 spec/helper/silent_constants.rb
protobuf-1.1.0.beta1 spec/helper/silent_constants.rb
protobuf-1.1.0.beta0 spec/helper/silent_constants.rb