activecouch 0.1.0

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 (52) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +42 -0
  3. data/Rakefile +31 -0
  4. data/VERSION +1 -0
  5. data/lib/active_couch.rb +18 -0
  6. data/lib/active_couch/associations.rb +1 -0
  7. data/lib/active_couch/associations/has_many_association.rb +35 -0
  8. data/lib/active_couch/attribute.rb +46 -0
  9. data/lib/active_couch/base.rb +458 -0
  10. data/lib/active_couch/callbacks.rb +81 -0
  11. data/lib/active_couch/connection.rb +155 -0
  12. data/lib/active_couch/errors.rb +17 -0
  13. data/lib/active_couch/migrations.rb +3 -0
  14. data/lib/active_couch/migrations/errors.rb +4 -0
  15. data/lib/active_couch/migrations/migration.rb +77 -0
  16. data/lib/active_couch/migrations/migrator.rb +53 -0
  17. data/lib/active_couch/support.rb +2 -0
  18. data/lib/active_couch/support/extensions.rb +92 -0
  19. data/lib/active_couch/support/inflections.rb +52 -0
  20. data/lib/active_couch/support/inflector.rb +280 -0
  21. data/spec/attribute/initialize_spec.rb +138 -0
  22. data/spec/base/after_delete_spec.rb +107 -0
  23. data/spec/base/after_save_spec.rb +99 -0
  24. data/spec/base/before_delete_spec.rb +106 -0
  25. data/spec/base/before_save_spec.rb +98 -0
  26. data/spec/base/create_spec.rb +27 -0
  27. data/spec/base/database_spec.rb +65 -0
  28. data/spec/base/delete_spec.rb +78 -0
  29. data/spec/base/find_spec.rb +165 -0
  30. data/spec/base/from_json_spec.rb +48 -0
  31. data/spec/base/has_many_spec.rb +81 -0
  32. data/spec/base/has_spec.rb +76 -0
  33. data/spec/base/id_spec.rb +22 -0
  34. data/spec/base/initialize_spec.rb +43 -0
  35. data/spec/base/module_spec.rb +18 -0
  36. data/spec/base/nested_class_spec.rb +19 -0
  37. data/spec/base/rev_spec.rb +16 -0
  38. data/spec/base/save_spec.rb +65 -0
  39. data/spec/base/site_spec.rb +41 -0
  40. data/spec/base/to_json_spec.rb +64 -0
  41. data/spec/connection/initialize_spec.rb +28 -0
  42. data/spec/has_many_association/initialize_spec.rb +33 -0
  43. data/spec/migration/define_spec.rb +29 -0
  44. data/spec/migration/include_attributes_spec.rb +30 -0
  45. data/spec/migration/view_js_spec.rb +46 -0
  46. data/spec/migration/with_filter_spec.rb +13 -0
  47. data/spec/migration/with_key_spec.rb +13 -0
  48. data/spec/migrator/create_database_spec.rb +48 -0
  49. data/spec/migrator/delete_database_spec.rb +46 -0
  50. data/spec/migrator/migrate_spec.rb +99 -0
  51. data/spec/spec_helper.rb +9 -0
  52. metadata +104 -0
