Sha256: 06ced4ed31bee4cc076c623d630ceb77a66a24d46088bd3ad0bf384f20a1eccb

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 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
      @namespace.__send__ :remove_const, basename
      @namespace.const_set basename, @original_value if @original_value
      @namespace = nil
      @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

1 entries across 1 versions & 1 rubygems

Version Path
with_model-2.1.0 lib/with_model/constant_stubber.rb