From 464817627aaaef666ff96d29fdfd9947b8c2a038 Mon Sep 17 00:00:00 2001 From: Ben Lavender Date: Mon, 1 Nov 2010 13:42:36 -0500 Subject: [PATCH] Implement after_update --- lib/spira/resource/instance_methods.rb | 1 + spec/hooks.spec | 34 ++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/spira/resource/instance_methods.rb b/lib/spira/resource/instance_methods.rb index de27c22..3b49b2f 100644 --- a/lib/spira/resource/instance_methods.rb +++ b/lib/spira/resource/instance_methods.rb @@ -191,6 +191,7 @@ def update(properties) properties.each do |property, value| attribute_set(property, value) end + after_update if self.respond_to?(:after_update) self end diff --git a/spec/hooks.spec b/spec/hooks.spec index fd00107..dbbb24e 100644 --- a/spec/hooks.spec +++ b/spec/hooks.spec @@ -5,10 +5,17 @@ describe 'Spira resources' do before :all do class ::HookTest < ::Spira::Base property :name, :predicate => FOAF.name + property :age, :predicate => FOAF.age end @subject = RDF::URI.intern('http://example.org/test') end + before :each do + @repository = RDF::Repository.new + @repository << RDF::Statement.new(@subject, RDF::FOAF.name, "A name") + Spira.add_repository(:default, @repository) + end + context "with a before_create method" do before :all do class ::BeforeCreateTest < ::HookTest @@ -27,10 +34,7 @@ describe 'Spira resources' do end before :each do - @repository = RDF::Repository.new @repository << RDF::Statement.new(@subject, RDF.type, RDF::FOAF.bc_test) - @repository << RDF::Statement.new(@subject, RDF::FOAF.name, "A name") - Spira.add_repository(:default, @repository) end it "calls the before_create method before saving a resouce for the first time" do @@ -73,10 +77,7 @@ describe 'Spira resources' do end before :each do - @repository = RDF::Repository.new @repository << RDF::Statement.new(@subject, RDF.type, RDF::FOAF.bc_test) - @repository << RDF::Statement.new(@subject, RDF::FOAF.name, "A name") - Spira.add_repository(:default, @repository) end it "calls the after_create method after saving a resource for the first time" do @@ -101,15 +102,28 @@ describe 'Spira resources' do end end - context "with a before_update method" do - it "calls the before_update method before updating a field" do + context "with an after_update method" do + before :all do + class ::AfterUpdateTest < ::HookTest + def after_update + self.age = 15 + end + end end - end - context "with an after_update method" do it "calls the after_update method after updating a field" do + test = @subject.as(AfterUpdateTest) + test.age.should be_nil + test.update(:name => "A new name") + test.age.should == 15 + end + it "does not call the after_update method after simply setting a field" do + test = @subject.as(AfterUpdateTest) + test.age.should be_nil + test.name = "a new name" + test.age.should be_nil end end