lib/couchpillow/document.rb in couchpillow-0.4.8 vs lib/couchpillow/document.rb in couchpillow-0.4.9

- old
+ new

@@ -50,11 +50,11 @@ @id = self.class._is_type_prefixed? ? self.class._sanitize_id(id) : id time = Time.now.utc @data[:_created_at] ||= time - @data[:_updated_at] = time + @data[:_updated_at] ||= time @cas = cas rename! whitelist! @@ -257,23 +257,34 @@ # # @return nil if not found or Document is of a different type. # If multiple ids are passed, returns an array. # def self.get *ids - results = ids.map do |id| - id = _sanitize_id(id) + results = [] + if ids.length == 1 + id = _sanitize_id(ids.first) tid = _is_type_prefixed? ? "#{_doc_type}::#{id}" : id result, _, cas = _default_db.get(tid, extended: true) - result and - type = result[:_type] || result["_type"] and - type == _doc_type and - new(result, id, cas) or - nil + results << create(result, id, cas) + + elsif ids.length > 1 + # Sanitize ids first + sanitized_ids = ids.map do |id| + id = _sanitize_id(id) + _is_type_prefixed? ? "#{_doc_type}::#{id}" : id + end + + # Query the db + db_result = _default_db.get(*sanitized_ids, extended: true) + results = sanitized_ids.map do |k| + result, _, cas = db_result[k] + create(result, k, cas) + end end - results && results.size == 1 ? results.first : results + results.size == 1 ? results.first : results end # Registers a listener on a specific event. # See {EVENTS} constant for a list of accepted events. @@ -348,9 +359,18 @@ # Get the final id value that will be saved to the database. # def db_id @id = self.class._sanitize_id(@id) self.class._is_type_prefixed? ? "#{doc_type}::#{@id}" : @id + end + + + def self.create result, id, cas + result and + type = result[:_type] || result["_type"] and + type == _doc_type and + new(result, id, cas) or + nil end def self.event_listeners @event_listeners ||= {}