Sha256: 1757134e2ea1a76b2f82982e92f63bae2c514e4d7a2966ae780b6339a111d9e6

Contents?: true

Size: 737 Bytes

Versions: 18

Compression:

Stored size: 737 Bytes

Contents

class ConstMocker
  def initialize(const_name, parent = Object)
    @parent = parent
    @const_name = const_name
    @const_state = nil
    @const_state = @parent.const_defined?(@const_name) ? @parent.const_get(@const_name) : nil
  end
  
  def remove
    @parent.send :remove_const, @const_name if @parent.const_defined?(@const_name)
  end
  
  def declare
    @parent.const_set @const_name, Class.new
  end
  
  def restore
    remove
    @parent.const_set @const_name, @const_state if @const_state
  end

  def const
    @parent.const_get @const_name if @parent.const_defined?(@const_name)
  end
  
  def self.mock(const_name, parent = Object, &block)
    cm = new(const_name, parent)
    yield(cm)
    cm.restore
    true
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
active_scaffold-3.4.17 test/const_mocker.rb
active_scaffold-3.4.16 test/const_mocker.rb
active_scaffold-3.4.14 test/const_mocker.rb
active_scaffold-3.4.13 test/const_mocker.rb
active_scaffold-3.4.12 test/const_mocker.rb
active_scaffold-3.4.11 test/const_mocker.rb
active_scaffold-3.4.10 test/const_mocker.rb
active_scaffold-3.4.9 test/const_mocker.rb
active_scaffold-3.4.8 test/const_mocker.rb
active_scaffold-3.4.7 test/const_mocker.rb
active_scaffold-3.4.5 test/const_mocker.rb
active_scaffold-3.4.4 test/const_mocker.rb
active_scaffold-3.4.3 test/const_mocker.rb
active_scaffold-3.4.2 test/const_mocker.rb
active_scaffold-3.4.1 test/const_mocker.rb
active_scaffold-3.4.0.1 test/const_mocker.rb
active_scaffold-3.4.0 test/const_mocker.rb
active_scaffold-3.3.3 test/const_mocker.rb