Sha256: f99050ef87344a7084e484e6ae60a4c4705c704eaa87a82b84b125487c2b88a2

Contents?: true

Size: 729 Bytes

Versions: 65

Compression:

Stored size: 729 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

65 entries across 65 versions & 1 rubygems

Version Path
active_scaffold-3.7.8 test/const_mocker.rb
active_scaffold-3.7.7 test/const_mocker.rb
active_scaffold-3.7.6 test/const_mocker.rb
active_scaffold-3.7.5 test/const_mocker.rb
active_scaffold-3.7.2 test/const_mocker.rb
active_scaffold-3.7.1 test/const_mocker.rb
active_scaffold-3.7.0 test/const_mocker.rb
active_scaffold-3.6.20 test/const_mocker.rb
active_scaffold-3.6.19 test/const_mocker.rb
active_scaffold-3.6.17 test/const_mocker.rb
active_scaffold-3.6.15 test/const_mocker.rb
active_scaffold-3.6.14 test/const_mocker.rb
active_scaffold-3.6.13 test/const_mocker.rb
active_scaffold-3.6.12 test/const_mocker.rb
active_scaffold-3.6.11.1 test/const_mocker.rb
active_scaffold-3.6.11 test/const_mocker.rb
active_scaffold-3.6.10 test/const_mocker.rb
active_scaffold-3.6.9 test/const_mocker.rb
active_scaffold-3.6.8 test/const_mocker.rb
active_scaffold-3.6.6 test/const_mocker.rb