Module: ROM::AutoCurry Private

Included in:
Relation
Defined in:
core/lib/rom/auto_curry.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Relation extension which provides auto-currying of relation view methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



8
9
10
11
12
13
14
15
# File 'core/lib/rom/auto_curry.rb', line 8

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_curried_methodsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
# File 'core/lib/rom/auto_curry.rb', line 31

def auto_curried_methods
  @__auto_curried_methods__ ||= Set.new
end

#auto_curry(name, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Auto-curry a method

Parameters:

  • name (Symbol)

    The name of a method



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'core/lib/rom/auto_curry.rb', line 40

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
    ruby2_keywords(name) if respond_to?(:ruby2_keywords, true)
  end

  auto_curried_methods << name

  prepend(mod)
end

#auto_curry_busy?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


26
27
28
# File 'core/lib/rom/auto_curry.rb', line 26

def auto_curry_busy?
  @__auto_curry_busy__ ||= false
end

#auto_curry_guardObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
# File 'core/lib/rom/auto_curry.rb', line 18

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