forked from tutsplus/ruby-refactoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0739b1
commit 6315a7a
Showing
8 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class Post | ||
attr_reader :id, :title, :body, :created_at | ||
def initialize id, title, body, created_at | ||
@id = id | ||
@title = title | ||
@body = body | ||
@created_at = created_at | ||
@published = false | ||
end | ||
|
||
def self.find id | ||
# database operation to retrieve data. We'll simulate it for now. | ||
post = POSTS.find { |post| post.id == id } | ||
end | ||
|
||
def self.unpublished | ||
return POSTS.count { |post| !post.published? } | ||
end | ||
|
||
def publish | ||
@published = true | ||
end | ||
|
||
def unpublish | ||
@published = false | ||
end | ||
|
||
def published? | ||
@published | ||
end | ||
|
||
end | ||
|
||
### Sample data | ||
|
||
POSTS = [ | ||
Post.new( | ||
1, | ||
"Introduce Null Object Pattern", | ||
"Post body should be here", | ||
Time.new(2013,01,25) | ||
), | ||
Post.new( | ||
2, | ||
"Introduce Assertion", | ||
"Post body should be here", | ||
Time.new(2012,02,26) | ||
), | ||
Post.new( | ||
3, | ||
"Extract Method", | ||
"Post body should be here", | ||
Time.new(2014,01,27) | ||
), | ||
Post.new( | ||
4, | ||
"Replace Type Code with Polymorphism", | ||
"Post body should be here", | ||
Time.new(2015,10,12) | ||
) | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class Post | ||
attr_reader :id, :title, :body, :created_at | ||
def initialize id, title, body, created_at | ||
@id = id | ||
@title = title | ||
@body = body | ||
@created_at = created_at | ||
@published = false | ||
end | ||
|
||
def self.find id | ||
# database operation to retrieve data. We'll simulate it for now. | ||
post = POSTS.find { |post| post.id == id } | ||
end | ||
|
||
def publish | ||
@published = true | ||
return POSTS.count { |post| !post.published? } | ||
end | ||
|
||
def unpublish | ||
@published = false | ||
end | ||
|
||
def published? | ||
@published | ||
end | ||
|
||
end | ||
|
||
### Sample data | ||
|
||
POSTS = [ | ||
Post.new( | ||
1, | ||
"Introduce Null Object Pattern", | ||
"Post body should be here", | ||
Time.new(2013,01,25) | ||
), | ||
Post.new( | ||
2, | ||
"Introduce Assertion", | ||
"Post body should be here", | ||
Time.new(2012,02,26) | ||
), | ||
Post.new( | ||
3, | ||
"Extract Method", | ||
"Post body should be here", | ||
Time.new(2014,01,27) | ||
), | ||
Post.new( | ||
4, | ||
"Replace Type Code with Polymorphism", | ||
"Post body should be here", | ||
Time.new(2015,10,12) | ||
) | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/spec' | ||
|
||
require 'before' if ENV["BEFORE"] | ||
require 'after' unless ENV["BEFORE"] | ||
|
||
describe Post do | ||
|
||
before :each do | ||
POSTS.each { |post| post.unpublish } | ||
end | ||
|
||
if ENV["BEFORE"] | ||
it "is publishable and retrieves all unpublished posts count" do | ||
post = Post.find(1) | ||
post.publish.must_equal 3 | ||
end | ||
|
||
else # AFTER | ||
|
||
it "is publishable" do | ||
post = Post.find(1) | ||
post.publish.must_equal true | ||
end | ||
|
||
it "retrieves all unpublished posts count" do | ||
Post.unpublished.must_equal 4 | ||
end | ||
|
||
end | ||
end |