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

Returns a new instance of Delegator.



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

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



92
93
94
# File 'lib/dicer/delegator.rb', line 92

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

Class Method Details

.def_delegators(klass, *methods) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/dicer/delegator.rb', line 54

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

.delegate_to(klass) ⇒ Object



49
50
51
52
# File 'lib/dicer/delegator.rb', line 49

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

.except_methodsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dicer/delegator.rb', line 19

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

.include(behavior) ⇒ Object



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

def self.include(behavior)
  super(behavior)

  if behavior <= Dicer::Behavior
    self.except_methods.concat(behavior.behavior_methods)
  end
end

.make(target_class, behaviors) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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
    include Dicer::Contextable

    behaviors.each do |behavior|
      include behavior
    end

    @@behaviors = behaviors
  end

  return delegator
end

Instance Method Details

#behaviorsObject



96
97
98
99
100
101
102
# File 'lib/dicer/delegator.rb', line 96

def behaviors
  if pure.respond_to?(:behaviors)
    pure.behaviors | @@behaviors
  else
    @@behaviors
  end
end

#methods(*args) ⇒ Object



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

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

#private_methods(*args) ⇒ Object



84
85
86
# File 'lib/dicer/delegator.rb', line 84

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

#protected_methods(*args) ⇒ Object



88
89
90
# File 'lib/dicer/delegator.rb', line 88

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

#public_methods(*args) ⇒ Object



80
81
82
# File 'lib/dicer/delegator.rb', line 80

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

#pureObject



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

def pure
  @delegate_object
end

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

Returns:

  • (Boolean)


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

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