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,54 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #from_json method, with many attributes" do
4
+ before(:all) do
5
+ class Hotel < ActiveCouch::Base
6
+ has :name, :which_is => :text, :with_default_value => "Swissotel The Stamford"
7
+ has :star_rating, :which_is => :decimal, :with_default_value => 5.0
8
+ has :rooms, :which_is => :number, :with_default_value => 100
9
+ end
10
+
11
+ class Hospital < ActiveCouch::Base
12
+ has :name
13
+ end
14
+
15
+ class CrazyPerson < ActiveCouch::Base
16
+ has :name, :which_is => :text, :with_default_value => "Crazed McLovin"
17
+ has_many :hospitals
18
+ end
19
+ end
20
+
21
+ after(:all) do
22
+ Object.send(:remove_const, :Hotel)
23
+ Object.send(:remove_const, :Hospital)
24
+ Object.send(:remove_const, :CrazyPerson)
25
+ end
26
+
27
+ it "should have the from_json method" do
28
+ Hotel.should respond_to(:from_json)
29
+ Hospital.should respond_to(:from_json)
30
+ CrazyPerson.should respond_to(:from_json)
31
+ end
32
+
33
+ it "should instantiate an object when sent the from_json method with valid json as a parameter" do
34
+ h = Hotel.from_json("{\"name\":\"Swissotel The Stamford\",\"rooms\":200,\"star_rating\":4.0}")
35
+ h.class.should == Hotel
36
+ # Check whether all attributes are set correctly
37
+ h.name.should == "Swissotel The Stamford"
38
+ h.rooms.should == 200
39
+ h.star_rating.should == 4.0
40
+ end
41
+
42
+ it "should instantiate an object when sent the from_json method with valid JSON (containing associations) as a parameter" do
43
+ crazy = CrazyPerson.from_json('{"name":"Crazed McLovin","hospitals":[{"name":"Crazy Hospital 1"},{"name":"Crazy Hospital 2"}]}')
44
+
45
+ crazy.name == "Crazed McLovin"
46
+ crazy.hospitals.size.should == 2
47
+
48
+ hospitals = crazy.hospitals.collect{|h| h.name }
49
+ hospitals.sort!
50
+
51
+ hospitals.first.should == 'Crazy Hospital 1'
52
+ hospitals.last.should == 'Crazy Hospital 2'
53
+ end
54
+ end
@@ -0,0 +1,89 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "A class which is a subclass of ActiveCouch::Base with a has_many association" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ has :name, :which_is => :text
7
+ end
8
+
9
+ class AgedPerson < ActiveCouch::Base
10
+ has :name
11
+ has :age, :which_is => :number, :with_default_value => 10
12
+ end
13
+
14
+ class Contact < ActiveCouch::Base
15
+ has_many :people
16
+ end
17
+
18
+ @c = Contact.new
19
+ @p1 = Person.new
20
+ @a1 = AgedPerson.new
21
+ end
22
+
23
+ after(:each) do
24
+ Object.send(:remove_const, :Person)
25
+ Object.send(:remove_const, :AgedPerson)
26
+ Object.send(:remove_const, :Contact)
27
+ end
28
+
29
+ it "should have an instance variable called associations which is a Hash with the key being :people" do
30
+ Contact.associations.class.should == Hash
31
+ Contact.associations.keys.should == [:people]
32
+ end
33
+
34
+ it "should have methods called people and add_person" do
35
+ @c.should respond_to(:people)
36
+ @c.should respond_to(:add_person)
37
+ end
38
+
39
+ it "should have a method called people which returns an empty array" do
40
+ @c.people.should == []
41
+ end
42
+
43
+ it "should be able to add a Person object to the association" do
44
+ @c.add_person(@p1)
45
+ @c.people.should == [@p1]
46
+ end
47
+
48
+ end
49
+
50
+ describe "An object instantiated from class which is a subclass of ActiveCouch::Base" do
51
+ before(:each) do
52
+ class Comment < ActiveCouch::Base
53
+ has :body
54
+ end
55
+
56
+ class Blog < ActiveCouch::Base
57
+ has :title
58
+ has_many :comments
59
+ end
60
+
61
+ @comment1 = Comment.new(:body => "I can haz redbull?")
62
+ @comment2 = Comment.new(:body => 'k thx bai')
63
+ @blog = Blog.new(:title => 'Lolcats Primer', :comments => [@comment1, @comment2])
64
+ @blog1 = Blog.new(:title => 'Lolcats Primer The Sequel', :comments => [{:body => 'can'}, {:body => 'haz'}])
65
+ end
66
+
67
+ after(:each) do
68
+ Object.send(:remove_const, :Comment)
69
+ Object.send(:remove_const, :Blog)
70
+ end
71
+
72
+ it "should be able to initialize with a hash which contains descendents of ActiveCouch::Base" do
73
+ @comment1.body.should == "I can haz redbull?"
74
+ @comment2.body.should == "k thx bai"
75
+
76
+ @blog.title.should == 'Lolcats Primer'
77
+ @blog.comments.should == [@comment1, @comment2]
78
+ end
79
+
80
+ it "should be able to initialize from a hash which contains only Strings" do
81
+ @blog1.title.should == 'Lolcats Primer The Sequel'
82
+
83
+ comment_bodies = @blog1.comments.collect{|c| c.body }
84
+ comment_bodies.sort!
85
+
86
+ comment_bodies.first.should == 'can'
87
+ comment_bodies.last.should == 'haz'
88
+ end
89
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "A class which is a subclass of ActiveCouch::Base" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ has :name, :which_is => :text
7
+ end
8
+ # Initialize a new Person object
9
+ @p = Person.new
10
+ end
11
+
12
+ after(:each) do
13
+ Object.send(:remove_const, :Person)
14
+ end
15
+
16
+ it "should have a method called name which returns the value of the variable name" do
17
+ @p.should respond_to(:name)
18
+ @p.name.should == ""
19
+ end
20
+
21
+ it "should have a method called name= which should let you set the instance variable name" do
22
+ @p.should respond_to(:name=)
23
+ @p.name = "McLovin"
24
+ @p.name.should == "McLovin"
25
+ end
26
+ end
27
+
28
+ describe "A class which is a subclass of ActiveCouch::Base with a default value specified" do
29
+ before(:each) do
30
+ class NamedPerson < ActiveCouch::Base
31
+ has :name, :which_is => :text, :with_default_value => "McLovin"
32
+ end
33
+
34
+ @n = NamedPerson.new
35
+ end
36
+
37
+ after(:each) do
38
+ Object.send(:remove_const, :NamedPerson)
39
+ end
40
+
41
+ it "should have a method called name which returns the value of the variable name" do
42
+ @n.should respond_to(:name)
43
+ @n.name.should == "McLovin"
44
+ end
45
+
46
+ it "should have a method called name= which should let you set the instance variable name" do
47
+ @n.should respond_to(:name=)
48
+ @n.name = "Seth"
49
+ @n.name.should == "Seth"
50
+ end
51
+ end
52
+
53
+ describe "A class which is a subclass of ActiveCouch::Base with a default numerical value specified" do
54
+ before(:each) do
55
+ class AgedPerson < ActiveCouch::Base
56
+ has :name
57
+ has :age, :which_is => :number, :with_default_value => 10
58
+ end
59
+
60
+ @a = AgedPerson.new
61
+ end
62
+
63
+ after(:each) do
64
+ Object.send(:remove_const, :AgedPerson)
65
+ end
66
+
67
+ it "should have an instance variable called attributes which is a Hash with the keys being :name, :age" do
68
+ AgedPerson.attributes.class.should == Hash
69
+ AgedPerson.attributes.keys.index(:name).should_not == nil
70
+ AgedPerson.attributes.keys.index(:age).should_not == nil
71
+ end
72
+
73
+ it "should have methods called name and age which return the values of the variables name and age respectively" do
74
+ @a.should respond_to(:name)
75
+ @a.should respond_to(:age)
76
+
77
+ @a.name.should == ""
78
+ @a.age.should == 10
79
+ end
80
+
81
+ it "should have a method called name= which should let you set the instance variable name" do
82
+ @a.should respond_to(:name=)
83
+ @a.should respond_to(:age=)
84
+
85
+ @a.age = 15
86
+ @a.age.should == 15
87
+ end
88
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "An object instantiated from the subclass of ActiveCouch::Base" do
4
+ before(:all) do
5
+ class Person < ActiveCouch::Base
6
+ has :name, :which_is => :text
7
+ end
8
+ @person = Person.new
9
+ end
10
+
11
+ after(:all) do
12
+ Object.send(:remove_const, :Person)
13
+ end
14
+
15
+ it "should have accessors for the id attribute" do
16
+ @person.should respond_to(:id)
17
+ @person.should respond_to(:id=)
18
+ end
19
+
20
+ it "should be able to set and retrieve the id variable" do
21
+ @person.id = 'abc-def'
22
+ @person.id.should == 'abc-def'
23
+ end
24
+
25
+ end
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #new method with a hash containing one key-value pair" do
4
+ before(:all) do
5
+ class Person < ActiveCouch::Base
6
+ has :name
7
+ end
8
+ end
9
+
10
+ after(:all) do
11
+ Object.send(:remove_const, :Person)
12
+ end
13
+
14
+ it "should be able to initialize attributes correctly from a hash" do
15
+ p = Person.new(:name => 'McLovin')
16
+ p.name.should == 'McLovin'
17
+ end
18
+ end
19
+
20
+ describe "ActiveCouch::Base #new method with a hash containing more than one key-value pair" do
21
+ before(:all) do
22
+ class Person < ActiveCouch::Base
23
+ has :name
24
+ has :age, :which_is => :number, :with_default_value => 25
25
+ end
26
+ end
27
+
28
+ after(:all) do
29
+ Object.send(:remove_const, :Person)
30
+ end
31
+
32
+ it "should be able to initialize attributes correctly from the hash" do
33
+ p = Person.new(:name => 'McLovin', :age => 12)
34
+ p.name.should == 'McLovin'
35
+ p.age.should == 12
36
+ end
37
+ end
38
+
39
+ describe "ActiveCouch::Base #new method with a hash containing a CouchDB reserved attribute" do
40
+ before(:all) do
41
+ class Person < ActiveCouch::Base
42
+ has :name
43
+ end
44
+ end
45
+
46
+ after(:all) do
47
+ Object.send(:remove_const, :Person)
48
+ end
49
+
50
+ it "should be able to initialize attributes correctly, including CouchDB reserved attributes" do
51
+ p = Person.new(:name => 'McLovin', :id => '123')
52
+ p.name.should == 'McLovin'
53
+ p.id.should == '123'
54
+ end
55
+ end
56
+
57
+ describe "ActiveCouch::Base #new method with a block (and self being passed to the block)" do
58
+ before(:all) do
59
+ class Dog < ActiveCouch::Base
60
+ has :name
61
+ has :age, :which_is => :number
62
+ has :collar
63
+ end
64
+ end
65
+
66
+ after(:all) do
67
+ Object.send(:remove_const, :Dog)
68
+ end
69
+
70
+ it "should be able to initialize all the attributes correctly" do
71
+ dog = Dog.new do |d|
72
+ d.name = "Buster"
73
+ d.age = 2
74
+ d.collar = "Stray"
75
+ end
76
+
77
+ dog.name.should == "Buster"
78
+ dog.age.should == 2
79
+ dog.collar.should == "Stray"
80
+ end
81
+
82
+ it "should be able to initialize all attributes (including CouchDB reserved attributes)" do
83
+ dog = Dog.new do |d|
84
+ d.name = "Spike"
85
+ d.id = 'underdog_1'
86
+ end
87
+
88
+ dog.name.should == "Spike"
89
+ dog.id.should == 'underdog_1'
90
+ end
91
+ end
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #marshal_dump method with just simple attributes" do
4
+ before(:each) do
5
+ class Hotel < ActiveCouch::Base
6
+ has :name, :which_is => :text, :with_default_value => "Swissotel The Stamford"
7
+ has :star_rating, :which_is => :decimal, :with_default_value => 5.0
8
+ has :rooms, :which_is => :number, :with_default_value => 100
9
+ end
10
+
11
+ @h = Hotel.new
12
+ end
13
+
14
+ after(:each) do
15
+ Object.send(:remove_const, :Hotel)
16
+ end
17
+
18
+ it "should have to the marshal_dump method" do
19
+ @h.should respond_to(:marshal_dump)
20
+ end
21
+
22
+ it "should produce valid JSON output when sent the marshal_dump method" do
23
+ @h.stub!(:to_json).and_return("Deflated JSON")
24
+ @h.marshal_dump.should == 'Deflated JSON'
25
+ end
26
+
27
+ it "should produce valid JSON output when an attribute has been changed and the marshal_dump method is sent" do
28
+ @h.rooms = 200
29
+ @h.stub!(:to_json).and_return("Deflated JSON, Part deux")
30
+ @h.marshal_dump.should == 'Deflated JSON, Part deux'
31
+ end
32
+ end
33
+
34
+ describe "ActiveCouch::Base #marshal_dump with associations" do
35
+ before(:each) do
36
+ class Hospital < ActiveCouch::Base
37
+ has :name
38
+ end
39
+
40
+ class CrazyPerson < ActiveCouch::Base
41
+ has :name, :which_is => :text, :with_default_value => "Crazed McLovin"
42
+ has_many :hospitals
43
+ end
44
+
45
+ @c = CrazyPerson.new
46
+
47
+ @h1 = Hospital.new(:name => "Crazy Hospital 1")
48
+ @h2 = Hospital.new(:name => "Crazy Hospital 2")
49
+
50
+ @c.add_hospital(@h1)
51
+ @c.add_hospital(@h2)
52
+ end
53
+
54
+ after(:each) do
55
+ Object.send(:remove_const, :Hospital)
56
+ Object.send(:remove_const, :CrazyPerson)
57
+ end
58
+
59
+ it "should produce valid JSON when sent the marshal_dump method" do
60
+ # Stub the deflate method, which will basically give us the gzip'd JSON
61
+ @c.stub!(:to_json).and_return("Deflated JSON, Part three")
62
+ @c.marshal_dump.should == 'Deflated JSON, Part three'
63
+ end
64
+ end
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #marshal_load method, with many attributes" do
4
+ before(:all) do
5
+ class Hotel < ActiveCouch::Base
6
+ has :name, :which_is => :text, :with_default_value => "Swissotel The Stamford"
7
+ has :star_rating, :which_is => :decimal, :with_default_value => 5.0
8
+ has :rooms, :which_is => :number, :with_default_value => 100
9
+ end
10
+
11
+ class Hospital < ActiveCouch::Base
12
+ has :name
13
+ end
14
+
15
+ class CrazyPerson < ActiveCouch::Base
16
+ has :name, :which_is => :text, :with_default_value => "Crazed McLovin"
17
+ has_many :hospitals
18
+ end
19
+ end
20
+
21
+ after(:all) do
22
+ Object.send(:remove_const, :Hotel)
23
+ Object.send(:remove_const, :Hospital)
24
+ Object.send(:remove_const, :CrazyPerson)
25
+ end
26
+
27
+ it "should have the marshal_load method" do
28
+ Hotel.new.should respond_to(:marshal_load)
29
+ Hospital.new.should respond_to(:marshal_load)
30
+ CrazyPerson.new.should respond_to(:marshal_load)
31
+ end
32
+
33
+ it "should instantiate an object when sent the marshal_load method with valid json as a parameter" do
34
+ h = Hotel.new
35
+
36
+ h = h.marshal_load("{\"name\":\"Swissotel The Stamford\",\"rooms\":200,\"star_rating\":4.0}")
37
+ h.class.should == Hotel
38
+ # Check whether all attributes are set correctly
39
+ h.name.should == "Swissotel The Stamford"
40
+ h.rooms.should == 200
41
+ h.star_rating.should == 4.0
42
+ end
43
+
44
+ it "should instantiate an object when sent the marshal_load method with valid JSON (containing associations) as a parameter" do
45
+
46
+ crazy = CrazyPerson.new.marshal_load('{"name":"Crazed McLovin","hospitals":[{"name":"Crazy Hospital 1"},{"name":"Crazy Hospital 2"}]}')
47
+ crazy.class.should == CrazyPerson
48
+
49
+ crazy.name == "Crazed McLovin"
50
+ crazy.hospitals.size.should == 2
51
+
52
+ hospitals = crazy.hospitals.collect{|h| h.name }
53
+ hospitals.sort!
54
+
55
+ hospitals.first.should == 'Crazy Hospital 1'
56
+ hospitals.last.should == 'Crazy Hospital 2'
57
+ end
58
+ end