Skip to content
Browse files

Made pdf an instvar; tons of other refactorings

* Moved md5() helper method into spec_helper

* Got rid of that naaaaarsty open_file wrapper (which had been written
  purely to make this steaming pile even remotely testable)

* self.draw is starting to look priiiitty niiiice
  • Loading branch information...
1 parent 1ed7f2d commit 80cab9d1445bcc1d868d2be7885d1e78347eb8ec @dbrady committed Mar 13, 2012
Showing with 30 additions and 40 deletions.
  1. +17 −22 lib/planner.rb
  2. +5 −18 spec/lib/planner_spec.rb
  3. +8 −0 spec/spec_helper.rb
View
39 lib/planner.rb
@@ -43,38 +43,33 @@ class Planner
GRAPH_CELL_HEIGHT=9
GRAPH_CELL_WIDTH=9
+ attr_reader :pdf
+
def initialize(start_date)
- @start_date = start_date
+ @start_date = start_date.beginning_of_workweek
end
def self.draw(start_date, filename)
- planner = Planner.new start_date.beginning_of_workweek
- pdf = planner.generate_pdf
- save_pdf pdf, filename
+ planner = Planner.new start_date
+ planner.generate_pdf
+ planner.save filename
end
def generate_pdf
- Prawn::Document.new page_layout: :landscape do |pdf|
- generate_front_page pdf
- generate_back_page pdf
- end
+ @pdf = Prawn::Document.new page_layout: :landscape
+ generate_front_page
+ generate_back_page
+ @pdf
end
- def self.save_pdf(pdf, filename)
- open_file(filename, 'w') do |file|
- file.write pdf.render
+ def save(filename)
+ File.open(filename, "w") do |file|
+ write_to file
end
end
- # This is just a wrapper to File.open so we can test this class
- # without stubbing File.open itself (which other programs in the
- # testing ecosystem are using). A later refactoring could make this
- # filename -> file conversion explicit, and eliminate the need for a
- # stub altogether.
- def self.open_file(filename, mode, &block)
- File.open(filename, mode) do |file|
- yield file
- end
+ def write_to(file)
+ file.write pdf.render
end
def check_column_positions(&block)
@@ -96,7 +91,7 @@ def date_label_for_week
label += end_date.strftime("%-d, %Y")
end
- def generate_front_page(pdf)
+ def generate_front_page
# draw light horz lines--half-hour increments plus to-do list items
pdf.line_width THIN_LINE_WIDTH
pdf.opacity LIGHT_LINE_OPACITY do
@@ -164,7 +159,7 @@ def generate_front_page(pdf)
end
end
- def generate_back_page(pdf)
+ def generate_back_page
pdf.start_new_page
# lightweight graph
View
23 spec/lib/planner_spec.rb
@@ -2,35 +2,22 @@
require_relative '../spec_helper'
require 'digest/md5'
-def md5(string)
- digest = Digest::MD5.new
- string.each_line do |line|
- digest << line
- end
- digest.hexdigest
-end
-
describe Planner do
- describe ".draw" do
+ describe ".generate_pdf" do
before do
@planner_filename = "spec_planner_sheet.pdf"
@date = Date.parse '2012-03-11'
end
- it "writes the PDF with correct MD5 checksum" do
+ it "creates a PDF with correct MD5 checksum" do
buffer = StringIO.new
- Planner.should_receive(:open_file).with(@planner_filename, "w").and_yield(buffer)
- Planner.draw @date, @planner_filename
+ planner = Planner.new @date
+ pdf = planner.generate_pdf
+ planner.write_to buffer
md5(buffer.string).should == MAGIC_MD5_CHECKSUM
end
end
- # Returns a label for the week starting on start_date, e.g. "Mar 5 -
- # 11, 2012". If the week spans a month, both month abbreviations are
- # included, e.g. "Mar 26 - Feb 1, 2012". The year is NOT duplicated
- # if it is spanned, mostly because it's very rare and the expansion
- # looks as weird as the unexpanded version. So the correct output
- # for Dec 26, 2011 would be e.g. "Dec 26 - Jan 1, 2012"
describe ".date_label_for_week" do
it "returns correct date string for the week" do
Planner.new(Date.parse("2012-03-12")).date_label_for_week.should == "Mar 12 - 18, 2012"
View
8 spec/spec_helper.rb
@@ -3,3 +3,11 @@
MAGIC_MD5_CHECKSUM="e59168e1dca8c81f9b92ac5b5179263a"
+def md5(string)
+ digest = Digest::MD5.new
+ string.each_line do |line|
+ digest << line
+ end
+ digest.hexdigest
+end
+

0 comments on commit 80cab9d

Please sign in to comment.
Something went wrong with that request. Please try again.