Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Extracted beginning_of_workweek to Date class

* Monkeypatches FTW

* SUCK IT @garybernhardt HAHAHAHAHAAAAA
  • Loading branch information...
commit 1ed7f2dd34f5829d786d44b5a60873c792fbdb63 1 parent 5d4f67b
David Brady authored
10 lib/date_patches.rb
... ... @@ -0,0 +1,10 @@
  1 +require 'date'
  2 +
  3 +class Date
  4 + # Returns first Monday on or before start_date
  5 + def beginning_of_workweek
  6 + delta = wday - 1
  7 + delta += 7 if delta < 0 # Sundays will advance the date by default
  8 + self - delta
  9 + end
  10 +end
31 lib/planner.rb
@@ -48,14 +48,16 @@ def initialize(start_date)
48 48 end
49 49
50 50 def self.draw(start_date, filename)
51   - start_date = self.beginning_of_workweek start_date
52   - # pdf = generate_pdf start_date
53   - # save_pdf pdf, filename
54   -
55   - planner = Planner.new start_date
  51 + planner = Planner.new start_date.beginning_of_workweek
56 52 pdf = planner.generate_pdf
57 53 save_pdf pdf, filename
  54 + end
58 55
  56 + def generate_pdf
  57 + Prawn::Document.new page_layout: :landscape do |pdf|
  58 + generate_front_page pdf
  59 + generate_back_page pdf
  60 + end
59 61 end
60 62
61 63 def self.save_pdf(pdf, filename)
@@ -75,13 +77,6 @@ def self.open_file(filename, mode, &block)
75 77 end
76 78 end
77 79
78   - # Returns first Monday on or before start_date
79   - def self.beginning_of_workweek(start_date)
80   - delta = start_date.wday - 1
81   - delta += 7 if delta < 0 # Sundays will advance the date by default
82   - start_date -= delta
83   - end
84   -
85 80 def check_column_positions(&block)
86 81 (0...PAGE_WIDTH).step(COLUMN_WIDTH) do |i|
87 82 yield CHECK_COLUMN_WIDTH + i
@@ -101,11 +96,7 @@ def date_label_for_week
101 96 label += end_date.strftime("%-d, %Y")
102 97 end
103 98
104   - def generate_pdf
105   - Prawn::Document.new page_layout: :landscape do |pdf|
106   - # ======================================================================
107   - # Front Page
108   -
  99 + def generate_front_page(pdf)
109 100 # draw light horz lines--half-hour increments plus to-do list items
110 101 pdf.line_width THIN_LINE_WIDTH
111 102 pdf.opacity LIGHT_LINE_OPACITY do
@@ -171,10 +162,9 @@ def generate_pdf
171 162 day_labels.map.with_index {|label, i| [label, (TODO_COLUMNS+i)*COLUMN_WIDTH]}.each do |label, x|
172 163 pdf.text_box label, at: [x,PAGE_HEIGHT], height: HEADER_HEIGHT, width: COLUMN_WIDTH, align: :center, valign: :center, style: :bold
173 164 end
  165 + end
174 166
175   - # ======================================================================
176   - # Back Page
177   -
  167 + def generate_back_page(pdf)
178 168 pdf.start_new_page
179 169
180 170 # lightweight graph
@@ -199,7 +189,6 @@ def generate_pdf
199 189 0.upto(GRAPH_MAJOR_ROWS).map {|i| i * PAGE_HEIGHT/GRAPH_MAJOR_ROWS }.each do |y|
200 190 pdf.stroke_line [0,y], [PAGE_WIDTH,y]
201 191 end
202   - end
203 192 end
204 193 end
205 194
1  lib/poodle.rb
... ... @@ -1,4 +1,5 @@
1 1 require 'prawn'
  2 +require_relative "date_patches"
2 3 require_relative "prawn_patches"
3 4 require_relative "planner"
4 5
18 spec/lib/date_patches_spec.rb
... ... @@ -0,0 +1,18 @@
  1 +require_relative '../spec_helper'
  2 +
  3 +describe Date do
  4 + describe ".beginning_of_workweek" do
  5 + it "rewinds date to previous Monday" do
  6 + Date.parse('2012-03-15').beginning_of_workweek.should == Date.parse('2012-03-12')
  7 + end
  8 +
  9 + it "does not rewind if given date is a Monday" do
  10 + Date.parse('2012-03-12').beginning_of_workweek.should == Date.parse('2012-03-12')
  11 + end
  12 +
  13 + it "rewinds if given date is a Sunday" do
  14 + Date.parse('2012-03-11').beginning_of_workweek.should == Date.parse('2012-03-05')
  15 + end
  16 + end
  17 +end
  18 +
14 spec/lib/planner_spec.rb
@@ -11,20 +11,6 @@ def md5(string)
11 11 end
12 12
13 13 describe Planner do
14   - describe ".beginning_of_workweek" do
15   - it "rewinds date to previous Monday" do
16   - Planner.beginning_of_workweek(Date.parse('2012-03-15')).should == Date.parse('2012-03-12')
17   - end
18   -
19   - it "does not rewind if given date is a Monday" do
20   - Planner.beginning_of_workweek(Date.parse('2012-03-12')).should == Date.parse('2012-03-12')
21   - end
22   -
23   - it "rewinds if given date is a Sunday" do
24   - Planner.beginning_of_workweek(Date.parse('2012-03-11')).should == Date.parse('2012-03-05')
25   - end
26   - end
27   -
28 14 describe ".draw" do
29 15 before do
30 16 @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.