Skip to content

Commit

Permalink
Separate Query from Modifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
josemotanet committed Jun 4, 2013
1 parent e0739b1 commit 6315a7a
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 5 deletions.
Empty file removed move-method/lib/after.rb
Empty file.
Empty file removed move-method/lib/before.rb
Empty file.
5 changes: 0 additions & 5 deletions move-method/test/test.rb

This file was deleted.

Binary file added separate-query-from-modifier/.DS_Store
Binary file not shown.
File renamed without changes.
62 changes: 62 additions & 0 deletions separate-query-from-modifier/lib/after.rb
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)
)
]

59 changes: 59 additions & 0 deletions separate-query-from-modifier/lib/before.rb
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)
)
]

31 changes: 31 additions & 0 deletions separate-query-from-modifier/test/test.rb
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

0 comments on commit 6315a7a

Please sign in to comment.