Skip to content

Commit

Permalink
Doc additions
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed Sep 11, 2009
1 parent a79ae92 commit e618397
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
27 changes: 19 additions & 8 deletions lib/recliner/document.rb
Expand Up @@ -53,17 +53,19 @@ def save!
create_or_update || raise(DocumentNotSaved)
end

#
# Updates all the attributes from the passed-in Hash and saves the document. If the object is invalid, the saving will
# fail and false will be returned.
def update_attributes(attrs)
self.attributes = attrs and save
end

# Deletes the document in the database and marks the instance as
# read-only to reflect that no changes should be made (since they
# can't be persisted). Returns the deleted instance.
#
def destroy
delete
end

#
# To enforce the object's +before_destroy+ and +after_destroy+
# callbacks, Observer methods, or any <tt>:dependent</tt> association
# options, use <tt>#destroy</tt>.
def delete
begin
database.delete("#{id}?rev=#{rev}") unless new_record?
Expand All @@ -75,6 +77,13 @@ def delete
self
end

# Deletes the document in the database and marks the instance as
# read-only to reflect that no changes should be made (since they
# can't be persisted).
def destroy
delete
end

# Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet; otherwise, returns false.
def new_record?
@new_record || false
Expand Down Expand Up @@ -187,6 +196,7 @@ def database
Thread.current["#{name}_database"] || default_database
end

#
def default_database
@default_database ||= Database.new(read_inheritable_attribute(:database_uri))
end
Expand All @@ -199,7 +209,8 @@ def with_database(db)
ensure
Thread.current["#{name}_database"] = nil
end


#
def instantiate_from_database(attrs)
unless attrs['class'] && (self == Document || attrs['class'] == name)
raise DocumentNotFound
Expand All @@ -218,7 +229,7 @@ def instantiate_from_database(attrs)
end
end

def self_and_descendants_from_recliner#nodoc:
def self_and_descendants_from_recliner#:nodoc:
klass = self
classes = [klass]
while klass.superclass != Document
Expand Down
24 changes: 13 additions & 11 deletions lib/recliner/pretty_inspect.rb
Expand Up @@ -2,17 +2,6 @@ module Recliner
module PrettyInspect
extend ActiveSupport::Concern

def inspect
"#<#{self.class.name} #{attributes_for_inspect}>"
end

def attributes_for_inspect
attrs = self.class.model_properties.map { |name, property| "#{name}: #{send(name).inspect}" }
attrs.unshift "rev: #{rev}" if rev
attrs.unshift "id: #{id}"
attrs * ', '
end

module ClassMethods
# Returns a string like 'Post(title:String, body:String)'
def inspect
Expand All @@ -24,5 +13,18 @@ def inspect
end
end
end

# Returns the contents of the document as a nicely formatted string.
def inspect
"#<#{self.class.name} #{attributes_for_inspect}>"
end

private
def attributes_for_inspect
attrs = self.class.model_properties.map { |name, property| "#{name}: #{send(name).inspect}" }
attrs.unshift "rev: #{rev}" if rev
attrs.unshift "id: #{id}"
attrs * ', '
end
end
end

0 comments on commit e618397

Please sign in to comment.