Sha256: a3f536d409c8192b6e43be82f2c7ec0e84124e210eadc681a5fb2d404aee46ca

Contents?: true

Size: 999 Bytes

Versions: 15

Compression:

Stored size: 999 Bytes

Contents

module Fakes
  class ClassSwap
    attr_accessor :original,:replacement
    attr_reader :klass

    def initialize(fully_qualified_klass,replacement,options ={})
      klass_parts = fully_qualified_klass.to_s
      parts = klass_parts.split('::')
      the_module = parts.count == 1 ? Object : eval(parts.slice(0,parts.count - 1).join('::'))
      @klass = parts[parts.count - 1].to_s.to_sym

      @replacement = replacement
      @remove_strategy = options.fetch(:remove_strategy,lambda{|klass_symbol| the_module.send(:remove_const,klass_symbol)})
      @set_strategy = options.fetch(:set_strategy,lambda{|klass_symbol,new_value| the_module.const_set(klass_symbol,new_value)})
    end

    def initiate
      swap_to(replacement){|original| @original = original}
    end

    def reset
      swap_to(@original)
    end

    def swap_to(new_value,&block)
      current = @remove_strategy.call(@klass)
      yield current if block_given?
      @set_strategy.call(@klass,new_value)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
fakes-1.1.3 lib/fakes/class_swap.rb
fakes-1.1.2 lib/fakes/class_swap.rb
fakes-1.0.31 lib/core/class_swap.rb
fakes-1.0.30 lib/core/class_swap.rb
fakes-1.0.25 lib/core/class_swap.rb
fakes-1.1.1 lib/fakes/class_swap.rb
fakes-1.0.24 lib/core/class_swap.rb
fakes-1.0.22 lib/core/class_swap.rb
fakes-1.1.0 lib/fakes/class_swap.rb
fakes-1.0.9 lib/fakes/class_swap.rb
fakes-1.0.8 lib/fakes/class_swap.rb
fakes-1.0.7 lib/fakes/class_swap.rb
fakes-1.0.6 lib/fakes/class_swap.rb
fakes-1.0.5 lib/core/class_swap.rb
fakes-1.0.2 lib/core/class_swap.rb