Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Replace Subclasses with Strategies: Step One
* Extract one method to a strategy for each subclass
- Loading branch information
Showing
9 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class MultipleChoiceSubmittable | ||
def initialize(question) | ||
@question = question | ||
end | ||
|
||
def score(text) | ||
options.score(text) | ||
end | ||
|
||
private | ||
|
||
def options | ||
@question.options | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
class OpenQuestion < Question | ||
def score(text) | ||
0 | ||
submittable.score(text) | ||
end | ||
|
||
def breakdown | ||
text_from_ordered_answers = answers.order(:created_at).pluck(:text) | ||
text_from_ordered_answers.join(', ') | ||
end | ||
|
||
def submittable | ||
OpenSubmittable.new | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class OpenSubmittable | ||
def score(text) | ||
0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ScaleSubmittable | ||
def score(text) | ||
text.to_i | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
example_app/spec/models/multiple_choice_submittable_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
describe MultipleChoiceSubmittable, '#score' do | ||
it 'returns the score for the option with the given text' do | ||
question = build_stubbed(:multiple_choice_question) | ||
submittable = MultipleChoiceSubmittable.new(question) | ||
question.options.target.stubs(score: 2) | ||
|
||
result = submittable.score('two') | ||
|
||
question.options.target.should have_received(:score).with('two') | ||
result.should eq 2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
describe OpenSubmittable, '#score' do | ||
it 'returns zero' do | ||
submittable = OpenSubmittable.new | ||
|
||
result = submittable.score('anything') | ||
|
||
result.should eq 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
describe ScaleSubmittable, '#score' do | ||
it 'returns the integer value of the text' do | ||
submittable = ScaleSubmittable.new | ||
|
||
result = submittable.score('5') | ||
|
||
result.should eq 5 | ||
end | ||
end |