Permalink
Browse files

Extracted Migrator#frontmatter method to class

  • Loading branch information...
1 parent 6cf0574 commit 961e48078fbdd4f999267e7f264645e5be7bc51e @natedavisolds committed Feb 13, 2014
View
@@ -1,6 +1,7 @@
require 'wp2middleman/version'
require 'wp2middleman/post'
require 'wp2middleman/post_collection'
+require 'wp2middleman/frontmatter'
require 'wp2middleman/migrator'
require 'wp2middleman/cli'
@@ -0,0 +1,29 @@
+puts "loading"
+
+module WP2Middleman
+ class Frontmatter
+ def initialize post
+ @post = post
+ end
+
+ def data include_fields=[]
+ data = {
+ 'title' => post.title,
+ 'date' => post.date_published,
+ 'tags' => post.tags
+ }
+
+ data['published'] = false if !post.published?
+
+ include_fields.each do |field|
+ data[field] = post.field(field)
+ end
+
+ data
+ end
+
+ private
+
+ attr_reader :post
+ end
+end
@@ -37,19 +37,7 @@ def file_content(post)
end
def frontmatter(post)
- data = {
- 'title' => post.title,
- 'date' => post.date_published,
- 'tags' => post.tags
- }
-
- data['published'] = false if !post.published?
-
- @include_fields.each do |field|
- data[field] = post.field(field)
- end
-
- data
+ Frontmatter.new(post).data(@include_fields)
end
def formatted_post_content(post)
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe WP2Middleman::Migrator do
+ it "exists as a class within the WP2Middleman module" do
+ WP2Middleman::Frontmatter.class.should eq Class
+ end
+
+ def post(attributes = {})
+ defaults = {title: "mytitle", date_published: "mydate", tags: "mytags", published?: false}
+ @post ||= double(defaults.merge attributes)
+ end
+
+ it "includes the title, date, and tags from the post" do
+ frontmatter = WP2Middleman::Frontmatter.new(post).data
+
+ expect(frontmatter["title"]).to eq("mytitle")
+ expect(frontmatter["date"]).to eq("mydate")
+ expect(frontmatter["tags"]).to eq("mytags")
+ end
+
+ it "sets published to false for unpublished posts" do
+ frontmatter = WP2Middleman::Frontmatter.new(post).data
+ expect(frontmatter["published"]).to be_false
+ end
+
+ it "sets published to nil for published posts" do
+ frontmatter = WP2Middleman::Frontmatter.new(post published?: true).data
+ expect(frontmatter["published"]).to be_nil
+ end
+
+ it "includes fields specified in include_fields" do
+ allow(post).to receive(:field).with("field1") { "value1" }
+ allow(post).to receive(:field).with("field2") { "value2" }
+ frontmatter = WP2Middleman::Frontmatter.new(post).data(["field1", "field2"])
+ expect(frontmatter["field1"]).to eq("value1")
+ expect(frontmatter["field2"]).to eq("value2")
+ end
+end
@@ -43,39 +43,6 @@
end
end
- describe "#frontmatter" do
- def post(attributes = {})
- defaults = {title: "mytitle", date_published: "mydate", tags: "mytags", published?: false}
- @post ||= double(defaults.merge attributes)
- end
-
- it "includes the title, date, and tags from the post" do
- frontmatter = migrator.frontmatter(post)
- expect(frontmatter["title"]).to eq("mytitle")
- expect(frontmatter["date"]).to eq("mydate")
- expect(frontmatter["tags"]).to eq("mytags")
- end
-
- it "sets published to false for unpublished posts" do
- frontmatter = migrator.frontmatter(post)
- expect(frontmatter["published"]).to be_false
- end
-
- it "sets published to nil for published posts" do
- frontmatter = migrator.frontmatter(post published?: true)
- expect(frontmatter["published"]).to be_nil
- end
-
- it "includes fields specified in include_fields" do
- migrator = WP2Middleman::Migrator.new(file, include_fields: ["field1", "field2"])
- allow(post).to receive(:field).with("field1") { "value1" }
- allow(post).to receive(:field).with("field2") { "value2" }
- frontmatter = migrator.frontmatter(post)
- expect(frontmatter["field1"]).to eq("value1")
- expect(frontmatter["field2"]).to eq("value2")
- end
- end
-
describe "#file_content" do
it "properly formats a post as a Middleman-style post" do
expect(migrator.file_content(migrator.posts[1])).to eq("---\ntitle: A second title\ndate: '2011-07-25'\ntags:\n- some_tag\n- another tag\n- tag\n---\n\n <strong>Foo</strong>\n")

0 comments on commit 961e480

Please sign in to comment.