Skip to content

Commit

Permalink
Fixed up some error handling for RecordNotSaved.
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Reeder committed Jan 5, 2011
1 parent 7bf1552 commit f21d4d9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 62 deletions.
16 changes: 6 additions & 10 deletions lib/simple_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
require File.expand_path(File.dirname(__FILE__) + "/simple_record/active_sdb")
require File.expand_path(File.dirname(__FILE__) + "/simple_record/callbacks")
require File.expand_path(File.dirname(__FILE__) + "/simple_record/encryptor")
require File.expand_path(File.dirname(__FILE__) + "/simple_record/exceptions")
require File.expand_path(File.dirname(__FILE__) + "/simple_record/errors")
require File.expand_path(File.dirname(__FILE__) + "/simple_record/json")
require File.expand_path(File.dirname(__FILE__) + "/simple_record/logging")
Expand Down Expand Up @@ -602,7 +601,12 @@ def single_clob_id
end

def save!(options={})
save(options) || raise(RecordNotSaved)
save(options) || raise(RecordNotSaved.new(nil, self))
end

# this is a bit wonky, save! should call this, not sure why it's here.
def save_with_validation!(options={})
save!
end

def self.create(attributes={})
Expand All @@ -616,14 +620,6 @@ def self.create!(attributes={})
item
end

def save_with_validation!(options={})
if valid?
save
else
raise RecordInvalid.new(self)
end
end


def self.get_encryption_key()
key = SimpleRecord.options[:encryption_key]
Expand Down
10 changes: 7 additions & 3 deletions lib/simple_record/errors.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
module SimpleRecord

class SimpleRecordError < StandardError

end


class RecordInvalid < SimpleRecordError
class RecordNotSaved < SimpleRecordError
attr_accessor :record

def initialize(record)
def initialize(record=nil)
@record = record
super("Validation failed: #{@record.errors.full_messages.join(", ")}")
end
end

class RecordNotFound < SimpleRecordError

end

class Error
attr_accessor :base, :attribute, :type, :message, :options
Expand Down
6 changes: 0 additions & 6 deletions lib/simple_record/exceptions.rb

This file was deleted.

49 changes: 6 additions & 43 deletions test/temp.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,9 @@
require_relative 'my_model'

class ToSClass
def to_s
"#{self.class.name} to_s called"
end
end
class InspectClass
def inspect
"#{self.class.name} inspect called"
end
end
class BothClass
def inspect
"#{self.class.name} inspect called"
end
def to_s
"#{self.class.name} to_s called"
end
end

c1 = ToSClass.new
puts c1.inspect
puts c1.to_s
c1 = InspectClass.new
puts c1.inspect
puts c1.to_s
c1 = BothClass.new
puts c1.inspect
puts c1.to_s

a = []
afirst = a.first
puts 'afirst=' + afirst.inspect

p = nil
puts 'p=' + p.inspect.to_s
puts "p=" + p.inspect

p = MyModel.new
puts 'p=' + p.inspect.to_s
puts 'p=' + p.to_s
puts "p=" + p.inspect

id = 123
puts 'id&&id ' + (id && id.to_s).inspect
begin
raise StandardError
rescue => ex
p ex
puts ex.message
end

0 comments on commit f21d4d9

Please sign in to comment.