Document Store Module

Simple module to use a single shared connection, of course I have made my personal one very robust, but these are easy examples.

require 'couchbase'

module DocumentStore
  C ="http://localhost:8091/")
  C.quiet = true                                # return nil instead of Exceptions for missing keys

Object <> Document

Simple Models are a direct and a simple relationship between Object and Couchbase Document.
Let's look at a class definition and methods to map back and forth:

class User
  include DocumentStore
  attr_accessor :name, :email, :password, :zip
  # iterate through attr keys and set instance vars
  def initialize(attr = {})
    unless attr.nil?
      attr.each do |name, value|
        setter = "#{name}="
        next unless respond_to?(setter)
        send(setter, value)

  # iterate through instance variables and convert keypairs to hash
  def to_hash
    Hash[ { |name| [name[].to_s, instance_variable_get(name)] } ]
  # save object to Couchbase, with the email as the key, normally we use a common module 
  # for the connection as only a single connection is required for an entire app server
  def save    
    C.set(@email.downcase, to_hash)
  class << self
    include DocumentStore

    # find user by email 
    def self.find_by_email(email)    
      C.quiet = true                # => don't throw exception for missing keys
      doc = C.get(email.downcase)   # => try to find key
      return nil if doc.nil?        # => return nil unless key exists 
      return          # => return User object (doc will be a hash)

In this basic class, we have 4 simple methods, initialize, to_hash, save and a class method to find a user by email.

Model in Action

Instantiating, saving and retrieving objects is simple and straightforward.

# of course you could get this from submitted params instead
u_hash = { 
    "name" => "John Smith", 
    "email" => "", 
    "password" => "test",
    "zip" => "94101" }
u =                            # => Create a new user                                          # => Save to Couchbase 
# k:
# v: { "name": "John Smith", "email": "", "password": "test", "zip": "94101" }

john = User.find_by_email("")   # => Retrieve by email
puts john.inspect
# => <User:0x007fef02820ca0 @name="John Smith", @email="", @password="test", @zip="94101">

Q&A and Comments


Q&A and Comments

comments powered by Disqus