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
f07639d
commit e0739b1
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
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,37 @@ | ||
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_and_publish id | ||
# database operation to retrieve data. We'll simulate it for now. | ||
post = POSTS.find { |post| post.id == id } || NullPost.new | ||
post.publish | ||
end | ||
|
||
def publish | ||
@published = true | ||
end | ||
|
||
end | ||
|
||
class NullPost | ||
def publish | ||
# noop | ||
end | ||
end | ||
|
||
POSTS = [ | ||
Post.new( | ||
1, | ||
"Introduce Null Object Pattern", | ||
"Post body should be here", | ||
Time.new(2013,01,25) | ||
) | ||
] | ||
|
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 @@ | ||
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_and_publish id | ||
# database operation to retrieve data. We'll simulate it for now. | ||
post = POSTS.find { |post| post.id == id } | ||
post.publish unless post.nil? | ||
end | ||
|
||
def publish | ||
@published = true | ||
end | ||
|
||
end | ||
|
||
POSTS = [ | ||
Post.new( | ||
1, | ||
"Introduce Null Object Pattern", | ||
"Post body should be here", | ||
Time.new(2013,01,25) | ||
) | ||
] | ||
|
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 |
---|---|---|
@@ -1,5 +1,15 @@ | ||
require 'minitest/spec' | ||
require 'minitest/autorun' | ||
require 'minitest/spec' | ||
|
||
require 'before' if ENV["BEFORE"] | ||
require 'after' unless ENV["BEFORE"] | ||
|
||
describe Post do | ||
it "is publishable" do | ||
Post.find_and_publish(1).must_equal true | ||
end | ||
|
||
it "does nothing if post is not found" do | ||
Post.find_and_publish(0) | ||
end | ||
end |