Class: Giblish::DataDelegator

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/config_utils.rb

Overview

delegates all method calls to the first supplied delegate that implements it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*delegate_arr) ⇒ DataDelegator

Returns a new instance of DataDelegator.



7
8
9
# File 'lib/giblish/config_utils.rb', line 7

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/giblish/config_utils.rb', line 27

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

Instance Attribute Details

#delegatesObject (readonly)

Returns the value of attribute delegates.



5
6
7
# File 'lib/giblish/config_utils.rb', line 5

def delegates
  @delegates
end

Instance Method Details

#add(delegate) ⇒ Object



11
12
13
# File 'lib/giblish/config_utils.rb', line 11

def add(delegate)
  @delegates << delegate
end

#inspectObject

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.



21
22
23
24
25
# File 'lib/giblish/config_utils.rb', line 21

def inspect
  @delegates.map do |d|
    "<#{d.class}:#{d.object_id}>"
  end.join(",")
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/giblish/config_utils.rb', line 36

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