Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Extract Decorator Step Six
* Flip composition
  • Loading branch information
jferris committed Feb 22, 2013
1 parent 61ca678 commit 876ec97
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions example_app/app/models/survey.rb
Expand Up @@ -9,8 +9,8 @@ class Survey < ActiveRecord::Base

def summaries_using(summarizer, options = {})
questions.map do |question|
UnansweredQuestionHider.new(summarizer, options[:answered_by]).
summarize(question)
hider = UnansweredQuestionHider.new(summarizer, options[:answered_by])
question.summary_using(hider)
end
end
end
4 changes: 2 additions & 2 deletions example_app/app/models/unanswered_question_hider.rb
Expand Up @@ -10,14 +10,14 @@ def summarize(question)
if hide_unanswered_question?(question)
hide_answer_to_question(question)
else
question.summary_using(@summarizer)
@summarizer.summarize(question)
end
end

private

def hide_answer_to_question(question)
Summary.new(question.title, NO_ANSWER)
NO_ANSWER
end

def hide_unanswered_question?(question)
Expand Down
17 changes: 7 additions & 10 deletions example_app/spec/models/unanswered_question_hider_spec.rb
Expand Up @@ -9,36 +9,33 @@

result = hider.summarize(question)

result.title.should eq question.title
result.value.should eq UnansweredQuestionHider::NO_ANSWER
result.should eq UnansweredQuestionHider::NO_ANSWER
end

it 'delegates to the summarizer given a user with an answer' do
summary = stub('summary')
user = build_stubbed(:user)
question = stub_answered_question(user, true)
summarizer = stub_summarizer(question, summary)
summarizer = stub_summarizer(question, 'value')
hider = UnansweredQuestionHider.new(summarizer, user)

result = hider.summarize(question)

result.should eq summary
result.should eq 'value'
end

it 'delegates to the summarizer without a user' do
summary = stub('summary')
question = build_stubbed(:question)
summarizer = stub_summarizer(question, summary)
summarizer = stub_summarizer(question, 'value')
hider = UnansweredQuestionHider.new(summarizer, nil)

result = hider.summarize(question)

result.should eq summary
result.should eq 'value'
end

def stub_summarizer(question, summary)
def stub_summarizer(question, value)
stub('summarizer').tap do |summarizer|
question.stubs(:summary_using).with(summarizer).returns(summary)
summarizer.stubs(:summarize).with(question).returns(value)
end
end

Expand Down

0 comments on commit 876ec97

Please sign in to comment.