-
Notifications
You must be signed in to change notification settings - Fork 49
/
11_method_object.rb
45 lines (38 loc) · 960 Bytes
/
11_method_object.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Method Object
#
# See also "Sprout Class" in Working Effectively with Legacy Code
# Original:
class Obligation
def send_task(task, job)
not_processed, processed, copied, executed = []
# ...150 lines of heavily commented code...
end
end
# Refactored:
class TaskSender
attr_accessor :obligation, :task, :job, :not_processed, :processed,
:copied, :executed
def initialize(obligation, task, job)
@obligation = obligation
@task = task
@job = job
end
# The Ruby convention for an "executable object" is to implement #call
def call
# ...150 lines of heavily commented code...
#
# (remembering to replace all instances of e.g. "copied =" with
# "self.copied =")
end
end
class Obligation
def send_task(task, job)
TaskSender.new(self, task, job).call
end
end
# Or in Ruby 1.9:
class Obligation
def send_task(task, job)
TaskSender.new(self, task, job).()
end
end