Sha256: f1c5cd37c6ccc3c146349264ea94ffcf3eaf894d8d6633dd814f86743af3c0e0

Contents?: true

Size: 1.04 KB

Versions: 7

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module WithModel
  class ConstantStubber
    def initialize(const_name)
      @const_name = const_name.to_sym
      @namespace = nil
      @original_value = nil
    end

    def stub_const(value)
      @namespace = namespace
      if @namespace.const_defined?(basename)
        @original_value = @namespace.const_get(basename)
        @namespace.__send__ :remove_const, basename
      end

      @namespace.const_set basename, value
    end

    def unstub_const
      if @namespace
        @namespace.__send__ :remove_const, basename
        @namespace.const_set basename, @original_value if @original_value
        @namespace = nil
      end
      @original_value = nil
    end

    private

    def namespace
      *namespace_parts, _ = lookup_list
      namespace_parts.reduce(Object) do |ns, ns_part|
        ns.const_get(ns_part.to_sym)
      end
    end

    def lookup_list
      @const_name.to_s.split('::')
    end

    def basename
      @basename ||= lookup_list.last
    end
  end
  private_constant :ConstantStubber
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
with_model-2.1.7 lib/with_model/constant_stubber.rb
with_model-2.1.6 lib/with_model/constant_stubber.rb
with_model-2.1.5 lib/with_model/constant_stubber.rb
with_model-2.1.4 lib/with_model/constant_stubber.rb
with_model-2.1.3 lib/with_model/constant_stubber.rb
with_model-2.1.2 lib/with_model/constant_stubber.rb
with_model-2.1.1 lib/with_model/constant_stubber.rb