Permalink
Browse files

Refactoring

  • Loading branch information...
1 parent 8c3ee9c commit 4363490a8c6108394b63cdd443e8b9bc452fb3b1 @brynary committed Dec 9, 2008
Showing with 62 additions and 42 deletions.
  1. +9 −1 lib/features2cards/card.rb
  2. +25 −39 lib/features2cards/cli.rb
  3. +28 −2 lib/features2cards/prawn.rb
View
@@ -4,7 +4,15 @@ class Card
attr_reader :type
attr_reader :body
attr_reader :footer
-
+
+ def self.for_feature(feature)
+ new("Feature", feature.header.split("\n").first, "")
+ end
+
+ def self.for_scenario(scenario)
+ new("Scenario", scenario.name, scenario.feature.header.split("\n").first)
+ end
+
def initialize(type, body, footer = nil)
@type = type
@body = body
View
@@ -22,52 +22,38 @@ def self.load_cucumber
end
def execute
- files = ARGV
- parser = Cucumber::TreetopParser::FeatureParser.new
-
- features = []
-
- files.each do |file|
- features << parser.parse_feature(file)
- end
-
- cards = features_to_cards(features)
generate_pdf(cards)
end
+
+ def cards
+ features_to_cards(features)
+ end
+
+ def features
+ files.map do |file|
+ parser.parse_feature(file)
+ end
+ end
+
+ def files
+ ARGV
+ end
+
+ def parser
+ @parser ||= Cucumber::TreetopParser::FeatureParser.new
+ end
def features_to_cards(features)
- cards = []
-
- features.map { |f| f.scenarios }.flatten.each do |scenario|
- cards << Card.new("Scenario", scenario.name, scenario.feature.header.split("\n").first)
- end
-
- cards
+ features.map do |feature|
+ [Card.for_feature(feature)] +
+ feature.scenarios.map do |scenario|
+ Card.for_scenario(scenario)
+ end
+ end.flatten
end
def generate_pdf(cards)
- Prawn::Document.generate("cards.pdf", :page_layout => :landscape) do
- row = 2
- col = 0
-
- cards.each do |card|
- if row == 0
- start_new_page
- row = 2
- col = 0
- end
-
- draw_card(card, row, col)
-
- col += 1
-
- if col > 1
- col = 0
- row -= 1
- end
- end
- end
-
+ Prawn::Document.generate_cards(cards)
end
end
@@ -2,6 +2,30 @@ class Prawn::Document
CARD_WIDTH = 72 * 5 # 5 inches
CARD_HEIGHT = 72 * 3 # 3 inches
+ def self.generate_cards(cards)
+ generate("cards.pdf", :page_layout => :landscape) do
+ row = 2
+ col = 0
+
+ cards.each do |card|
+ if row == 0
+ start_new_page
+ row = 2
+ col = 0
+ end
+
+ draw_card(card, row, col)
+
+ col += 1
+
+ if col > 1
+ col = 0
+ row -= 1
+ end
+ end
+ end
+ end
+
def margin_box(margin, &block)
bounding_box [bounds.left + margin, bounds.top - margin],
:width => bounds.width - (margin * 2), :height => bounds.height - (margin * 2),
@@ -25,8 +49,10 @@ def draw_card(card, row, col)
text card.body, :size => 16, :align => :center
end
- bounding_box [bounds.left, bounds.bottom + 18], :width => bounds.width, :height => 18 do
- text card.footer, :align => :right
+ unless card.footer.nil?
+ bounding_box [bounds.left, bounds.bottom + 18], :width => bounds.width, :height => 18 do
+ text card.footer, :align => :right
+ end
end
end
end

0 comments on commit 4363490

Please sign in to comment.