module ROM::AutoCurry

Public Class Methods

extended(klass) click to toggle source
Calls superclass method
# File lib/rom/support/auto_curry.rb, line 3
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

Public Instance Methods

auto_curried_methods() click to toggle source
# File lib/rom/support/auto_curry.rb, line 22
def auto_curried_methods
  @__auto_curried_methods__ ||= []
end
auto_curry(name, &block) click to toggle source
Calls superclass method
# File lib/rom/support/auto_curry.rb, line 26
def auto_curry(name, &block)
  arity = instance_method(name).arity

  return if private_instance_methods.include?(name) || arity == 0

  mod = Module.new

  mod.module_eval do
    define_method(name) do |*args|
      response =
        if arity < 0 || arity == args.size
          super(*args)
        else
          self.class.curried.new(self, name: 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
auto_curry_busy?() click to toggle source
# File lib/rom/support/auto_curry.rb, line 18
def auto_curry_busy?
  @__auto_curry_busy__ ||= false
end
auto_curry_guard() { || ... } click to toggle source
# File lib/rom/support/auto_curry.rb, line 11
def auto_curry_guard
  @__auto_curry_busy__ = true
  yield
ensure
  @__auto_curry_busy__ = false
end