peleteiro-activecouch 0.2.1

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.
Files changed (58) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +28 -0
  3. data/Rakefile +52 -0
  4. data/VERSION +1 -0
  5. data/lib/active_couch.rb +12 -0
  6. data/lib/active_couch/base.rb +608 -0
  7. data/lib/active_couch/callbacks.rb +89 -0
  8. data/lib/active_couch/connection.rb +164 -0
  9. data/lib/active_couch/errors.rb +13 -0
  10. data/lib/active_couch/support.rb +3 -0
  11. data/lib/active_couch/support/exporter.rb +97 -0
  12. data/lib/active_couch/support/extensions.rb +86 -0
  13. data/lib/active_couch/support/inflections.rb +52 -0
  14. data/lib/active_couch/support/inflector.rb +279 -0
  15. data/lib/active_couch/views.rb +3 -0
  16. data/lib/active_couch/views/errors.rb +4 -0
  17. data/lib/active_couch/views/raw_view.rb +40 -0
  18. data/lib/active_couch/views/view.rb +85 -0
  19. data/lib/activecouch.rb +1 -0
  20. data/spec/base/after_delete_spec.rb +110 -0
  21. data/spec/base/after_save_spec.rb +102 -0
  22. data/spec/base/before_delete_spec.rb +109 -0
  23. data/spec/base/before_save_spec.rb +101 -0
  24. data/spec/base/count_all_spec.rb +29 -0
  25. data/spec/base/count_spec.rb +77 -0
  26. data/spec/base/create_spec.rb +28 -0
  27. data/spec/base/database_spec.rb +70 -0
  28. data/spec/base/delete_spec.rb +97 -0
  29. data/spec/base/find_from_url_spec.rb +55 -0
  30. data/spec/base/find_spec.rb +383 -0
  31. data/spec/base/from_json_spec.rb +54 -0
  32. data/spec/base/has_many_spec.rb +89 -0
  33. data/spec/base/has_spec.rb +88 -0
  34. data/spec/base/id_spec.rb +25 -0
  35. data/spec/base/initialize_spec.rb +91 -0
  36. data/spec/base/marshal_dump_spec.rb +64 -0
  37. data/spec/base/marshal_load_spec.rb +58 -0
  38. data/spec/base/module_spec.rb +18 -0
  39. data/spec/base/nested_class_spec.rb +19 -0
  40. data/spec/base/rev_spec.rb +20 -0
  41. data/spec/base/save_spec.rb +130 -0
  42. data/spec/base/site_spec.rb +62 -0
  43. data/spec/base/to_json_spec.rb +73 -0
  44. data/spec/connection/initialize_spec.rb +28 -0
  45. data/spec/exporter/all_databases_spec.rb +24 -0
  46. data/spec/exporter/create_database_spec.rb +47 -0
  47. data/spec/exporter/delete_database_spec.rb +45 -0
  48. data/spec/exporter/delete_spec.rb +36 -0
  49. data/spec/exporter/export_spec.rb +62 -0
  50. data/spec/exporter/export_with_raw_views_spec.rb +66 -0
  51. data/spec/spec_helper.rb +9 -0
  52. data/spec/views/define_spec.rb +34 -0
  53. data/spec/views/include_attributes_spec.rb +30 -0
  54. data/spec/views/raw_view_spec.rb +49 -0
  55. data/spec/views/to_json_spec.rb +58 -0
  56. data/spec/views/with_filter_spec.rb +13 -0
  57. data/spec/views/with_key_spec.rb +19 -0
  58. metadata +117 -0
