Project

General

Profile

Bug #12278 ยป procs_reducer.rb

newmen (Gleb Averchuk), 04/13/2016 06:19 PM

 
1
module VersatileDiamond
2
  module Modules
3

    
4
    # Provides method for reducing proc objects
5
    module ProcsReducer
6
    private
7

    
8
      # Combines passed procs to one function
9
      # @param [Array] procs which will be combined
10
      # @yield returns heart of combination result
11
      # @return [Proc] the general function which contains calls of all other nested
12
      def reduce_procs(procs, &deepest_block)
13
        procs.reverse.reduce(deepest_block) do |acc, block|
14
          -> { block[&acc] }
15
        end
16
      end
17

    
18
      # Calls combined procs as one function
19
      # @param [Array] procs which will be combined
20
      # @yield returns heart of combination result
21
      # @return [Object]
22
      def call_procs(procs, &block)
23
        reduce_procs(procs, &block).call
24
      end
25

    
26
      # Collects procs which nests each other, after than calls combined procedure
27
      # @param [Proc] deepest_block the block for the deepest call
28
      # @yield [Symbol, Array, Hash] nests the some method call
29
      # @return [Object] the result of deepest block call
30
      def inlay_procs(deepest_block, &block)
31
        procs = []
32
        nest = -> method_name, *args, **kwargs do
33
          procs <<
34
            if kwargs.empty?
35
              -> &prc { send(method_name, *args, &prc) }
36
            else
37
              -> &prc { send(method_name, *args, **kwargs, &prc) }
38
            end
39
        end
40

    
41
        block[nest]
42
        call_procs(procs, &deepest_block)
43
      end
44
    end
45

    
46
  end
47
end