-
-
Save manlycode/4756234 to your computer and use it in GitHub Desktop.
0
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 :title, :text | |
def initialize(ship) | |
@title = "USS Enterprise Report" | |
logs = retrieve_logs_for_year(Time.now.year) | |
@text = format_lines(logs) | |
end | |
def retrieve_logs_for_year(year) | |
logs = ship.logs.where( | |
"date >= '#{year}-01-01' AND state=?", | |
"active" | |
).order("date DESC") | |
end | |
def format_lines(logs) | |
logs.map do |log| | |
status = "#{log.date}: #{log.message} (#{log.alert_level})" | |
end | |
end | |
def plain_format(title, line) | |
output = [] | |
output << "###### #{title} ######" | |
@text.each_line do |line| | |
output << line | |
end | |
output.join "\n" | |
end | |
def html_format(title, line) | |
output = "<html>" | |
output << " <head>" | |
output << " <title>#{title}</title>" | |
output << " </head>" | |
output << " <body>" | |
@text.each do |line| | |
output << " <p>#{line}</p>" | |
end | |
output << " </body>" | |
output << "</html>" | |
output | |
end | |
def json_format(title, line) | |
output = { title: title, lines: [] } | |
@text.each do |line| | |
output[:lines] << line | |
end | |
output | |
end | |
def generate(format) | |
if format == :plain | |
plain_format(title, line) | |
elsif format == :html | |
html_format(title, line) | |
elsif format == :json | |
json_format(title,line) | |
else | |
raise "Unknown report format: #{format.inspect}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment