Sha256: e702d1c886f8cc432bbc92b90315d57ab7d0aaa249957f988cc91bb7329e8a15

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

module Giblish
  # delegates all method calls to the first supplied delegate that
  # implements it.
  class DataDelegator
    attr_reader :delegates

    def initialize(*delegate_arr)
      @delegates = Array(delegate_arr)
    end

    def add(delegate)
      @delegates << delegate
    end

    # define this to short-cut circular references
    #
    # TODO: This should probably be avoided by refactoring the SuccessfulConversion
    # class which, as of this writing, is part of a circular ref to a PathTree which
    # throws 'inspect' calls into an eternal loop instead of implementing a custom 'inspect'
    # method.
    def inspect
      @delegates.map do |d|
        "<#{d.class}:#{d.object_id}>"
      end.join(",")
    end

    def method_missing(m, *args, &block)
      del = @delegates&.find { |d| d.respond_to?(m) }
      if del.nil?
        Giblog.logger.warn { "Did not find method '#{m}' in any delegate (#{@delegates}"}
      end

      del.nil? ? super : del.send(m, *args, &block)
    end

    def respond_to_missing?(method_name, include_private = false)
      ok = @delegates.find { |d|
        d.respond_to?(method_name)
      }

      ok || super(method_name, include_private)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
giblish-2.2.2 lib/giblish/config_utils.rb
giblish-2.2.1 lib/giblish/config_utils.rb
giblish-2.2.0 lib/giblish/config_utils.rb