Permalink
Browse files

refined examples a bit

  • Loading branch information...
1 parent 83a1daa commit 92082a2ab612d86d6d592560b99fbee77883118b @napcs committed Sep 9, 2011
Showing with 71 additions and 107 deletions.
  1. +8 −17 06_before_save/01_methods.rb
  2. +7 −15 06_before_save/02_lambdas.rb
  3. +22 −33 06_before_save/03_classes.rb
  4. +34 −42 06_before_save/04_modularized.rb
View
25 06_before_save/01_methods.rb
@@ -11,7 +11,7 @@
#
# When we call
#
-# p = Person.save
+# p = Person.new
# p.save
#
# then we want <tt>foo</tt> to fire, then <tt>bar</tt>.
@@ -26,19 +26,13 @@
require 'rubygems'
require 'test/unit'
-require 'mocha'
-
class BeforeSaveTest < Test::Unit::TestCase
- def test_should_invoke_defined_bar_method_when_specified
-
- Person.send :before_save, :bar
-
+ def test_should_invoke_method_before_save_to_set_
p = Person.new
- p.expects(:bar)
p.save
-
+ assert p.active
end
end
@@ -55,24 +49,21 @@ def self.before_save(*args)
def save
self.class.callbacks.each do |callback|
- self.send callback if self.respond_to? callback
+ self.send callback if self.respond_to? callback
end
puts "Saved"
end
end
class Person < Record
- attr_accessor :name, :approve
- before_save :foo, :bar
+ attr_accessor :name, :active
+ before_save :set_active
- def foo
- puts "called foo before save"
+ def set_active
+ self.active = true
end
- def bar
- puts "called bar before save"
- end
end
p = Person.new
View
22 06_before_save/02_lambdas.rb
@@ -24,26 +24,18 @@
require 'rubygems'
require 'test/unit'
-require 'mocha'
class BeforeSaveTest < Test::Unit::TestCase
- def test_should_invoke_defined_bar_method_when_specified
-
- Person.send :before_save, :bar
-
+ def test_should_invoke_method_before_save_to_set_
p = Person.new
- p.expects(:bar)
p.save
-
+ assert p.active
end
- def test_should_take_lambda
- Person.send :before_save, lambda{|p| p.name = "test"}
-
+ def test_should_take_lambda_that_sets_name_to_test
p = Person.new
- assert_nil p.name
p.save
assert_equal "test", p.name
end
@@ -73,11 +65,11 @@ def save
end
class Person < Record
- attr_accessor :name
- before_save :foo, :bar, lambda{|p| p.name = "test"}
+ attr_accessor :name, :active
+ before_save :set_active, lambda{|p| p.name = "test"}
- def foo
- puts "called foo before save"
+ def set_active
+ self.active = true
end
end
View
55 06_before_save/03_classes.rb
@@ -15,45 +15,25 @@
require 'rubygems'
require 'test/unit'
-require 'mocha'
-
class BeforeSaveTest < Test::Unit::TestCase
- def test_should_invoke_defined_bar_method_when_specified
- Person.send :before_save, :bar
-
-
+ def test_should_invoke_method_before_save_to_set_
p = Person.new
- p.expects(:bar)
p.save
-
+ assert p.active
end
- def test_should_take_lambda
- Person.send :before_save, lambda{|p| p.name = "test"}
-
+ def test_should_take_lambda_that_sets_name_to_test
p = Person.new
- assert_nil p.name
p.save
assert_equal "test", p.name
end
- def test_should_take_a_class
- Person.send :before_save, SuperFilter
- p = Person.new
- p.save
- assert_not_nil p.updated_at
- end
-
- def test_should_take_method_lambda_and_class
- Person.send :before_save, :foo, lambda{|p| p.name = "test"}, SuperFilter
+ def test_should_use_a_class_to_set_updated_at_field
p = Person.new
- p.expects(:foo)
p.save
- assert "test", p.name
assert_not_nil p.updated_at
-
end
end
@@ -79,23 +59,32 @@ def save
end
end
-class SuperFilter
+class LogFilter
def self.call(object)
- object.updated_at = Time.now if object.respond_to? :updated_at
- self.log(object)
+ LogFilter.new(object)
+ end
+
+ def initialize(object)
+ @object = object
+ log
+ set_updated_at
+ end
+
+ def set_updated_at
+ @object.updated_at = Time.now if @object.respond_to? :updated_at
end
- def self.log(object)
- puts "Called Superfilter on #{object.class.to_s}"
+ def log
+ puts "Called LogFilter on #{@object.class.to_s}"
end
end
class Person < Record
- attr_accessor :name, :updated_at
- before_save :foo, :bar, lambda{|p| p.name = "test"}, SuperFilter
+ attr_accessor :name, :active, :updated_at
+ before_save :set_active, lambda{|p| p.name = "test"}, LogFilter
- def foo
- puts "called foo before save"
+ def set_active
+ self.active = true
end
end
View
76 06_before_save/04_modularized.rb
@@ -56,62 +56,34 @@
class BeforeSaveTest < Test::Unit::TestCase
-
- def test_should_invoke_defined_bar_method_when_specified
-
- Person.send :before_save, :bar
-
+
+ def test_should_invoke_method_before_save_to_set_
p = Person.new
- p.expects(:bar)
p.save
-
+ assert p.active
end
-
- def test_should_take_lambda
- Person.send :before_save, lambda{|p| p.name = "test"}
-
+
+ def test_should_take_lambda_that_sets_name_to_test
p = Person.new
- assert_nil p.name
p.save
assert_equal "test", p.name
end
-
- def test_should_take_a_class
- Person.send :before_save, SuperFilter
+
+ def test_should_use_a_class_to_set_updated_at_field
p = Person.new
p.save
assert_not_nil p.updated_at
end
-
- def test_should_take_method_lambda_and_class
- Person.send :before_save, :foo, lambda{|p| p.name = "test"}, SuperFilter
- p = Person.new
- p.expects(:foo)
- p.save
- assert "test", p.name
- assert_not_nil p.updated_at
- end
-
end
+
class Record
def save
puts "Saved!"
end
end
-class SuperFilter
- def self.call(object)
- object.updated_at = Time.now if object.respond_to? :updated_at
- self.log(object)
- end
-
- def self.log(object)
- puts "Called Superfilter on #{object.class.to_s}"
- end
-end
-
module BeforeSaveCallbacks
module ClassMethods
@@ -144,15 +116,35 @@ def save
Record.send :include, BeforeSaveCallbacks
+class LogFilter
+ def self.call(object)
+ LogFilter.new(object)
+ end
+
+ def initialize(object)
+ @object = object
+ log
+ set_updated_at
+ end
+
+ def set_updated_at
+ @object.updated_at = Time.now if @object.respond_to? :updated_at
+ end
+
+ def log
+ puts "Called LogFilter on #{@object.class.to_s}"
+ end
+end
+
##################
class Person < Record
- attr_accessor :name, :updated_at
- before_save :foo, lambda{ puts "called lambda at #{Time.now}" }, SuperFilter
-
- def foo
- puts "called foo before save"
- end
+ attr_accessor :name, :active, :updated_at
+ before_save :set_active, lambda{|p| p.name = "test"}, LogFilter
+
+ def set_active
+ self.active = true
+ end
end
p = Person.new

0 comments on commit 92082a2

Please sign in to comment.