Sha256: 1d6520f8f9d6218c44dab1b45dc9a222c7920488f0af04c88cea5c44035c90f1

Contents?: true

Size: 896 Bytes

Versions: 1

Compression:

Stored size: 896 Bytes

Contents

module DynaMo
  class OverrideMethod
    attr_reader :context, :name

    def initialize(context, name, &block)
      @context = context.to_sym
      @name = name.to_sym
      @override = block

      @mod = nil
    end

    def applied_module
      return @mod if @mod

      mod = Module.new
      mod.__send__(:define_method, @name, self.to_proc)
      @mod = mod
    end

    def to_proc
      context = @context
      override = @override

      -> (*args) {
        Thread.current[:dynamo_contexts] ||= {}
        Thread.current[:dynamo_stack] ||= []

        if Thread.current[:dynamo_contexts][context]
          Thread.current[:dynamo_stack].push(-> (*args) { super(*args) })
          begin
            instance_exec(*args, &override)
          ensure
            Thread.current[:dynamo_stack].pop
          end
        else
          super(*args)
        end
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dyna_mo-0.0.1 lib/dyna_mo/override_method.rb