Class: ExplicitDelegator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/explicit_delegator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate) ⇒ ExplicitDelegator

Returns a new instance of ExplicitDelegator.



16
17
18
19
20
# File 'lib/explicit_delegator.rb', line 16

def initialize(delegate)
  @delegate = delegate

  ensure_defined
end

Class Method Details

.enforce_definitions(*methods) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/explicit_delegator.rb', line 6

def self.enforce_definitions(*methods)
  define_method(:enforced_methods) do
    super() | methods
  end

  methods.each do |method|
    def_instance_delegator(:@delegate, method)
  end
end

Instance Method Details

#enforced_methodsObject



22
23
24
# File 'lib/explicit_delegator.rb', line 22

def enforced_methods
  []
end

#ensure_definedObject



26
27
28
29
30
31
32
# File 'lib/explicit_delegator.rb', line 26

def ensure_defined
  missing_methods = []
  enforced_methods.each do |method|
    missing_methods << method unless @delegate.methods.include?(method)
  end
  raise "Methods required to use #{self.class}: #{missing_methods}" unless missing_methods.empty?
end