Class: Dicer::Delegator

Inherits:
Object
  • Object
show all
Defined in:
lib/dicer/delegator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Delegator



52
53
54
# File 'lib/dicer/delegator.rb', line 52

def initialize(object)
  @delegate_object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



76
77
78
# File 'lib/dicer/delegator.rb', line 76

def method_missing(name, *args, &block)
  @delegate_object.send(name, *args, &block)
end

Class Method Details

.def_delegators(klass, *methods) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/dicer/delegator.rb', line 42

def self.def_delegators(klass, *methods)
  methods.each do |method|
    class_eval "      def \#{method}(*args, &block)\n        @delegate_object.__send__(:\#{method}, *args, &block)\n      end\n    EOF\n  end\nend\n"

.delegate_to(klass) ⇒ Object



37
38
39
40
# File 'lib/dicer/delegator.rb', line 37

def self.delegate_to(klass)
  delegate_methods = klass.public_methods.map(&:to_sym) - self.except_methods
  def_delegators(klass, *delegate_methods)
end

.except_methodsObject



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

def self.except_methods
  @except_methods ||= [
    :__send__,
    :__id__,
    :object_id,
    :respond_to?,
    :methods,
    :public_methods,
    :private_methods,
    :protected_methods,
    :method_missing,
    # for RSpec
    :should,
    :should_not
  ]
end

.include(behavior) ⇒ Object



32
33
34
35
# File 'lib/dicer/delegator.rb', line 32

def self.include(behavior)
  super(behavior)
  self.except_methods.concat(behavior.behavior_methods)
end

.make(target_class, behaviors) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/dicer/delegator.rb', line 3

def self.make(target_class, behaviors)
  delegator = Class.new(Dicer::Delegator)
  delegator.delegate_to(target_class)
  delegator.class_eval do
    behaviors.each do |behavior|
      include behavior
    end
  end

  return delegator
end

Instance Method Details

#methods(*args) ⇒ Object



60
61
62
# File 'lib/dicer/delegator.rb', line 60

def methods(*args)
  @delegate_object.mehtods(*args) | super
end

#private_methods(*args) ⇒ Object



68
69
70
# File 'lib/dicer/delegator.rb', line 68

def private_methods(*args)
  @delegate_object.private_mehtods(*args) | super
end

#protected_methods(*args) ⇒ Object



72
73
74
# File 'lib/dicer/delegator.rb', line 72

def protected_methods(*args)
  @delegate_object.mehtods(*args) | super
end

#public_methods(*args) ⇒ Object



64
65
66
# File 'lib/dicer/delegator.rb', line 64

def public_methods(*args)
  @delegate_object.public_methods(*args) | super
end

#respond_to?(name, private = false) ⇒ Boolean



56
57
58
# File 'lib/dicer/delegator.rb', line 56

def respond_to?(name, private = false)
  super || @delegate_object.respond_to?(name)
end