-
-
Save ejdyksen/5044844 to your computer and use it in GitHub Desktop.
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
# | |
# The goal of this exercise is work on identifying abstraction which helps simplify, document, | |
# and separate the concerns going on in file. | |
# | |
# Exercise: | |
# * Find related ideas in the below code | |
# * Abstract them out (methods, modules, classes, etc, you pick!) | |
# * If you find multiple ways, then do a separate gist for each way. | |
# * Rinse repeat until you see no other ways. | |
# | |
# Note: there is not enough of the code-base to run this code, so in order to run | |
# the code you'll have to implement the missing pieces. | |
class Report | |
attr_accessor :ship | |
def initialize(ship) | |
@ship = ship | |
end | |
def title | |
"#{ship.name} Report" | |
end | |
def logs | |
ship.logs.active.this_year.order("date DESC") | |
end | |
def generate(format) | |
Report.const_get(format.to_s.capitalize).new(self).generate | |
end | |
class Exporter | |
attr_accessor :report | |
initialize(report) | |
@report = report | |
end | |
end | |
class Plain < Exporter | |
def generate | |
output = [] | |
output << "###### #{@title} ######" | |
ship.logs do |log| | |
output << log.line | |
end | |
output.join "\n" | |
end | |
end | |
class Html < Exporter | |
def generate | |
output = "<html>" | |
output << " <head>" | |
output << " <title>#{@title}</title>" | |
output << " </head>" | |
output << " <body>" | |
ship.logs.each do |log| | |
output << " <p>#{log.line}</p>" | |
end | |
output << " </body>" | |
output << "</html>" | |
output | |
end | |
end | |
class Json < Exporter | |
def generate | |
output = { title: @title, lines: [] } | |
ship.logs.each do |log| | |
output[:lines] << log.line | |
end | |
output | |
end | |
end | |
end | |
class Ship | |
attr_accessor :name | |
has_many :logs | |
end | |
class Log | |
scope :active, where("status = ?", "active") | |
scope :this_year, where("date >= '#{Time.now.year}-01-01' AND state=?") | |
def line | |
"#{self.date}: #{self.message} (#{self.alert_level})" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment