Skip to content
Browse files

Extracted beginning_of_workweek to Date class

* Monkeypatches FTW

* SUCK IT @garybernhardt HAHAHAHAHAAAAA
  • Loading branch information...
1 parent 5d4f67b commit 1ed7f2dd34f5829d786d44b5a60873c792fbdb63 @dbrady committed Mar 13, 2012
Showing with 39 additions and 35 deletions.
  1. +10 −0 lib/date_patches.rb
  2. +10 −21 lib/planner.rb
  3. +1 −0 lib/poodle.rb
  4. +18 −0 spec/lib/date_patches_spec.rb
  5. +0 −14 spec/lib/planner_spec.rb
View
10 lib/date_patches.rb
@@ -0,0 +1,10 @@
+require 'date'
+
+class Date
+ # Returns first Monday on or before start_date
+ def beginning_of_workweek
+ delta = wday - 1
+ delta += 7 if delta < 0 # Sundays will advance the date by default
+ self - delta
+ end
+end
View
31 lib/planner.rb
@@ -48,14 +48,16 @@ def initialize(start_date)
end
def self.draw(start_date, filename)
- start_date = self.beginning_of_workweek start_date
- # pdf = generate_pdf start_date
- # save_pdf pdf, filename
-
- planner = Planner.new start_date
+ planner = Planner.new start_date.beginning_of_workweek
pdf = planner.generate_pdf
save_pdf pdf, filename
+ end
+ def generate_pdf
+ Prawn::Document.new page_layout: :landscape do |pdf|
+ generate_front_page pdf
+ generate_back_page pdf
+ end
end
def self.save_pdf(pdf, filename)
@@ -75,13 +77,6 @@ def self.open_file(filename, mode, &block)
end
end
- # Returns first Monday on or before start_date
- def self.beginning_of_workweek(start_date)
- delta = start_date.wday - 1
- delta += 7 if delta < 0 # Sundays will advance the date by default
- start_date -= delta
- end
-
def check_column_positions(&block)
(0...PAGE_WIDTH).step(COLUMN_WIDTH) do |i|
yield CHECK_COLUMN_WIDTH + i
@@ -101,11 +96,7 @@ def date_label_for_week
label += end_date.strftime("%-d, %Y")
end
- def generate_pdf
- Prawn::Document.new page_layout: :landscape do |pdf|
- # ======================================================================
- # Front Page
-
+ def generate_front_page(pdf)
# draw light horz lines--half-hour increments plus to-do list items
pdf.line_width THIN_LINE_WIDTH
pdf.opacity LIGHT_LINE_OPACITY do
@@ -171,10 +162,9 @@ def generate_pdf
day_labels.map.with_index {|label, i| [label, (TODO_COLUMNS+i)*COLUMN_WIDTH]}.each do |label, x|
pdf.text_box label, at: [x,PAGE_HEIGHT], height: HEADER_HEIGHT, width: COLUMN_WIDTH, align: :center, valign: :center, style: :bold
end
+ end
- # ======================================================================
- # Back Page
-
+ def generate_back_page(pdf)
pdf.start_new_page
# lightweight graph
@@ -199,7 +189,6 @@ def generate_pdf
0.upto(GRAPH_MAJOR_ROWS).map {|i| i * PAGE_HEIGHT/GRAPH_MAJOR_ROWS }.each do |y|
pdf.stroke_line [0,y], [PAGE_WIDTH,y]
end
- end
end
end
View
1 lib/poodle.rb
@@ -1,4 +1,5 @@
require 'prawn'
+require_relative "date_patches"
require_relative "prawn_patches"
require_relative "planner"
View
18 spec/lib/date_patches_spec.rb
@@ -0,0 +1,18 @@
+require_relative '../spec_helper'
+
+describe Date do
+ describe ".beginning_of_workweek" do
+ it "rewinds date to previous Monday" do
+ Date.parse('2012-03-15').beginning_of_workweek.should == Date.parse('2012-03-12')
+ end
+
+ it "does not rewind if given date is a Monday" do
+ Date.parse('2012-03-12').beginning_of_workweek.should == Date.parse('2012-03-12')
+ end
+
+ it "rewinds if given date is a Sunday" do
+ Date.parse('2012-03-11').beginning_of_workweek.should == Date.parse('2012-03-05')
+ end
+ end
+end
+
View
14 spec/lib/planner_spec.rb
@@ -11,20 +11,6 @@ def md5(string)
end
describe Planner do
- describe ".beginning_of_workweek" do
- it "rewinds date to previous Monday" do
- Planner.beginning_of_workweek(Date.parse('2012-03-15')).should == Date.parse('2012-03-12')
- end
-
- it "does not rewind if given date is a Monday" do
- Planner.beginning_of_workweek(Date.parse('2012-03-12')).should == Date.parse('2012-03-12')
- end
-
- it "rewinds if given date is a Sunday" do
- Planner.beginning_of_workweek(Date.parse('2012-03-11')).should == Date.parse('2012-03-05')
- end
- end
-
describe ".draw" do
before do
@planner_filename = "spec_planner_sheet.pdf"

0 comments on commit 1ed7f2d

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