Skip to content

Commit 7747366

Browse files
committed
Replace Subclasses with Strategies: Step One
* Extract one method to a strategy for each subclass
1 parent 4939d3e commit 7747366

9 files changed

Lines changed: 70 additions & 3 deletions

example_app/app/models/multiple_choice_question.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def options_for_form
1212
end
1313

1414
def score(text)
15-
options.score(text)
15+
submittable.score(text)
1616
end
1717

1818
def breakdown
@@ -24,4 +24,8 @@ def breakdown
2424
end
2525
percents.join(', ')
2626
end
27+
28+
def submittable
29+
MultipleChoiceSubmittable.new(self)
30+
end
2731
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class MultipleChoiceSubmittable
2+
def initialize(question)
3+
@question = question
4+
end
5+
6+
def score(text)
7+
options.score(text)
8+
end
9+
10+
private
11+
12+
def options
13+
@question.options
14+
end
15+
end
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
class OpenQuestion < Question
22
def score(text)
3-
0
3+
submittable.score(text)
44
end
55

66
def breakdown
77
text_from_ordered_answers = answers.order(:created_at).pluck(:text)
88
text_from_ordered_answers.join(', ')
99
end
10+
11+
def submittable
12+
OpenSubmittable.new
13+
end
1014
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class OpenSubmittable
2+
def score(text)
3+
0
4+
end
5+
end

example_app/app/models/scale_question.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ScaleQuestion < Question
33
validates :minimum, presence: true
44

55
def score(text)
6-
text.to_i
6+
submittable.score(text)
77
end
88

99
def steps
@@ -13,4 +13,8 @@ def steps
1313
def breakdown
1414
sprintf('Average: %.02f', answers.average('text'))
1515
end
16+
17+
def submittable
18+
ScaleSubmittable.new
19+
end
1620
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ScaleSubmittable
2+
def score(text)
3+
text.to_i
4+
end
5+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
describe MultipleChoiceSubmittable, '#score' do
2+
it 'returns the score for the option with the given text' do
3+
question = build_stubbed(:multiple_choice_question)
4+
submittable = MultipleChoiceSubmittable.new(question)
5+
question.options.target.stubs(score: 2)
6+
7+
result = submittable.score('two')
8+
9+
question.options.target.should have_received(:score).with('two')
10+
result.should eq 2
11+
end
12+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
describe OpenSubmittable, '#score' do
2+
it 'returns zero' do
3+
submittable = OpenSubmittable.new
4+
5+
result = submittable.score('anything')
6+
7+
result.should eq 0
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
describe ScaleSubmittable, '#score' do
2+
it 'returns the integer value of the text' do
3+
submittable = ScaleSubmittable.new
4+
5+
result = submittable.score('5')
6+
7+
result.should eq 5
8+
end
9+
end

0 commit comments

Comments
 (0)