couch_crumbs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require "facets/string/modulize"
4
+
5
+ require "core_ext/array.rb"
6
+
7
+ require "couch_crumbs/server.rb"
8
+ require "couch_crumbs/query.rb"
9
+ require "couch_crumbs/database.rb"
10
+ require "couch_crumbs/document.rb"
11
+ require "couch_crumbs/design.rb"
12
+ require "couch_crumbs/view.rb"
13
+
14
+ module CouchCrumbs
15
+
16
+ # Defaults
17
+ @@default_server = @@default_database = nil
18
+
19
+ # Connect to a specific couch server/database
20
+ #
21
+ # ==== Parameters
22
+ # server_uri<String>:: host/port in URI form
23
+ # default_database<String>:: default database name
24
+ #
25
+ def self.connect(opts = {})
26
+ @@default_server = Server.new(:uri => opts[:server_uri])
27
+ @@default_database = Database.new(:name => opts[:default_database])
28
+
29
+ # return true if both server and database were instantiated
30
+ (@@default_server && @@default_database) ? true : (raise "unable to connect CouchCrumbs to a CouchDB instance")
31
+ end
32
+
33
+ # Return a default server for use
34
+ #
35
+ def self.default_server
36
+ @@default_server or (raise "default server is only available after calling CouchCrumbs::connect")
37
+ end
38
+
39
+ # Return a default database that models will use
40
+ #
41
+ def self.default_database
42
+ @@default_database or (raise "default database is only available after calling CouchCrumbs::connect")
43
+ end
44
+
45
+ end
data/script/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ libs = " -r ruby-debug"
7
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
8
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
9
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/couch_crumbs.rb'}"
10
+ puts "Loading couch_crumbs gem"
11
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe Array do
4
+
5
+ context "#destroy!" do
6
+
7
+ before do
8
+ @object = mock(:object)
9
+
10
+ @array.stub!(:all).and_return([@object])
11
+ end
12
+
13
+ it "should destroy all" do
14
+ @object.should_receive(:destroy!).once
15
+
16
+ @array.all.destroy!
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,69 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ module CouchCrumbs
4
+
5
+ describe Database do
6
+
7
+ before do
8
+ @database = CouchCrumbs::default_database
9
+ end
10
+
11
+ describe "#initialize" do
12
+
13
+ it "should create a new database" do
14
+ Database.new.should be_kind_of(Database)
15
+ end
16
+
17
+ it "should return an existing database" do
18
+ Database.new(:name => CouchCrumbs::default_database.name).should be_kind_of(Database)
19
+ end
20
+
21
+ end
22
+
23
+ describe "#documents" do
24
+
25
+ before do
26
+ @resource = Resource.create!
27
+ end
28
+
29
+ it "should return an array of all documents" do
30
+ @database.documents(:limit => 1_000_000).collect{ |doc| doc.id }.should include(@resource.id)
31
+ end
32
+
33
+ it "should support optional view parameters" do
34
+ @database.documents(:key => @resource.id).collect{ |d| d.id }.should eql([@resource.id])
35
+ end
36
+
37
+ after do
38
+ @resource.destroy!
39
+ end
40
+
41
+ end
42
+
43
+ describe "#design_documents" do
44
+
45
+ before do
46
+ @design = Design.get!(@database, :name => "spec")
47
+ end
48
+
49
+ it "should return an array of design documents" do
50
+ @database.design_documents.collect{ |d| d.rev }.should include(@design.rev)
51
+ end
52
+
53
+ end
54
+
55
+ describe "#destroy!" do
56
+
57
+ before do
58
+ Database.new(:name => "destroy").destroy!
59
+ end
60
+
61
+ it "should destroy a database" do
62
+ CouchCrumbs::default_server.databases.collect{ |db| db.name }.should_not include("destroy")
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,103 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ module CouchCrumbs
4
+
5
+ describe Design do
6
+
7
+ before do
8
+ @database = CouchCrumbs::default_database
9
+ end
10
+
11
+ describe "#get!" do
12
+
13
+ it "should find or create a design document" do
14
+ Design.get!(@database, :name => "get").should be_kind_of(Design)
15
+ end
16
+
17
+ end
18
+
19
+ describe "#initialize" do
20
+
21
+ before do
22
+ @design = Design.get!(@database, :name => "example")
23
+ end
24
+
25
+ it "should create a new design document" do
26
+ @design.should be_kind_of(Design)
27
+ end
28
+
29
+ it "should have an id property" do
30
+ @design.id.should eql("_design/example")
31
+ end
32
+
33
+ it "should have a name property" do
34
+ @design.name.should eql("example")
35
+ end
36
+
37
+ end
38
+
39
+ describe "#save!" do
40
+
41
+ before do
42
+ @design = Design.new(@database, :name => "save")
43
+
44
+ @design.save!
45
+ end
46
+
47
+ it "should have a revision property" do
48
+ @design.rev.should_not be_nil
49
+ end
50
+
51
+ after do
52
+ @design.destroy!
53
+ end
54
+
55
+ end
56
+
57
+ describe "#destroy!" do
58
+
59
+ before do
60
+ @design = Design.get!(@database, :name => "destroy")
61
+ end
62
+
63
+ it "should destroy the design document" do
64
+ @design.destroy!.should be_true
65
+ end
66
+
67
+ end
68
+
69
+ describe "#views" do
70
+
71
+ before do
72
+ # Grab the design doc from the Person class declared above
73
+ @design = Design.get!(@database, :name => "person")
74
+ end
75
+
76
+ it "should return an array of views" do
77
+ @design.views.should_not be_empty
78
+ end
79
+
80
+ it "should support an optional name parameter to single a specific view" do
81
+ @design.views(:name => "all").should be_kind_of(View)
82
+ end
83
+
84
+ end
85
+
86
+ describe "#add_view" do
87
+
88
+ before do
89
+ # Manually construct a design doc and view on Person
90
+ @design = Design.get!(@database, :name => "append")
91
+
92
+ @view = View.create!(@design, "title", View.simple_json(Person.crumb_type, :title))
93
+ end
94
+
95
+ it "should append a view to the list of views" do
96
+ @design.views.collect{ |v| v.hash }.should eql([@view.hash])
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+
103
+ end