aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/dsl/stanza_proxy.rb
blob: b1a850c5aa1716f102a6094abe84b028685cab1e (plain)
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
module Hbc
  class DSL
    class StanzaProxy
      attr_reader :type

      def self.once(type)
        resolved = nil
        new(type) { resolved ||= yield }
      end

      def initialize(type, &resolver)
        @type = type
        @resolver = resolver
      end

      def proxy?
        true
      end

      def to_s
        @resolver.call.to_s
      end

      # Serialization for dumpcask
      def encode_with(coder)
        coder["type"] = type
        coder["resolved"] = @resolver.call
      end

      def method_missing(method, *args)
        if method != :to_ary
          @resolver.call.send(method, *args)
        else
          super
        end
      end

      def respond_to?(method, include_private = false)
        return true if %i{encode_with proxy? to_s type}.include?(method)
        return false if method == :to_ary
        @resolver.call.respond_to?(method, include_private)
      end

      def respond_to_missing?(*)
        true
      end
    end
  end
end