@@ -0,0 +1 @@
1
+ require 'active_couch'
@@ -0,0 +1,110 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #after_delete method" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984/'
7
+ has :name
8
+ has :delete_status
9
+ # Callback, after the actual save happens
10
+ after_delete :print_message
11
+
12
+ private
13
+ def print_message
14
+ self.delete_status = "Deleted McLovin"
15
+ end
16
+ end
17
+ # Migration needed for this spec
18
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
19
+ end
20
+
21
+ after(:each) do
22
+ # Migration needed for this spec
23
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
24
+ Object.send(:remove_const, :Person)
25
+ end
26
+
27
+ it "should have a class method called after_delete" do
28
+ Person.methods.include?('after_delete').should == true
29
+ end
30
+
31
+ it "should call the method specified as an argument to after_delete, *after_delete* the object has been deleted from CouchDB" do
32
+ p = Person.new(:name => 'McLovin')
33
+ # First, it must be empty...
34
+ p.delete_status.should == ""
35
+ # then it must be saved...
36
+ p.save
37
+ # Delete should return true...
38
+ p.delete.should == true
39
+ p.delete_status.should == "Deleted McLovin"
40
+ end
41
+ end
42
+
43
+ describe "ActiveCouch::Base #after_delete method with a block as argument" do
44
+ before(:each) do
45
+ class Person < ActiveCouch::Base
46
+ site 'http://localhost:5984/'
47
+ has :name
48
+ has :delete_status
49
+ # Callback, after the actual save happens
50
+ after_delete { |record| record.delete_status = 'Deleted McLovin' }
51
+ end
52
+ # Migration needed for this spec
53
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
54
+ end
55
+
56
+ after(:each) do
57
+ # Migration needed for this spec
58
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
59
+ Object.send(:remove_const, :Person)
60
+ end
61
+
62
+ it "should execute the block as a param to after_delete" do
63
+ p = Person.new(:name => 'McLovin')
64
+ # First, it must be empty...
65
+ p.delete_status.should == ""
66
+ # then it must be saved...
67
+ p.save
68
+ # Delete should return true...
69
+ p.delete.should == true
70
+ # ...and then delete_status must be "Deleted McLovin"
71
+ p.delete_status.should == "Deleted McLovin"
72
+ end
73
+ end
74
+
75
+ describe "ActiveCouch::Base #after_save method with an Object (which implements after_save) as argument" do
76
+ before(:each) do
77
+ class DeleteStatusSetter
78
+ def after_delete(record)
79
+ record.delete_status = 'Deleted McLovin'
80
+ end
81
+ end
82
+
83
+ class Person < ActiveCouch::Base
84
+ site 'http://localhost:5984/'
85
+ has :name
86
+ has :delete_status
87
+ # Callback, after the actual save happens
88
+ after_delete DeleteStatusSetter.new
89
+ end
90
+ # Migration needed for this spec
91
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
92
+ end
93
+
94
+ after(:each) do
95
+ # Migration needed for this spec
96
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
97
+ Object.send(:remove_const, :Person)
98
+ end
99
+
100
+ it "should call before_save in the object passed as a param to after_delete" do
101
+ p = Person.new(:name => 'McLovin')
102
+ # First, it must be empty...
103
+ p.delete_status.should == ""
104
+ # then it must be saved...
105
+ p.save
106
+ # Delete should return true...
107
+ p.delete.should == true
108
+ p.delete_status.should == "Deleted McLovin"
109
+ end
110
+ end
@@ -0,0 +1,102 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #after_save method" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984/'
7
+ has :name
8
+ has :saved_revision
9
+ # Callback, after the actual save happens
10
+ after_save :print_message
11
+
12
+ private
13
+ def print_message
14
+ # This can only be set if the object has been saved.
15
+ # Otherwise it will be nil
16
+ self.saved_revision = self.rev
17
+ end
18
+ end
19
+ # Migration needed for this spec
20
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
21
+ end
22
+
23
+ after(:each) do
24
+ # Migration needed for this spec
25
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
26
+ Object.send(:remove_const, :Person)
27
+ end
28
+
29
+ it "should have a class method called after_save" do
30
+ Person.methods.include?('after_save').should == true
31
+ end
32
+
33
+ it "should call the method specified as an argument to after_save, *after* the object has been persisted in CouchDB" do
34
+ p = Person.new(:name => 'McLovin')
35
+ # Save should return true...
36
+ p.save.should == true
37
+ # ...and saved_id must not be nil (because it is set to the revision)
38
+ p.saved_revision.should_not == nil
39
+ end
40
+ end
41
+
42
+ describe "ActiveCouch::Base #after_save method with a block as argument" do
43
+ before(:each) do
44
+ class Person < ActiveCouch::Base
45
+ site 'http://localhost:5984/'
46
+ has :name
47
+ has :saved_revision
48
+ # Callback, after the actual save happens
49
+ after_save { |record| record.saved_revision = record.rev }
50
+ end
51
+ # Migration needed for this spec
52
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
53
+ end
54
+
55
+ after(:each) do
56
+ # Migration needed for this spec
57
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
58
+ Object.send(:remove_const, :Person)
59
+ end
60
+
61
+ it "should execute the block as a param to after_save" do
62
+ p = Person.new(:name => 'McLovin')
63
+ # Save should return true...
64
+ p.save.should == true
65
+ # ...and saved_id must not be nil (because it is set to the revision)
66
+ p.saved_revision.should_not == nil
67
+ end
68
+ end
69
+
70
+ describe "ActiveCouch::Base #after_save method with an Object (which implements after_save) as argument" do
71
+ before(:each) do
72
+ class RevisionSetter
73
+ def after_save(record)
74
+ record.saved_revision = record.rev
75
+ end
76
+ end
77
+
78
+ class Person < ActiveCouch::Base
79
+ site 'http://localhost:5984/'
80
+ has :name
81
+ has :saved_revision
82
+ # Callback, after the actual save happens
83
+ after_save RevisionSetter.new
84
+ end
85
+ # Migration needed for this spec
86
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
87
+ end
88
+
89
+ after(:each) do
90
+ # Migration needed for this spec
91
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
92
+ Object.send(:remove_const, :Person)
93
+ end
94
+
95
+ it "should call before_save in the object passed as a param to after_save" do
96
+ p = Person.new(:name => 'McLovin')
97
+ # Save should return true...
98
+ p.save.should == true
99
+ # ...and saved_id must not be nil (because it is set to the revision)
100
+ p.saved_revision.should_not == nil
101
+ end
102
+ end
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #before_delete method with a Symbol as argument" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984/'
7
+ has :name
8
+ has :age, :which_is => :number
9
+ # Callback, before the actual save happens
10
+ before_delete :zero_age
11
+
12
+ private
13
+ def zero_age
14
+ self.age = 0
15
+ end
16
+ end
17
+ # Migration needed for this spec
18
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
19
+ end
20
+
21
+ after(:each) do
22
+ # Migration needed for this spec
23
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
24
+ Object.send(:remove_const, :Person)
25
+ end
26
+
27
+ it "should have a class method called before_save" do
28
+ Person.methods.include?('before_delete').should == true
29
+ end
30
+
31
+ it "should call the method specified as an argument to before_delete, *before* deleting the object from CouchDB" do
32
+ # First save the object
33
+ p = Person.create(:name => 'McLovin', :age => 10)
34
+ # Before deleting, age must be 10
35
+ p.age.should == 10
36
+ # Delete the object, and...
37
+ p.delete.should == true
38
+ # ...age must equal 0
39
+ p.age.should == 0
40
+ end
41
+ end
42
+
43
+ describe "ActiveCouch::Base #before_save method with a block as argument" do
44
+ before(:each) do
45
+ class Person < ActiveCouch::Base
46
+ site 'http://localhost:5984/'
47
+ has :name
48
+ has :age, :which_is => :number
49
+ # Callback, before the actual save happens
50
+ before_delete { |record| record.age = 0 }
51
+ end
52
+ # Migration needed for this spec
53
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
54
+ end
55
+
56
+ after(:each) do
57
+ # Migration needed for this spec
58
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
59
+ Object.send(:remove_const, :Person)
60
+ end
61
+
62
+ it "should execute the block as a param to before_save" do
63
+ # First save the object
64
+ p = Person.create(:name => 'McLovin', :age => 10)
65
+ # Before deleting, age must be 10
66
+ p.age.should == 10
67
+ # Delete the object, and...
68
+ p.delete.should == true
69
+ # ...age must equal 0
70
+ p.age.should == 0
71
+ end
72
+ end
73
+
74
+ describe "ActiveCouch::Base #before_save method with an Object (which implements before_save) as argument" do
75
+ before(:each) do
76
+ class AgeSetter
77
+ def before_delete(record)
78
+ record.age = 0
79
+ end
80
+ end
81
+
82
+ class Person < ActiveCouch::Base
83
+ site 'http://localhost:5984/'
84
+ has :name
85
+ has :age, :which_is => :number
86
+ # Callback, before the actual save happens
87
+ before_delete AgeSetter.new
88
+ end
89
+ # Migration needed for this spec
90
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
91
+ end
92
+
93
+ after(:each) do
94
+ # Migration needed for this spec
95
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
96
+ Object.send(:remove_const, :Person)
97
+ end
98
+
99
+ it "should call before_save in the object passed as a param to before_delete" do
100
+ # First save the object
101
+ p = Person.create(:name => 'McLovin', :age => 10)
102
+ # Before deleting, age must be 10
103
+ p.age.should == 10
104
+ # Delete the object, and...
105
+ p.delete.should == true
106
+ # ...age must equal 0
107
+ p.age.should == 0
108
+ end
109
+ end
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #before_save method with a Symbol as argument" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984/'
7
+ has :first_name
8
+ has :last_name
9
+ # Callback, before the actual save happens
10
+ before_save :set_first_name
11
+
12
+ private
13
+ def set_first_name
14
+ self.first_name = 'Seth'
15
+ end
16
+ end
17
+ # Migration needed for this spec
18
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
19
+ end
20
+
21
+ after(:each) do
22
+ # Migration needed for this spec
23
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
24
+ Object.send(:remove_const, :Person)
25
+ end
26
+
27
+ it "should have a class method called before_save" do
28
+ Person.methods.include?('before_save').should == true
29
+ end
30
+
31
+ it "should call the method specified as an argument to before_save, *before* the object has been persisted in CouchDB" do
32
+ p = Person.new(:last_name => 'McLovin')
33
+ # Save should return true...
34
+ p.save.should == true
35
+ # ...and the first_name must be set to 'Seth'
36
+ p.first_name.should == 'Seth'
37
+ end
38
+ end
39
+
40
+ describe "ActiveCouch::Base #before_save method with a block as argument" do
41
+ before(:each) do
42
+ class Person < ActiveCouch::Base
43
+ site 'http://localhost:5984/'
44
+ has :first_name; has :last_name
45
+ # Callback, before the actual save happens
46
+ before_save { |record| record.first_name = 'Seth' }
47
+ end
48
+
49
+ # Migration needed for this spec
50
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
51
+ end
52
+
53
+ after(:each) do
54
+ # Migration needed for this spec
55
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
56
+ Object.send(:remove_const, :Person)
57
+ end
58
+
59
+ it "should execute the block as a param to before_save" do
60
+ p = Person.new(:last_name => 'McLovin')
61
+ # Save should return true...
62
+ p.save.should == true
63
+ # ...and the first_name must be set to 'Seth'
64
+ p.first_name.should == 'Seth'
65
+ end
66
+ end
67
+
68
+ describe "ActiveCouch::Base #before_save method with an Object (which implements before_save) as argument" do
69
+ before(:each) do
70
+ class NameSetter
71
+ def before_save(record)
72
+ record.first_name = 'Seth'
73
+ end
74
+ end
75
+
76
+ class Person < ActiveCouch::Base
77
+ site 'http://localhost:5984/'
78
+ has :first_name; has :last_name
79
+ # Callback, before the actual save happens
80
+ before_save NameSetter.new
81
+ end
82
+
83
+ # Migration needed for this spec
84
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
85
+ end
86
+
87
+ after(:each) do
88
+ # Migration needed for this spec
89
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
90
+ Object.send(:remove_const, :Person)
91
+ end
92
+
93
+ it "should call before_save in the object passed as a param to before_save" do
94
+ p = Person.new(:last_name => 'McLovin')
95
+ # Save should return true...
96
+ p.save.should == true
97
+ # ...and the first_name must be set to 'Seth'
98
+ p.first_name.should == 'Seth'
99
+ end
100
+
101
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #count_all method with multiple documents in the CouchDB database" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984'
7
+
8
+ has :first_name
9
+ has :last_name
10
+ end
11
+
12
+ # Create the database first
13
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
14
+ # Save two objects
15
+ Person.create(:last_name => 'McLovin', :first_name => 'Seth')
16
+ Person.create(:last_name => 'McLovin', :first_name => 'Bob')
17
+ end
18
+
19
+ after(:each) do
20
+ # Delete the database last
21
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
22
+ Object.send(:remove_const, :Person)
23
+ end
24
+
25
+ it "should find all objects in the database when find method is sent the param :all" do
26
+ count = Person.count_all
27
+ count.should == 2
28
+ end
29
+ end