Skip to content

Commit

Permalink
moved commond error processing in persistence class
Browse files Browse the repository at this point in the history
  • Loading branch information
reborg committed Jan 15, 2010
1 parent 0118b75 commit 364b714
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
35 changes: 34 additions & 1 deletion app/models/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,40 @@ def moc
@moc ||= init_moc
end

private
def self.fetch(request)
error = fetch_error
results = Persistence.instance.moc.executeFetchRequest(request, error:error)
process_error(error)
results
end

##
# Saves an already existing instance to the database.
# FIXME: try to extend process_error to handle true/false
# results as well as exception raising and delete error
# processing from here.
def self.save
error = fetch_error
unless Persistence.instance.moc.save(error)
msg = error[0].localizedDescription ? error[0].localizedDescription : "Unknown"
NSLog("Error while saving entity #{msg}")
return false
end
return true
end

def self.fetch_error
Pointer.new_with_type('@')
end

def self.process_error(error)
unless error[0] == nil
msg = error[0].localizedDescription ? error[0].localizedDescription : "Unknown"
raise "CoreData access error: \n#{msg}"
end
end

private

def init_moc
moc = NSManagedObjectContext.new
Expand Down
32 changes: 4 additions & 28 deletions app/models/persistent_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ def self.entity
end

##
# Saves an already existing instance in the database.
# Saves an already existing instance to the database.
def save
error = PersistentObject.fetch_error
unless Persistence.instance.moc.save(error)
msg = error[0].localizedDescription ? error[0].localizedDescription : "Unknown"
NSLog("Error while saving entity #{msg}")
return false
end
return true
Persistence.save
end

##
Expand Down Expand Up @@ -55,32 +49,14 @@ def self.find_by_text(str)
request.includesPendingChanges = false
request.entity = entity
request.predicate = NSPredicate.predicateWithFormat("%K like %@", "text", str)
error = fetch_error
results = Persistence.instance.moc.executeFetchRequest(request, error:error)
if ((error[0] != nil) || (results == nil))
msg = error[0].localizedDescription ?
error[0].localizedDescription : "Unknown"
puts "Error fetching entity #{msg}"
end
results.first
Persistence.fetch(request).first
end

def self.all
request = NSFetchRequest.new
request.includesPendingChanges = false
request.entity = entity
error = fetch_error
results = Persistence.instance.moc.executeFetchRequest(request, error:error)
if ((error[0] != nil) || (results == nil))
msg = error[0].localizedDescription ?
error[0].localizedDescription : "Unknown"
puts "Error fetching entity #{msg}"
end
results
end

def self.fetch_error
Pointer.new_with_type('@')
Persistence.fetch(request)
end

##
Expand Down
14 changes: 14 additions & 0 deletions spec/models/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def setup
@persistence.moc.registeredObjects.count.should == 0
end

it 'handles errors executing fetch requests' do
moc = stub(:executeFetchRequest => ['results'])
Persistence.instance.stubs(:moc).returns(moc)
Pointer.stubs(:new_with_type).returns(['error'])
Persistence.expects(:fetch).raises
end

it 'handles errors executing save requests' do
moc = stub(:save => true)
Persistence.instance.stubs(:moc).returns(moc)
Pointer.stubs(:new_with_type).returns(['error'])
Persistence.expects(:save).raises
end

def teardown
@persistence.reset
end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/persistent_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ class PersistentObjectSpec < Test::Unit::TestCase

def setup
setup_persistence
@pomodoro = Pomodoro.new
end

it 'should intercept new and return a managed entity' do
@pomodoro = Pomodoro.new(:text => "Main Test Pomo")
@pomodoro.entity.name.should == "Pomodoro"
end

it 'is stored in the database' do
@pomodoro = Pomodoro.new
@pomodoro.text = "Test"
@pomodoro.save
Pomodoro.find_by_text("Test").should_not be_nil
end

it 'retrieve the count of created entities' do
Pomodoro.all.each {|p| puts p.description}
2.times {|i| Pomodoro.create!(:text => "Hi #{i}", :timestamp => NSDate.date) }
Pomodoro.count.should == 2 # FAIL! a 3rd one is created somehow
Pomodoro.count.should == 2
end

def teardown
Expand Down

0 comments on commit 364b714

Please sign in to comment.