Sha256: bbfccf409513be889534d44751892896db09efcdb254683a4d4e96160e842599

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module ROM
  module AutoCurry
    def self.extended(klass)
      klass.define_singleton_method(:method_added) do |name|
        return if auto_curry_busy?
        auto_curry_guard { auto_curry(name) }
        super(name)
      end
    end

    def auto_curry_guard
      @__auto_curry_busy__ = true
      yield
    ensure
      @__auto_curry_busy__ = false
    end

    def auto_curry_busy?
      @__auto_curry_busy__ ||= false
    end

    def auto_curried_methods
      @__auto_curried_methods__ ||= Set.new
    end

    def auto_curry(name, &block)
      arity = instance_method(name).arity

      return unless public_instance_methods.include?(name) && arity != 0

      mod = Module.new

      mod.module_eval do
        define_method(name) do |*args, &mblock|
          response =
            if arity < 0 || arity == args.size
              super(*args, &mblock)
            else
              self.class.curried.new(self, view: name, curry_args: args, arity: arity)
            end

          if block
            response.instance_exec(&block)
          else
            response
          end
        end
      end

      auto_curried_methods << name

      prepend(mod)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-core-4.0.0.beta3 lib/rom/auto_curry.rb