@@ -0,0 +1,107 @@
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::Migrator.create_database('http://localhost:5984/', 'people')
19
+ end
20
+
21
+ after(:each) do
22
+ # Migration needed for this spec
23
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
24
+ end
25
+
26
+ it "should have a class method called after_delete" do
27
+ Person.methods.include?('after_delete').should == true
28
+ end
29
+
30
+ it "should call the method specified as an argument to after_delete, *after_delete* the object has been deleted from CouchDB" do
31
+ p = Person.new(:name => 'McLovin')
32
+ # First, it must be empty...
33
+ p.delete_status.should == ""
34
+ # then it must be saved...
35
+ p.save
36
+ # Delete should return true...
37
+ p.delete.should == true
38
+ p.delete_status.should == "Deleted McLovin"
39
+ end
40
+ end
41
+
42
+ describe "ActiveCouch::Base #after_delete 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 :delete_status
48
+ # Callback, after the actual save happens
49
+ after_delete { |record| record.delete_status = 'Deleted McLovin' }
50
+ end
51
+ # Migration needed for this spec
52
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
53
+ end
54
+
55
+ after(:each) do
56
+ # Migration needed for this spec
57
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
58
+ end
59
+
60
+ it "should execute the block as a param to after_delete" do
61
+ p = Person.new(:name => 'McLovin')
62
+ # First, it must be empty...
63
+ p.delete_status.should == ""
64
+ # then it must be saved...
65
+ p.save
66
+ # Delete should return true...
67
+ p.delete.should == true
68
+ # ...and then delete_status must be "Deleted McLovin"
69
+ p.delete_status.should == "Deleted McLovin"
70
+ end
71
+ end
72
+
73
+ describe "ActiveCouch::Base #after_save method with an Object (which implements after_save) as argument" do
74
+ before(:each) do
75
+ class DeleteStatusSetter
76
+ def after_delete(record)
77
+ record.delete_status = 'Deleted McLovin'
78
+ end
79
+ end
80
+
81
+ class Person < ActiveCouch::Base
82
+ site 'http://localhost:5984/'
83
+ has :name
84
+ has :delete_status
85
+ # Callback, after the actual save happens
86
+ after_delete DeleteStatusSetter.new
87
+ end
88
+ # Migration needed for this spec
89
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
90
+ end
91
+
92
+ after(:each) do
93
+ # Migration needed for this spec
94
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
95
+ end
96
+
97
+ it "should call before_save in the object passed as a param to after_delete" do
98
+ p = Person.new(:name => 'McLovin')
99
+ # First, it must be empty...
100
+ p.delete_status.should == ""
101
+ # then it must be saved...
102
+ p.save
103
+ # Delete should return true...
104
+ p.delete.should == true
105
+ p.delete_status.should == "Deleted McLovin"
106
+ end
107
+ end
@@ -0,0 +1,99 @@
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::Migrator.create_database('http://localhost:5984/', 'people')
21
+ end
22
+
23
+ after(:each) do
24
+ # Migration needed for this spec
25
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
26
+ end
27
+
28
+ it "should have a class method called after_save" do
29
+ Person.methods.include?('after_save').should == true
30
+ end
31
+
32
+ it "should call the method specified as an argument to after_save, *after* the object has been persisted in CouchDB" do
33
+ p = Person.new(:name => 'McLovin')
34
+ # Save should return true...
35
+ p.save.should == true
36
+ # ...and saved_id must not be nil (because it is set to the revision)
37
+ p.saved_revision.should_not == nil
38
+ end
39
+ end
40
+
41
+ describe "ActiveCouch::Base #after_save method with a block as argument" do
42
+ before(:each) do
43
+ class Person < ActiveCouch::Base
44
+ site 'http://localhost:5984/'
45
+ has :name
46
+ has :saved_revision
47
+ # Callback, after the actual save happens
48
+ after_save { |record| record.saved_revision = record.rev }
49
+ end
50
+ # Migration needed for this spec
51
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
52
+ end
53
+
54
+ after(:each) do
55
+ # Migration needed for this spec
56
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
57
+ end
58
+
59
+ it "should execute the block as a param to after_save" do
60
+ p = Person.new(:name => 'McLovin')
61
+ # Save should return true...
62
+ p.save.should == true
63
+ # ...and saved_id must not be nil (because it is set to the revision)
64
+ p.saved_revision.should_not == nil
65
+ end
66
+ end
67
+
68
+ describe "ActiveCouch::Base #after_save method with an Object (which implements after_save) as argument" do
69
+ before(:each) do
70
+ class RevisionSetter
71
+ def after_save(record)
72
+ record.saved_revision = record.rev
73
+ end
74
+ end
75
+
76
+ class Person < ActiveCouch::Base
77
+ site 'http://localhost:5984/'
78
+ has :name
79
+ has :saved_revision
80
+ # Callback, after the actual save happens
81
+ after_save RevisionSetter.new
82
+ end
83
+ # Migration needed for this spec
84
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
85
+ end
86
+
87
+ after(:each) do
88
+ # Migration needed for this spec
89
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
90
+ end
91
+
92
+ it "should call before_save in the object passed as a param to after_save" do
93
+ p = Person.new(:name => 'McLovin')
94
+ # Save should return true...
95
+ p.save.should == true
96
+ # ...and saved_id must not be nil (because it is set to the revision)
97
+ p.saved_revision.should_not == nil
98
+ end
99
+ end
@@ -0,0 +1,106 @@
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::Migrator.create_database('http://localhost:5984/', 'people')
19
+ end
20
+
21
+ after(:each) do
22
+ # Migration needed for this spec
23
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
24
+ end
25
+
26
+ it "should have a class method called before_save" do
27
+ Person.methods.include?('before_delete').should == true
28
+ end
29
+
30
+ it "should call the method specified as an argument to before_delete, *before* deleting the object from CouchDB" do
31
+ # First save the object
32
+ p = Person.create(:name => 'McLovin', :age => 10)
33
+ # Before deleting, age must be 10
34
+ p.age.should == 10
35
+ # Delete the object, and...
36
+ p.delete.should == true
37
+ # ...age must equal 0
38
+ p.age.should == 0
39
+ end
40
+ end
41
+
42
+ describe "ActiveCouch::Base #before_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 :age, :which_is => :number
48
+ # Callback, before the actual save happens
49
+ before_delete { |record| record.age = 0 }
50
+ end
51
+ # Migration needed for this spec
52
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
53
+ end
54
+
55
+ after(:each) do
56
+ # Migration needed for this spec
57
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
58
+ end
59
+
60
+ it "should execute the block as a param to before_save" do
61
+ # First save the object
62
+ p = Person.create(:name => 'McLovin', :age => 10)
63
+ # Before deleting, age must be 10
64
+ p.age.should == 10
65
+ # Delete the object, and...
66
+ p.delete.should == true
67
+ # ...age must equal 0
68
+ p.age.should == 0
69
+ end
70
+ end
71
+
72
+ describe "ActiveCouch::Base #before_save method with an Object (which implements before_save) as argument" do
73
+ before(:each) do
74
+ class AgeSetter
75
+ def before_delete(record)
76
+ record.age = 0
77
+ end
78
+ end
79
+
80
+ class Person < ActiveCouch::Base
81
+ site 'http://localhost:5984/'
82
+ has :name
83
+ has :age, :which_is => :number
84
+ # Callback, before the actual save happens
85
+ before_delete AgeSetter.new
86
+ end
87
+ # Migration needed for this spec
88
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
89
+ end
90
+
91
+ after(:each) do
92
+ # Migration needed for this spec
93
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
94
+ end
95
+
96
+ it "should call before_save in the object passed as a param to before_delete" do
97
+ # First save the object
98
+ p = Person.create(:name => 'McLovin', :age => 10)
99
+ # Before deleting, age must be 10
100
+ p.age.should == 10
101
+ # Delete the object, and...
102
+ p.delete.should == true
103
+ # ...age must equal 0
104
+ p.age.should == 0
105
+ end
106
+ end
@@ -0,0 +1,98 @@
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::Migrator.create_database('http://localhost:5984/', 'people')
19
+ end
20
+
21
+ after(:each) do
22
+ # Migration needed for this spec
23
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
24
+ end
25
+
26
+ it "should have a class method called before_save" do
27
+ Person.methods.include?('before_save').should == true
28
+ end
29
+
30
+ it "should call the method specified as an argument to before_save, *before* the object has been persisted in CouchDB" do
31
+ p = Person.new(:last_name => 'McLovin')
32
+ # Save should return true...
33
+ p.save.should == true
34
+ # ...and the first_name must be set to 'Seth'
35
+ p.first_name.should == 'Seth'
36
+ end
37
+ end
38
+
39
+ describe "ActiveCouch::Base #before_save method with a block as argument" do
40
+ before(:each) do
41
+ class Person < ActiveCouch::Base
42
+ site 'http://localhost:5984/'
43
+ has :first_name; has :last_name
44
+ # Callback, before the actual save happens
45
+ before_save { |record| record.first_name = 'Seth' }
46
+ end
47
+
48
+ # Migration needed for this spec
49
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
50
+ end
51
+
52
+ after(:each) do
53
+ # Migration needed for this spec
54
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
55
+ end
56
+
57
+ it "should execute the block as a param to before_save" do
58
+ p = Person.new(:last_name => 'McLovin')
59
+ # Save should return true...
60
+ p.save.should == true
61
+ # ...and the first_name must be set to 'Seth'
62
+ p.first_name.should == 'Seth'
63
+ end
64
+ end
65
+
66
+ describe "ActiveCouch::Base #before_save method with an Object (which implements before_save) as argument" do
67
+ before(:each) do
68
+ class NameSetter
69
+ def before_save(record)
70
+ record.first_name = 'Seth'
71
+ end
72
+ end
73
+
74
+ class Person < ActiveCouch::Base
75
+ site 'http://localhost:5984/'
76
+ has :first_name; has :last_name
77
+ # Callback, before the actual save happens
78
+ before_save NameSetter.new
79
+ end
80
+
81
+ # Migration needed for this spec
82
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
83
+ end
84
+
85
+ after(:each) do
86
+ # Migration needed for this spec
87
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
88
+ end
89
+
90
+ it "should call before_save in the object passed as a param to before_save" do
91
+ p = Person.new(:last_name => 'McLovin')
92
+ # Save should return true...
93
+ p.save.should == true
94
+ # ...and the first_name must be set to 'Seth'
95
+ p.first_name.should == 'Seth'
96
+ end
97
+
98
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #create method" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984/'
7
+ has :name
8
+ end
9
+ ActiveCouch::Migrator.create_database('http://localhost:5984/', 'people')
10
+ end
11
+
12
+ after(:each) do
13
+ ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'people')
14
+ end
15
+
16
+ it "should have a class method called create" do
17
+ Person.methods.include?('create').should == true
18
+ end
19
+
20
+ it "should be able to persist itself in the CouchDB database" do
21
+ person = Person.create(:name => 'McLovin')
22
+ person.name.should == 'McLovin'
23
+ # Check whether document is persisted correctly in the database
24
+ response = Net::HTTP.get_response URI.parse("http://localhost:5984/people/_all_docs/")
25
+ response.body.index('"total_rows":1').should_not == nil
26
+ end
27
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "A direct subclass of ActiveCouch::Base" do
4
+
5
+ before(:each) do
6
+ class Foo < ActiveCouch::Base
7
+ end
8
+ end
9
+
10
+ after(:each) do
11
+ # Remove class definition so we can start fresh in each spec.
12
+ Object.send(:remove_const, :Foo)
13
+ end
14
+
15
+ it "should have a base_class of itself" do
16
+ Foo.base_class.should == Foo
17
+ end
18
+
19
+ it "should infer the database name from the class name" do
20
+ Foo.database_name.should == 'foos'
21
+ end
22
+
23
+ it "should use the database name set with the set_database_name macro" do
24
+ Foo.set_database_name('legacy_foo')
25
+
26
+ Foo.database_name.should == 'legacy_foo'
27
+ end
28
+
29
+ it "should have an database_name= alias for set_database_name" do
30
+ Foo.database_name = 'fubar'
31
+
32
+ Foo.database_name.should == 'fubar'
33
+ end
34
+
35
+ it "should set an attribute from a value with define_attr_method" do
36
+ Foo.define_attr_method(:database_name, 'defined_foo')
37
+
38
+ Foo.database_name.should == 'defined_foo'
39
+ end
40
+
41
+ it "should set an attribute from a block with define_attr_method" do
42
+ Foo.send(:define_attr_method, :database_name) { 'legacy_' + original_database_name }
43
+
44
+ Foo.database_name.should == 'legacy_foos'
45
+ end
46
+ end
47
+
48
+ describe "A subclass of ActiveCouch::Base that's a subclass of an ActiveCouch::Base subclass" do
49
+
50
+ before(:all) do
51
+ class Parent < ActiveCouch::Base
52
+ end
53
+
54
+ class Child < Parent
55
+ end
56
+ end
57
+
58
+ it "should have a base_class of the parent" do
59
+ Child.base_class.should == Parent
60
+ end
61
+
62
+ it "should have a database_name of the parent's" do
63
+ Child.database_name.should == 'parents'
64
+ end
65
+ end