Sha256: 8a6ef4d93a848fc8b75b0677127193e36bdd9ce0880aee6bfd6f7d0096141049

Contents?: true

Size: 965 Bytes

Versions: 8

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

module DeepCover
  # Helps redefine methods in overriden_modules.
  # For each methods in Mod, this defines `<method>_with{out}_deep_cover`.
  # Set `active` to true or false to alias <method> to one or the other.
  module ModuleOverride
    attr_reader :overriden_modules

    def active=(active)
      each do |mod, method_name|
        mod.send :alias_method, method_name, :"#{method_name}_#{active ? 'with' : 'without'}_deep_cover"
      end
    end

    def override(*modules)
      @overriden_modules = modules
      each do |mod, method_name|
        mod.send :alias_method, :"#{method_name}_without_deep_cover", method_name
        mod.send :define_method, :"#{method_name}_with_deep_cover", instance_method(method_name)
      end
    end

    def each(&block)
      overriden_modules.each do |mod|
        instance_methods(false).each do |method_name|
          yield mod, method_name
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
deep-cover-0.5.2 lib/deep_cover/module_override.rb
deep-cover-0.5.1 lib/deep_cover/module_override.rb
deep-cover-0.5.0 lib/deep_cover/module_override.rb
deep-cover-0.4.0 lib/deep_cover/module_override.rb
deep-cover-0.3.0 lib/deep_cover/module_override.rb
deep-cover-0.2.0 lib/deep_cover/module_override.rb
deep-cover-0.1.16 lib/deep_cover/module_override.rb
deep-cover-0.1.15 lib/deep_cover/module_override.rb