Skip to content

Instantly share code, notes, and snippets.

@scalabl3
scalabl3 / gist:3161434
Created July 23, 2012 00:09
Automatic Versioning
#Current key is always the most current version of the document. Everytime it gets modified, we increase the count of versions and save current couchbase version of document #as a version with current_version - 1 appended.
#So:
#create_doc
#mydoc = { "txt" => "my first text" }
#replace_doc
#mydoc::version = 2
#mydoc::version::1 = { "txt" => "my_first_text", "auto_version" => 1, "auto_version_stamp" => "6/28/2012 20:35" }
@scalabl3
scalabl3 / gist:3161435
Created July 23, 2012 00:09
Even better than has_key?, is has_key_and_value?
class Hash
# if key doesn't exist, returns false
# if self[key] == false, returns false
# if self[key] == nil, empty, or blank, returns false
# if self[key] exists and self[key] == true, or evaluates to true, returns true
def has_key_and_value? (key)
return false unless has_key?(key)
return false if self[key].nil?
return false if self[key].respond_to?(:empty?) && self[key].empty?