Created
April 11, 2014 12:45
-
-
Save sideshowcoder/10465780 to your computer and use it in GitHub Desktop.
Setting up couchbase views programmatically via Rak
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
{ | |
"_id": "_design/teams", | |
"language": "javascript", | |
"views": { | |
"all": { | |
"map": "function(doc, meta){ if(doc.type === 'team'){ emit(meta.id, null); } }" | |
} | |
} | |
} |
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
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