Permalink
Browse files

Parse stories as YAML file

  • Loading branch information...
1 parent 11e7e89 commit 5d2a8c71e4dc023956b2f6e6b8024fc9d09681c8 Wes Gibbs committed with Wes Gibbs Jan 29, 2010
View
@@ -26,30 +26,4 @@ end.parse!
story_file = ARGV.empty? ? "stories.slurper" : ARGV[0]
-story_lines = Array.new
-stories = Array.new
-IO.foreach(story_file) do |line|
- if line[0,2] == "==" && story_lines.any?
- stories << Story.new.parse(story_lines)
- story_lines.clear
- else
- story_lines << line
- end
-end
-
-stories.reverse! unless options[:reverse]
-
-stories.each_with_index do |story, index|
- begin
- story.save
- puts "Added \"#{story.name}\""
- rescue ActiveResource::ServerError, ActiveResource::ResourceNotFound => e
- msg = "Slurp failed on story "
- if story.attributes["name"]
- msg << story.attributes["name"]
- else
- msg << "##{options[:reverse] ? index + 1 : stories.size - index }"
- end
- puts msg + ". Error: #{e}"
- end
-end
+Slurper.slurp(story_file, options[:reverse])
View
@@ -1 +1,54 @@
require 'story'
+
+class Slurper
+ attr_accessor :story_file, :stories
+
+ def self.slurp(story_file, reverse = true)
+ slurper = new(story_file)
+ slurper.yamlize_story_file
+ slurper.stories.reverse! if reverse
+ slurper.create_stories
+ end
+
+ def initialize(story_file)
+ self.story_file = story_file
+ end
+
+ def yamlize_story_file
+ self.stories = YAML.load(
+ IO.read(story_file).
+ gsub(/^/, " ").
+ gsub(/ ==.*/, "- !ruby/object:Story\n attributes:").
+ gsub(/ description:$/, " description: |")
+ )
+ scrub_descriptions
+ end
+
+ def scrub_descriptions
+ stories.each do |story|
+ if story.attributes["description"]
+ story.description = story.description.gsub(" ", "").gsub(" \n", "\n")
+ end
+ if story.attributes["description"] && story.description == ""
+ story.attributes["description"] = nil
+ end
+ end
+ end
+
+ def create_stories
+ stories.each_with_index do |story, index|
+ begin
+ story.save
+ puts "Added \"#{story.name}\""
+ rescue ActiveResource::ServerError, ActiveResource::ResourceNotFound => e
+ msg = "Slurp failed on story "
+ if story.attributes["name"]
+ msg << story.attributes["name"]
+ else
+ msg << "##{options[:reverse] ? index + 1 : stories.size - index }"
+ end
+ puts msg + ". Error: #{e}"
+ end
+ end
+ end
+end
View
@@ -13,71 +13,4 @@ def initialize(attributes = {})
load(@@defaults.merge(attributes))
end
- def parse(story_lines)
- @story_lines = story_lines
- parse_type
- parse_name
- parse_description
- parse_labels
- self
- end
-
- private
-
- def parse_type
- @story_lines.each_with_index do |line, i|
- if start_of_value?(line, 'type')
- if starts_with_whitespace?(@story_lines[i+1])
- @attributes["story_type"] = @story_lines[i+1].strip
- else
- @attributes.delete("story_type")
- end
- end
- end
- end
-
- def parse_name
- @story_lines.each_with_index do |line, i|
- if start_of_value?(line, 'name')
- if starts_with_whitespace?(@story_lines[i+1])
- @attributes["name"] = @story_lines[i+1].strip
- else
- @attributes.delete("name")
- end
- end
- end
- end
-
- def parse_description
- @story_lines.each_with_index do |line, i|
- if start_of_value?(line, 'description')
- desc = Array.new
- while((next_line = @story_lines[i+=1]) && starts_with_whitespace?(next_line)) do
- desc << next_line
- end
- desc.empty? ? @attributes.delete("description") : @attributes["description"] = desc.join.gsub(/^ +/, "").gsub(/^\t+/, "")
- end
- end
- end
-
- def parse_labels
- @story_lines.each_with_index do |line, i|
- if start_of_value?(line, 'labels')
- if starts_with_whitespace?(@story_lines[i+1])
- @attributes["labels"] = @story_lines[i+1].strip
- else
- @attributes.delete("labels")
- end
- end
- end
- end
-
- def starts_with_whitespace?(line)
- line && line[0,1] =~ /\s/
- end
-
- def start_of_value?(line, attribute)
- line[0,attribute.size] == attribute
- end
-
end
@@ -0,0 +1,4 @@
+==
+name:
+description:
+labels:
@@ -1,3 +0,0 @@
-name
-description
-labels
@@ -1,14 +1,15 @@
-type
+==
+story_type:
feature
-name
+name:
Profit
-description
+description:
In order to do something
As a role
I want to click a thingy
Acceptance:
- * do the thing
- * don't forget the other thing
-labels
+ - do the thing
+ - don't forget the other thing
+labels:
money,power,fame
@@ -1,2 +1,3 @@
-name
+==
+name:
Profit
@@ -1,2 +1,3 @@
-description
+==
+description:
I have a "quote"
@@ -0,0 +1,7 @@
+==
+name:
+ Profit
+description:
+ In order to do something
+ As a role
+ I want to click a thingy
@@ -1,6 +0,0 @@
-name
- Profit
-description
- In order to do something
- As a role
- I want to click a thingy
@@ -1,18 +1,14 @@
require 'rubygems'
require 'spec'
-require 'story'
+require 'slurper'
-describe Story do
-
- it ".parse should return a reference to the story" do
- story = Story.new
- story.parse("").should == story
- end
+describe Slurper do
context "deals with leading/trailing whitespace" do
before do
- story_lines = IO.readlines(File.join(File.dirname(__FILE__), "fixtures", "whitespacey_story.txt"))
- @story = Story.new.parse(story_lines)
+ slurper = Slurper.new(File.join(File.dirname(__FILE__), "fixtures", "whitespacey_story.slurper"))
+ slurper.yamlize_story_file
+ @story = slurper.stories.first
end
it "strips whitespace from the name" do
@@ -26,16 +22,17 @@
context "given values for all attributes" do
before do
- story_lines = IO.readlines(File.join(File.dirname(__FILE__), "fixtures", "full_story.txt"))
- @story = Story.new.parse(story_lines)
+ slurper = Slurper.new(File.join(File.dirname(__FILE__), "fixtures", "full_story.slurper"))
+ slurper.yamlize_story_file
+ @story = slurper.stories.first
end
it "parses the name correctly" do
@story.name.should == "Profit"
end
it "parses the description correctly" do
- @story.description.should == "In order to do something\nAs a role\nI want to click a thingy\n\nAcceptance:\n* do the thing\n* don't forget the other thing\n"
+ @story.description.should == "In order to do something\nAs a role\nI want to click a thingy\n\nAcceptance:\n- do the thing\n- don't forget the other thing\n"
end
it "parses the label correctly" do
@@ -49,8 +46,9 @@
context "given only a name" do
before do
- story_lines = IO.readlines(File.join(File.dirname(__FILE__), "fixtures", "name_only.txt"))
- @story = Story.new.parse(story_lines)
+ slurper = Slurper.new(File.join(File.dirname(__FILE__), "fixtures", "name_only.slurper"))
+ slurper.yamlize_story_file
+ @story = slurper.stories.first
end
it "should parse the name correctly" do
@@ -61,27 +59,29 @@
context "given empty attributes" do
before do
- story_lines = IO.readlines(File.join(File.dirname(__FILE__), "fixtures", "empty_attributes.txt"))
- @story = Story.new.parse(story_lines)
+ slurper = Slurper.new(File.join(File.dirname(__FILE__), "fixtures", "empty_attributes.slurper"))
+ slurper.yamlize_story_file
+ @story = slurper.stories.first
end
it "should not set any name" do
- @story.attributes.keys.should_not include("name")
+ @story.name.should be_nil
end
it "should not set any description" do
- @story.attributes.keys.should_not include("description")
+ @story.description.should be_nil
end
it "should not set any labels" do
- @story.attributes.keys.should_not include("labels")
+ @story.labels.should be_nil
end
end
context "given attributes with spaces" do
before do
- story_lines = IO.readlines(File.join(File.dirname(__FILE__), "fixtures", "quoted_attributes.txt"))
- @story = Story.new.parse(story_lines)
+ slurper = Slurper.new(File.join(File.dirname(__FILE__), "fixtures", "quoted_attributes.slurper"))
+ slurper.yamlize_story_file
+ @story = slurper.stories.first
end
it "should set the description correctly" do

0 comments on commit 5d2a8c7

Please sign in to comment.