File: utils.rb

package info (click to toggle)
ruby-roadie-rails 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,280 kB
  • sloc: ruby: 1,670; sh: 47; makefile: 4
file content (62 lines) | stat: -rw-r--r-- 1,776 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

module Roadie
  module Rails
    module Utils
      module_function

      # Combine two hashes, or return the non-nil hash if either is nil.
      # Returns nil if both are nil.
      def combine_hash(first, second)
        combine_nilable(first, second) do |a, b|
          a.merge(b)
        end
      end

      # Return a callable that will call both inputs. If either is nil, then
      # just return the other.
      #
      # The result from the second one will be the result of the combined
      # callable.
      #
      # ```ruby
      # combine_callable(-> { 1 }, -> { 2 }).call # => 2
      # combine_callable(-> { 1 }, nil).call # => 1
      # combine_callable(nil, nil).nil? # => true
      # ```
      def combine_callable(first, second)
        combine_nilable(first, second) do |a, b|
          lambda do |*args|
            a.call(*args)
            b.call(*args)
          end
        end
      end

      # Combine two Provider ducks into a ProviderList. If either is nil, pick
      # the non-nil value instead.
      def combine_providers(first, second)
        combine_nilable(first, second) do |a, b|
          ProviderList.new(a.to_a + Array.wrap(b))
        end
      end

      # Discard the nil value. If neither is nil, then yield both and return
      # the result from the block.
      #
      # ```ruby
      # combine_nilable(nil, 5) { |a, b| a+b } # => 5
      # combine_nilable(7, nil) { |a, b| a+b } # => 7
      # combine_nilable(nil, nil) { |a, b| a+b } # => nil
      # combine_nilable(7, 5) { |a, b| a+b } # => 12
      # ```
      def combine_nilable(first, second)
        if first && second
          yield first, second
        else
          first || second
        end
      end
    end
  end
end