Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Implement after_update
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuga committed Nov 1, 2010
1 parent 49feaea commit 4648176
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/spira/resource/instance_methods.rb
Expand Up @@ -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

Expand Down
34 changes: 24 additions & 10 deletions spec/hooks.spec
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 4648176

Please sign in to comment.