Module: ROM::AutoCurry

Defined in:
lib/rom/support/auto_curry.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



3
4
5
6
7
8
9
# 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

Instance Method Details

#auto_curry(name, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rom/support/auto_curry.rb', line 22

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

  define_method(name) do |*args|
    response =
      if arity < 0 || arity == args.size
        meth.bind(self).(*args)
      else
        curried.new(self, name: name, curry_args: args, arity: arity)
      end

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

#auto_curry_busy?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/rom/support/auto_curry.rb', line 18

def auto_curry_busy?
  @__auto_curry_busy__ ||= false
end

#auto_curry_guardObject



11
12
13
14
15
16
# File 'lib/rom/support/auto_curry.rb', line 11

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