couchup 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,7 @@ Couchup::Commands.constants.each do |c|
14
14
  instance.run(*args)
15
15
  rescue
16
16
  puts $!.inspect
17
+ puts $!.backtrace
17
18
  end
18
19
  end"
19
20
  end
@@ -13,27 +13,28 @@ module Couchup
13
13
 
14
14
  def create_view(name)
15
15
  raise "Please set your EDITOR env variable before using view" if ENV['EDITOR'].nil?
16
- view = View.new(name)
16
+ view = ::Couchup::View.new(name)
17
17
  file = Tempfile.new(name.gsub("/", "_"))
18
18
  tmp_file_path = file.path
19
19
  file.write("------BEGIN Map-------\n")
20
- file.write(view.map) if view.map?
21
- file.write("------END------\n\n")
20
+ file.write( view.map? ? view.map : ::Couchup::View::MAP_TEMPLATE)
21
+ file.write("\n------END------\n")
22
22
 
23
- file.write("------BEGIN Reduce(Remove the function if you don't want a reduce)-------\n")
24
- file.write(view.reduce) if view.reduce?
25
- file.write("------END------\n")
23
+ file.write("\n------BEGIN Reduce(Remove the function if you don't want a reduce)-------\n")
24
+ file.write(view.reduce? ? view.reduce: ::Couchup::View::REDUCE_TEMPLATE )
25
+ file.write("\n------END------\n")
26
+ file.write("\nDelete Everything to do nothing.\n")
26
27
  file.close
27
28
 
28
29
  `#{ENV['EDITOR']} #{tmp_file_path}`
29
30
  contents = File.open(tmp_file_path).read
30
- ::Couchup::View.create(name, contents)
31
+ ::Couchup::View.create(name, contents) unless contents.blank?
31
32
  file.close
32
33
  file.unlink
33
34
  end
34
35
 
35
36
  def self.describe
36
- "Creates a new database and switches to the database"
37
+ "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'"
37
38
  end
38
39
  end
39
40
  end
@@ -0,0 +1,5 @@
1
+ class NilClass
2
+ def blank?
3
+ true
4
+ end
5
+ end
@@ -1,6 +1,6 @@
1
1
  class String
2
2
  def blank?
3
- nil || empty?
3
+ nil || strip.empty?
4
4
  end
5
5
 
6
6
  def constantize
@@ -1,3 +1,3 @@
1
1
  module Couchup
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -1,25 +1,65 @@
1
1
  module Couchup
2
2
  class View
3
+ MAP_TEMPLATE = "function(doc){\n}\n"
4
+ REDUCE_TEMPLATE = ""
3
5
 
4
6
  def initialize(name)
5
7
  @design, @name = name.split "/"
6
- design, name = name.split "/"
7
8
  begin
8
- doc = Couchup.database.get("_design/#{design}")
9
+ @doc = Couchup.database.get("_design/#{@design}")
9
10
  rescue
10
- # handle creation of new design doc.
11
+ puts "Design #{@design} not found creating a new one"
12
+ @doc = {"_id" => "_design/#{@design}", :language => 'javascript', :views => {}}
13
+ save
14
+ @doc = Couchup.database.get("_design/#{@design}")
11
15
  end
12
16
  end
17
+
18
+ def save
19
+ Couchup.database.save_doc(@doc)
20
+ end
21
+
22
+ def map
23
+ @doc["views"][@name]["map"]
24
+ end
25
+
26
+ def map=(fun)
27
+ if map?
28
+ @doc["views"][@name]["map"] = fun
29
+ else
30
+ @doc["views"][@name] = {:map => fun}
31
+ end
32
+ end
33
+
34
+ def reduce
35
+ @doc["views"][@name]["reduce"]
36
+ end
13
37
 
38
+ def reduce=(fun)
39
+ if reduce?
40
+ @doc["views"][@name]["reduce"] = fun
41
+ else
42
+ @doc["views"][@name] ={:reduce => fun}
43
+ end
44
+ end
45
+
46
+ def map?
47
+ @doc["views"][@name] && !@doc["views"][@name]["map"].blank?
48
+ end
49
+
50
+ def reduce?
51
+ @doc["views"][@name] && !@doc["views"][@name]["reduce"].blank?
52
+ end
53
+
54
+
14
55
  def self.create(name, file_contents)
15
- v = doc["views"][name]
56
+ v = new(name)
57
+
16
58
  map, reduce = parse(file_contents)
17
-
18
- puts "View Exists overwriting." unless v.nil?
19
- v = (doc["views"][name]= {}) if v.nil?
20
- v["map"] = map
21
- v["reduce"] = reduce unless reduce.blank?
22
- Couchup.database.save_doc(doc)
59
+ v.map = map
60
+ v.reduce = reduce unless reduce.blank?
61
+ puts v.inspect
62
+ v.save
23
63
  end
24
64
 
25
65
  def self.parse(file_contents)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: couchup
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.8
5
+ version: 0.0.9
6
6
  platform: ruby
7
7
  authors:
8
8
  - V Sreekanth
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-25 00:00:00 +05:30
13
+ date: 2011-02-26 00:00:00 +05:30
14
14
  default_executable: ey
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -135,6 +135,7 @@ files:
135
135
  - lib/couchup/commands/view.rb
136
136
  - lib/couchup/couchup.rb
137
137
  - lib/couchup/extensions/array.rb
138
+ - lib/couchup/extensions/nil_class.rb
138
139
  - lib/couchup/extensions/string.rb
139
140
  - lib/couchup/extensions/symbol.rb
140
141
  - lib/couchup/mapreduce.rb