Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Model#after_find hook specs and implementation

  • Loading branch information...
commit 7a8c72179af2c136c89b18694b5d036dd3af5eb5 1 parent 66812ee
@seanreads seanreads authored
View
9 lib/her/model/hooks.rb
@@ -35,6 +35,9 @@ def before_update(method=nil, &block); set_hook(:before, :update, method || bloc
# @param [Symbol, &block] method A method or a block to be called
def before_destroy(method=nil, &block); set_hook(:before, :destroy, method || block); end
+ # Do not add a *before find* callback. Only *after find* is supported.
+ def before_find(method=nil, &block); raise NoMethodError, "undefined method `before_find' for #{self}"; end
+
# Add a *after save* callback. Triggered after a resource is created or updated.
# @param [Symbol, &block] method A method or a block to be called
def after_save(method=nil, &block); set_hook(:after, :save, method || block); end
@@ -51,11 +54,15 @@ def after_update(method=nil, &block); set_hook(:after, :update, method || block)
# @param [Symbol, &block] method A method or a block to be called
def after_destroy(method=nil, &block); set_hook(:after, :destroy, method || block); end
+ # Add a *after find* callback. Triggered after a resource is found.
+ # @param [Symbol, &block] method A method or a block to be called
+ def after_find(method=nil, &block); set_hook(:after, :find, method || block); end
+
# Wrap a block between “before” and “after” hooks
# @private
def wrap_in_hooks(resource, *hooks)
perform_before_hooks(resource, *hooks)
- yield(resource, resource.class)
+ yield(resource, resource.class) if block_given?
perform_after_hooks(resource, *hooks.reverse)
end
View
11 lib/her/model/orm.rb
@@ -23,7 +23,11 @@ def initialize(params={})
# Initialize a collection of resources
# @private
def self.initialize_collection(klass, parsed_data={})
- collection_data = parsed_data[:data].map { |item_data| klass.new(item_data) }
+ collection_data = parsed_data[:data].map do |item_data|
+ resource = klass.new(item_data)
+ klass.wrap_in_hooks(resource, :find)
+ resource
+ end
Her::Collection.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
end
@@ -204,9 +208,12 @@ def new_collection(parsed_data)
def find(*ids)
params = ids.last.is_a?(Hash) ? ids.pop : {}
results = ids.flatten.compact.uniq.map do |id|
+ resource = nil
request(params.merge(:_method => :get, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
- new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
+ resource = new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
+ wrap_in_hooks(resource, :find)
end
+ resource
end
if ids.length > 1 || ids.first.kind_of?(Array)
results
View
131 spec/model/hooks_spec.rb
@@ -32,6 +32,12 @@
Foo::User.hooks[:before_destroy].first.class.should == Symbol
end
+ it "does not handle “before find” method hooks" do
+ expect {
+ Foo::User.before_find :set_internal_id
+ }.should raise_error(NoMethodError)
+ end
+
it "handles “after save” method hooks" do
Foo::User.after_save :set_internal_id
Foo::User.hooks[:after_save].length.should == 1
@@ -55,6 +61,12 @@
Foo::User.hooks[:after_destroy].length.should == 1
Foo::User.hooks[:after_destroy].first.class.should == Symbol
end
+
+ it "handles “after find” method hooks" do
+ Foo::User.after_find :set_internal_id
+ Foo::User.hooks[:after_find].length.should == 1
+ Foo::User.hooks[:after_find].first.class.should == Symbol
+ end
end
describe "block hooks" do
@@ -82,6 +94,12 @@
Foo::User.hooks[:before_destroy].first.class.should == Proc
end
+ it "does not handle “before find” block hooks" do
+ expect {
+ Foo::User.before_find :set_internal_id
+ }.should raise_error(NoMethodError)
+ end
+
it "handles “after save” block hooks" do
Foo::User.after_save { |record| record.internal_id = 42 }
Foo::User.hooks[:after_save].length.should == 1
@@ -105,6 +123,12 @@
Foo::User.hooks[:after_destroy].length.should == 1
Foo::User.hooks[:after_destroy].first.class.should == Proc
end
+
+ it "handles “after find” block hooks" do
+ Foo::User.after_find { |record| record.internal_id = 42 }
+ Foo::User.hooks[:after_find].length.should == 1
+ Foo::User.hooks[:after_find].first.class.should == Proc
+ end
end
context "inheriting hooks from a superclass" do
@@ -125,8 +149,10 @@
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
+ stub.get("/users") { |env| [200, {}, [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json] }
stub.post("/users") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
+ stub.get("/users/2") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke" }.to_json] }
stub.put("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
stub.delete("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
end
@@ -135,6 +161,7 @@
spawn_model "Foo::User" do
attr_accessor :internal_save_id, :internal_create_id, :internal_update_id, :internal_destroy_id
attr_accessor :internal_after_save_id, :internal_after_create_id, :internal_after_update_id, :internal_after_destroy_id
+ attr_accessor :internal_after_find_id
def change_internal_save_id; @internal_save_id = 100; end
def change_internal_create_id; @internal_create_id = 101; end
@@ -145,6 +172,8 @@ def change_internal_after_save_id; @internal_after_save_id = 100; end
def change_internal_after_create_id; @internal_after_create_id = 101; end
def change_internal_after_update_id; @internal_after_update_id = 102; end
def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
+
+ def change_internal_after_find_id; @internal_after_find_id = 104; end
end
end
@@ -159,6 +188,8 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
Foo::User.after_update :change_internal_after_update_id
Foo::User.after_create :change_internal_after_create_id
Foo::User.after_destroy :change_internal_after_destroy_id
+
+ Foo::User.after_find :change_internal_after_find_id
end
it "perform “before save” “before create” method hook on Model#save without an ID" do
@@ -167,6 +198,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_save_id.should == 100
@user.internal_create_id.should == 101
@user.internal_update_id.should == nil
+ @user.internal_after_find_id.should == nil
end
it "perform “before save” and “before update” method hook on Model#save with an ID" do
@@ -175,6 +207,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_save_id.should == 100
@user.internal_create_id.should == nil
@user.internal_update_id.should == 102
+ @user.internal_after_find_id.should == 104
end
it "perform “before destroy” method hook on Model#destroy" do
@@ -184,6 +217,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_create_id.should == nil
@user.internal_update_id.should == nil
@user.internal_destroy_id.should == 103
+ @user.internal_after_find_id.should == 104
end
it "perform “after save” “after create” method hook on Model#save without an ID" do
@@ -192,6 +226,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == 101
@user.internal_after_update_id.should == nil
+ @user.internal_after_find_id.should == nil
end
it "perform “after save” “after update” method hook on Model#save with an ID" do
@@ -200,6 +235,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == nil
@user.internal_after_update_id.should == 102
+ @user.internal_after_find_id.should == 104
end
it "perform “after save” “after update” method hook on Model.save_existing" do
@@ -207,6 +243,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == nil
@user.internal_after_update_id.should == 102
+ @user.internal_after_find_id.should == nil
end
it "perform “after save” “after create” method hook on Model.create" do
@@ -214,6 +251,38 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == 101
@user.internal_after_update_id.should == nil
+ @user.internal_after_find_id.should == nil
+ end
+
+ it "perform “after find” method hook on Model.find" do
+ @user = Foo::User.find(1)
+ @user.internal_after_find_id.should == 104
+ @user.internal_save_id.should == nil
+ @user.internal_create_id.should == nil
+ @user.internal_update_id.should == nil
+ @user.internal_destroy_id.should == nil
+ end
+
+ it "perform “after find” method hook on Model#find with IDs" do
+ @users = Foo::User.find([1,2])
+ @users.each do |user|
+ user.internal_after_find_id.should == 104
+ user.internal_save_id.should == nil
+ user.internal_create_id.should == nil
+ user.internal_update_id.should == nil
+ user.internal_destroy_id.should == nil
+ end
+ end
+
+ it "perform “after find” method hook on Model.all" do
+ @users = Foo::User.all
+ @users.each do |user|
+ user.internal_after_find_id.should == 104
+ user.internal_save_id.should == nil
+ user.internal_create_id.should == nil
+ user.internal_update_id.should == nil
+ user.internal_destroy_id.should == nil
+ end
end
end
@@ -228,6 +297,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
Foo::User.after_create { |record| record.internal_after_create_id = 201 }
Foo::User.after_update { |record| record.internal_after_update_id = 202 }
Foo::User.after_destroy { |record| record.internal_after_destroy_id = 203 }
+ Foo::User.after_find { |record| record.internal_after_find_id = 204 }
end
it "perform “before save” and “before create” block hook on Model#save without an ID" do
@@ -236,6 +306,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_save_id.should == 200
@user.internal_create_id.should == 201
@user.internal_update_id.should == nil
+ @user.internal_after_find_id.should == nil
end
it "perform “before save” and “before update” block hook on Model#save with an ID" do
@@ -244,6 +315,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_save_id.should == 200
@user.internal_create_id.should == nil
@user.internal_update_id.should == 202
+ @user.internal_after_find_id.should == 204
end
it "perform “before destroy” block hook on Model#destroy" do
@@ -253,6 +325,7 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_create_id.should == nil
@user.internal_update_id.should == nil
@user.internal_destroy_id.should == 203
+ @user.internal_after_find_id.should == 204
end
it "perform “after save” “after create” block hook on Model#save without an ID" do
@@ -261,15 +334,73 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
@user.internal_after_save_id.should == 200
@user.internal_after_create_id.should == 201
@user.internal_after_update_id.should == nil
+ @user.internal_after_find_id.should == nil
end
it "perform “after save” “after update” block hook on Model#save with an ID" do
@user = Foo::User.find(1)
@user.save
+ @user.internal_after_find_id.should == 204
@user.internal_after_save_id.should == 200
@user.internal_after_create_id.should == nil
@user.internal_after_update_id.should == 202
end
+
+ it "perform “after find” block hook on Model#find" do
+ @user = Foo::User.find(1)
+ @user.internal_after_find_id.should == 204
+ @user.internal_save_id.should == nil
+ @user.internal_create_id.should == nil
+ @user.internal_update_id.should == nil
+ end
+
+ it "perform “after find” method hook on Model#find with IDs" do
+ @users = Foo::User.find([1,2])
+ @users.each do |user|
+ user.internal_after_find_id.should == 204
+ user.internal_save_id.should == nil
+ user.internal_create_id.should == nil
+ user.internal_update_id.should == nil
+ end
+ end
+
+ it "perform “after find” method hook on Model.all" do
+ @users = Foo::User.all
+ @users.each do |user|
+ user.internal_after_find_id.should == 204
+ user.internal_save_id.should == nil
+ user.internal_create_id.should == nil
+ user.internal_update_id.should == nil
+ end
+ end
+
+ it "perform “after find” block hook on Model#find" do
+ @user = Foo::User.find(1)
+ @user.internal_after_find_id.should == 204
+ @user.internal_save_id.should == nil
+ @user.internal_create_id.should == nil
+ @user.internal_update_id.should == nil
+ end
+
+ it "perform “after find” method hook on Model#find with IDs" do
+ @users = Foo::User.find([1,2])
+ @users.each do |user|
+ user.internal_after_find_id.should == 204
+ user.internal_save_id.should == nil
+ user.internal_create_id.should == nil
+ user.internal_update_id.should == nil
+ end
+ end
+
+ it "perform “after find” method hook on Model.all" do
+ @users = Foo::User.all
+ @users.each do |user|
+ user.internal_after_find_id.should == 204
+ user.internal_save_id.should == nil
+ user.internal_create_id.should == nil
+ user.internal_update_id.should == nil
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.