Class: Couchup::Commands::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/couchup/commands/create.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.describeObject



36
37
38
# File 'lib/couchup/commands/create.rb', line 36

def self.describe
  "Creates a new database and switches to the database if using create :database, :foo or a new view if using create :view,'Rider/by_number'"
end

Instance Method Details

#create_view(name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/couchup/commands/create.rb', line 14

def create_view(name)
  raise "Please set your EDITOR env variable before using view" if ENV['EDITOR'].nil? 
  view = ::Couchup::View.new(name)  
  file = Tempfile.new(name.gsub("/", "_"))
  tmp_file_path = file.path
  file.write("------BEGIN Map-------\n")
  file.write( view.map? ? view.map : ::Couchup::View::MAP_TEMPLATE)
  file.write("\n------END------\n")
  
  file.write("\n------BEGIN Reduce(Remove the function if you don't want a reduce)-------\n")
  file.write(view.reduce? ? view.reduce: ::Couchup::View::REDUCE_TEMPLATE )
  file.write("\n------END------\n")
  file.write("\nDelete Everything to do nothing.\n")
  file.close
  
  `#{ENV['EDITOR']} #{tmp_file_path}`
  contents = File.open(tmp_file_path).read
  ::Couchup::View.create(name, contents) unless contents.blank?
  file.close
  file.unlink
end

#run(*params) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/couchup/commands/create.rb', line 4

def run(*params)
  what = params.shift.to_s
  if(what == 'view')
    create_view(params.first)
  else
    Couchup.server.database!(params.first)
    Use.new.run(params.first)
  end
end