Created
March 25, 2010 18:35
-
-
Save mkeen/343936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Throne::MultiDocument < Hashie::Mash | |
class NotFound < StandardError; end | |
## Class methods | |
class << self | |
# Sets the database that the class will use | |
def database(name = nil) | |
if name.nil? | |
@database || :default | |
else | |
raise Throne::Database::NotSetup, "ensure that you've setup your database before using it in a class definition" unless Throne::Database.setup?(name) | |
@database = name | |
end | |
end | |
end | |
## Instance methods | |
def initialize(attributes = {:docs => []}) | |
#self[:ruby_class] = self.class.name | |
super(attributes) | |
end | |
# Persist a document set to the database | |
# @param [Hash] The document properties | |
# @return [Hash] | |
def save(attributes = {}) | |
data = {:database => self.class.database, :resource => "_bulk_docs"}.merge normalise(attributes).to_hash | |
response = Throne::Request.post data | |
end | |
private | |
def normalise(hash) | |
# Merge the hash with self | |
merge!(hash) unless hash.nil? | |
# Rename id and rev to _id and _rev | |
%w(id rev).each do |attribute| | |
self[:"_#{attribute}"] = delete attribute if key? attribute | |
end | |
# Remove ok | |
delete("ok") | |
self | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Throne::MultiDocument.database :my_database | |
batched_docs = Throne::MultiDocument.new | |
syncpoint = Throne::Document.new({ | |
:attrib => "value", | |
:another => "value | |
}) | |
batched_docs['docs'] << syncpoint | |
batched_docs.save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment