Module: BBLib::Delegator

Defined in:
lib/bblib/core/mixins/delegator.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bblib/core/mixins/delegator.rb', line 15

def method_missing(method, *args, &block)
  delegates.each do |delegate|
    case delegate
    when Symbol
      next unless respond_to?(delegate) && method(delegate).arity == 0
      object = send(delegate)
      next unless object.respond_to?(method)
      return object.send(method, *args, &block)
    else
      next unless delegate.respond_to?(method)
      return delegate.send(method, *args, &block)
    end
  end
  super
end

Class Method Details

.included(base) ⇒ Object



4
5
6
7
# File 'lib/bblib/core/mixins/delegator.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
  base.send(:attr_ary, :instance_delegates)
end

Instance Method Details

#delegatesObject



9
10
11
# File 'lib/bblib/core/mixins/delegator.rb', line 9

def delegates
  (instance_delegates + self.class.delegates).uniq
end