Module: CouchRest::Commands::Generate

Defined in:
lib/couchrest/commands/generate.rb

Class Method Summary collapse

Class Method Details

.helpObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/couchrest/commands/generate.rb', line 49

def self.help
  helpstring = "\nUsage: couchview generate directory design1 design2 design3 ...\n\nCouchview will create directories and example views for the design documents you specify.\n\n"
  helpstring.gsub(/^        /, '')
end

.run(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/couchrest/commands/generate.rb', line 7

def self.run(options)
  directory    = options[:directory]
  design_names = options[:trailing_args]

  FileUtils.mkdir_p(directory)
  filename = File.join(directory, "lib.js")
  self.write(filename, "// Put global functions here.\n// Include in your views with\n//\n//   //include-lib\n")

  design_names.each do |design_name|
    subdirectory = File.join(directory, design_name)
    FileUtils.mkdir_p(subdirectory)
    filename = File.join(subdirectory, "sample-map.js")
    self.write(filename, "function(doc) {\n// Keys is first letter of _id\nemit(doc._id[0], doc);\n}\n")

    filename = File.join(subdirectory, "sample-reduce.js")
    self.write(filename, "function(keys, values) {\n// Count the number of keys starting with this letter\nreturn values.length;\n}\n")

    filename = File.join(subdirectory, "lib.js")
    self.write(filename, "// Put functions specific to '\#{design_name}' here.\n// Include in your views with\n//\n//   //include-lib\n")
  end
end

.write(filename, contents) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/couchrest/commands/generate.rb', line 60

def self.write(filename, contents)
  puts "Writing #{filename}"
  File.open(filename, "w") do |f|
    # Remove leading spaces
    contents.gsub!(/^        (  )?/, '')
    f.write contents
  end
end