Permalink
Browse files

few changes, for further merb compatibility

* Errors subclass of Hash give method 'on' that used by merb form helpers,
  Hash can be monkey patched but avoided
* added few other methods for more compatibility
  to_param
  new_record?
  id
  one
  • Loading branch information...
1 parent 9123372 commit d1342aadbb8af6f7a0adf8ad1b1b42eb9689ba61 Niket Patel committed Aug 31, 2008
Showing with 18 additions and 1 deletion.
  1. +18 −1 lib/relaxdb/document.rb
View
@@ -61,7 +61,7 @@ def initialize(hash={})
# The default _id will be overwritten if loaded from CouchDB
self._id = UuidGenerator.uuid
- @errors = {}
+ @errors = Errors.new
# Set default properties if this object has not known CouchDB
unless hash["_rev"]
@@ -152,6 +152,12 @@ def validates?
def unsaved?
instance_variable_get(:@_rev).nil?
end
+ alias_method :new_record?, :unsaved?
+
+ def to_param
+ self._id
+ end
+ alias_method :id, :to_param
def set_created_at_if_new
if unsaved? and methods.include? "created_at"
@@ -271,6 +277,12 @@ def self.all_relationships
def self.all
@all_delegator ||= AllDelegator.new(self)
end
+
+ def self.one(id)
+ if id.is_a? String
+ self.all.sorted_by(:_id) {|q| q.key(id) }.first
+ end
+ end
# destroy! nullifies all relationships with peers and children before deleting
# itself in CouchDB
@@ -294,5 +306,10 @@ def destroy!
end
end
+ # END - Document
+
+ class Errors < Hash
+ alias_method :on, :[]
+ end
end

0 comments on commit d1342aa

Please sign in to comment.