Skip to content

Instantly share code, notes, and snippets.

@sideshowcoder
Created April 11, 2014 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideshowcoder/10465780 to your computer and use it in GitHub Desktop.
Save sideshowcoder/10465780 to your computer and use it in GitHub Desktop.
Setting up couchbase views programmatically via Rak
{
"_id": "_design/teams",
"language": "javascript",
"views": {
"all": {
"map": "function(doc, meta){ if(doc.type === 'team'){ emit(meta.id, null); } }"
}
}
}
require "couchbase"
require "dotenv/tasks" # used to manage environment variables
APP_ROOT = File.expand_path File.dirname(__FILE__)
class CB
# create a connection to Couchbase
# RACK_ENV to be set to the current environment to be use
# COUCHBASE_DEVLEOPMENT to contain a couchbase connection url OR
# COUCHBASE_PRODUCTION to contain a couchbase connection url OR
# COUCHBASE_TEST to contain a couchbase connection url
def self.connection
db_url_env = "COUCHBASE_#{ENV["RACK_ENV"].upcase}"
@@connection ||= Couchbase.connect ENV[db_url_env]
end
end
desc "setup all needed views (run for each used environment using env RACK_ENV=environment"
task :views => [:dotenv] do
views_path = "#{APP_ROOT}/config/couchbase_views/"
views_dir = Dir.new(views_path)
views_dir.each do |f|
next if [".", ".."].include? f
path = "#{views_path}/#{f}"
puts "creating #{f} view..."
if CB.connection.save_design_doc File.read(path)
puts "success"
else
puts "failed"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment