-
-
Save ljsc/5816855 to your computer and use it in GitHub Desktop.
Add factory method.
This file contains hidden or 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
class Post | |
# Make the DI overhead a bit more bareable with a factory method. You can | |
# still use the default constructor if you need the extra flexibility. | |
# | |
# I'm not quite sure about this last parameter though for the should_order. | |
# Most of the time passing a boolean is a code smell that you have some unwanted | |
# control coupling... | |
# | |
def self.standard_post(should_order) | |
alarm_handler = AlarmHandler.new(:post_search) | |
stats_collector = StatsCollector.new | |
new(Searcher.new, alarm_handler, stats_collector, should_order) | |
end | |
def initialize(searcher, alarm_handler, stats_collector, should_order_posts) | |
@searcher = searcher | |
@alarm_handler = alarm_handler | |
@status_collector = stats_collector | |
@should_order_posts = should_order_posts | |
end | |
def search(keywords) | |
search = @searcher.search(keywords, should_order_posts) | |
@alarm_handler.raise('empty!') if search.results.empty? | |
@stats_collector.register_event(:post_search, keywords) | |
search.results | |
end | |
end | |
class SunspotSearcher | |
def search(keywords, should_order) | |
Sunspot.search do | |
fulltext keywords | |
order_by :published_at, :desc if should_order | |
end | |
end | |
end | |
Post.standard_post(env['should_order']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment