couchmodel 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,16 +6,17 @@ implementation to integrate into an Rails 3 application.
6
6
 
7
7
  The current version is under development and open for everyone to find bugs and post them into the issue tracker.
8
8
 
9
- The code has been tested Ruby 1.9.1, CouchDB 0.10.0 and Rails 3.0.0.beta.
9
+ The code has been tested Ruby 1.8.7 and 1.9.1, CouchDB 0.10.0 and Rails 3.0.0.beta3.
10
10
 
11
11
  == Dependencies
12
12
 
13
- If CouchModel is used without Rails, the ruby standard library (tested with 1.9.1) if the only requirement.
13
+ Basically, the ruby standard library (1.9.1) if the only requirement. If you still using Ruby 1.8, the json gem is
14
+ required.
14
15
 
15
16
  If the activemodel gem is installed, CouchModel automatically provides an interface to Rails 3.
16
17
 
17
- To run the test suite, <tt>rspec</tt> (tested with 1.2.9) is required. A CouchDB instance is just required for the
18
- integration tests (task <tt>spec:integration</tt>).
18
+ To run the test suite, rspec (tested with 1.2.9) is required. A CouchDB instance is only required for the integration
19
+ tests (task <tt>spec:integration</tt>).
19
20
 
20
21
  == Installation
21
22
 
data/Rakefile CHANGED
@@ -9,8 +9,8 @@ task :default => :spec
9
9
 
10
10
  specification = Gem::Specification.new do |specification|
11
11
  specification.name = "couchmodel"
12
- specification.version = "0.1.0"
13
- specification.date = "2010-03-31"
12
+ specification.version = "0.1.1"
13
+ specification.date = "2010-05-05"
14
14
 
15
15
  specification.authors = [ "Philipp Bruell" ]
16
16
  specification.email = "b.phifty@gmail.com"
@@ -26,6 +26,8 @@ specification = Gem::Specification.new do |specification|
26
26
  specification.require_path = "lib"
27
27
 
28
28
  specification.test_files = Dir["spec/**/*_spec.rb"]
29
+
30
+ specification.add_development_dependency "rspec"
29
31
  end
30
32
 
31
33
  Rake::GemPackageTask.new(specification) do |package|
@@ -3,7 +3,13 @@ class String # :nodoc:
3
3
 
4
4
  # This method converts a CamelCaseString into an underscore_string.
5
5
  def underscore
6
- self.gsub(/([a-z][A-Z])/){ |match| "#{match[0]}_#{match[1]}" }.downcase
6
+ word = self.to_s.dup
7
+ word.gsub!(/::/, '/')
8
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
9
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
10
+ word.tr!("-", "_")
11
+ word.downcase!
12
+ word
7
13
  end
8
14
 
9
15
  # This method converts an underscore_string into a CamelCaseString.
@@ -37,10 +37,14 @@ module CouchModel
37
37
 
38
38
  alias new_record? new?
39
39
 
40
+ def persisted?
41
+ !new?
42
+ end
43
+
40
44
  alias destroyed? new?
41
45
 
42
46
  def to_param
43
- id
47
+ persisted? ? id : nil
44
48
  end
45
49
 
46
50
  alias save_without_active_model save
@@ -29,7 +29,7 @@ module CouchModel
29
29
  end
30
30
 
31
31
  def filename
32
- @filename ||= File.join(CouchModel::Configuration.design_directory, "#{model_class.to_s.underscore}.design")
32
+ @filename ||= File.join(CouchModel::Configuration.design_directory, "#{@model_class.to_s.underscore}.design")
33
33
  end
34
34
 
35
35
  def load_file
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "models"))
3
+
4
+ describe "integration" do
5
+
6
+ use_real_transport!
7
+
8
+ before :each do
9
+ create_users_and_memberships
10
+ end
11
+
12
+ describe "belongs_to" do
13
+
14
+ it "should return the related model" do
15
+ @membership_one.user.should == @user_one
16
+ @membership_two.user.should == @user_two
17
+ end
18
+
19
+ end
20
+
21
+ describe "has_many" do
22
+
23
+ it "should include the related model" do
24
+ @user_one.memberships.should include(@membership_one)
25
+ @user_two.memberships.should include(@membership_two)
26
+ end
27
+
28
+ it "should not include the not-related model" do
29
+ @user_one.memberships.should_not include(@membership_two)
30
+ @user_two.memberships.should_not include(@membership_one)
31
+ end
32
+
33
+ it "should use the selector" do
34
+ @user_one.memberships("yesterday").should include(@membership_one)
35
+ @user_one.memberships("today").should_not include(@membership_one)
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "couch_model"))
2
+
3
+ CouchModel::Configuration.design_directory = File.join File.dirname(__FILE__), "design"
4
+
5
+ DATABASE = {
6
+ :url => "http://localhost:5984/test",
7
+ :create_if_missing => true,
8
+ :delete_if_exists => true,
9
+ :push_design => true
10
+ }.freeze unless defined?(DATABASE)
11
+
12
+ class User < CouchModel::Base
13
+
14
+ setup_database DATABASE
15
+
16
+ key_accessor :username
17
+ key_accessor :email, :default => "no email"
18
+
19
+ has_many :memberships,
20
+ :class_name => "Membership",
21
+ :view_name => :by_user_id_and_created_at,
22
+ :query => lambda { |created_at| { :startkey => [ self.id, (created_at || nil) ], :endkey => [ self.id, (created_at || { }) ] } }
23
+
24
+ end
25
+
26
+ class Membership < CouchModel::Base
27
+
28
+ setup_database DATABASE
29
+
30
+ key_accessor :created_at
31
+
32
+ belongs_to :user, :class_name => "User"
33
+
34
+ end
35
+
36
+ def create_users_and_memberships
37
+ @user_one = User.create :username => "user one"
38
+ @user_two = User.create :username => "user two"
39
+ @membership_one = Membership.create :created_at => "yesterday", :user => @user_one
40
+ @membership_two = Membership.create :created_at => "yesterday", :user => @user_two
41
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "models"))
3
+
4
+ describe "integration" do
5
+
6
+ use_real_transport!
7
+
8
+ before :each do
9
+ @user = User.new :username => "user"
10
+ end
11
+
12
+ describe "setup" do
13
+
14
+ it "should have been created the database" do
15
+ User.database.exists?.should be_true
16
+ end
17
+
18
+ it "should have been created the design" do
19
+ User.design.exists?.should be_true
20
+ end
21
+
22
+ it "should setup unique databases" do
23
+ User.database.should === Membership.database
24
+ end
25
+
26
+ it "should setup designs for each model" do
27
+ User.design.should_not == Membership.design
28
+ end
29
+
30
+ end
31
+
32
+ describe "save" do
33
+
34
+ it "should create the model" do
35
+ @user.save
36
+ @user.should_not be_new
37
+ end
38
+
39
+ it "should return true" do
40
+ @user.save.should be_true
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "models"))
3
+
4
+ describe "integration" do
5
+
6
+ use_real_transport!
7
+
8
+ before :each do
9
+ create_users_and_memberships
10
+ end
11
+
12
+ describe "save" do
13
+
14
+ before :each do
15
+ @user_one.username = "new username"
16
+ end
17
+
18
+ it "should update the model" do
19
+ @user_one.save
20
+ @user_one.username.should == "new username"
21
+ end
22
+
23
+ it "should return true" do
24
+ @user_one.save.should be_true
25
+ end
26
+
27
+ end
28
+
29
+ describe "destroy" do
30
+
31
+ it "should return true" do
32
+ @user_one.destroy.should be_true
33
+ end
34
+
35
+ it "should set the model to new" do
36
+ @user_one.destroy
37
+ @user_one.should be_new
38
+ end
39
+
40
+ end
41
+
42
+ describe "count" do
43
+
44
+ it "should return the number of users" do
45
+ User.count.should >= 2
46
+ end
47
+
48
+ end
49
+
50
+ describe "all" do
51
+
52
+ it "should include the saved user" do
53
+ begin
54
+ User.all.should include(@user_one)
55
+ rescue Object => error
56
+ puts error.backtrace
57
+ raise error
58
+ end
59
+ User.all.should include(@user_two)
60
+ end
61
+
62
+ end
63
+
64
+ describe "user_count" do
65
+
66
+ before :each do
67
+ @rows = User.user_count :returns => :rows
68
+ end
69
+
70
+ it "should return the user count" do
71
+ @rows.first.value.should >= 2
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "models"))
3
+
4
+ describe "integration" do
5
+
6
+ use_real_transport!
7
+
8
+ before :each do
9
+ create_users_and_memberships
10
+ end
11
+
12
+ describe "all" do
13
+
14
+ it "should include the saved user" do
15
+ begin
16
+ User.all.should include(@user_one)
17
+ rescue Object => error
18
+ puts error.backtrace
19
+ raise error
20
+ end
21
+ User.all.should include(@user_two)
22
+ end
23
+
24
+ end
25
+
26
+ describe "user_count" do
27
+
28
+ before :each do
29
+ @rows = User.user_count :returns => :rows
30
+ end
31
+
32
+ it "should return the user count" do
33
+ @rows.first.value.should >= 2
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -36,6 +36,16 @@ describe ActiveTestModel do
36
36
  @model = ActiveTestModel.new :id => "test_model_1"
37
37
  end
38
38
 
39
+ it "should fullfill all the lint tests" do
40
+ test_to_key
41
+ test_to_param
42
+ test_valid?
43
+ test_persisted?
44
+ test_model_naming
45
+ test_errors_aref
46
+ test_errors_full_messages
47
+ end
48
+
39
49
  describe "initialize" do
40
50
 
41
51
  it "should call the initialize callback" do
@@ -44,20 +54,8 @@ describe ActiveTestModel do
44
54
 
45
55
  end
46
56
 
47
- describe "new_record?" do
48
-
49
- it "should fullfill the lint test" do
50
- test_new_record?
51
- end
52
-
53
- end
54
-
55
57
  describe "destroyed?" do
56
58
 
57
- it "should fullfill the lint test" do
58
- test_destroyed?
59
- end
60
-
61
59
  it "should return true if model is new" do
62
60
  @model.stub!(:new?).and_return(true)
63
61
  @model.should be_destroyed
@@ -65,14 +63,6 @@ describe ActiveTestModel do
65
63
 
66
64
  end
67
65
 
68
- describe "naming" do
69
-
70
- it "should fullfill the lint test" do
71
- test_model_naming
72
- end
73
-
74
- end
75
-
76
66
  describe "valid?" do
77
67
 
78
68
  it "should be true with a given name" do
@@ -125,6 +115,10 @@ describe ActiveTestModel do
125
115
 
126
116
  describe "to_param" do
127
117
 
118
+ before :each do
119
+ @model.stub!(:persisted?).and_return(true)
120
+ end
121
+
128
122
  it "should return the model's id" do
129
123
  @model.to_param.should == @model.id
130
124
  end
@@ -244,7 +238,10 @@ describe ActiveTestModel do
244
238
  end
245
239
 
246
240
  it "should return all attributes as json" do
247
- @model.to_json.should == "{\"_id\":\"test_model_1\",\"email\":\"test\",\"model_class\":\"ActiveTestModel\",\"name\":\"test\"}"
241
+ @model.to_json.should =~ /"_id":"test_model_1"/
242
+ @model.to_json.should =~ /"email":"test"/
243
+ @model.to_json.should =~ /"model_class":"ActiveTestModel"/
244
+ @model.to_json.should =~ /"name":"test"/
248
245
  end
249
246
 
250
247
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Philipp Bruell
@@ -14,10 +14,21 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-31 00:00:00 +02:00
17
+ date: 2010-05-05 00:00:00 +02:00
18
18
  default_executable:
19
- dependencies: []
20
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
21
32
  description: CouchModel provides an interface to easly handle CouchDB documents. It also comes with a ActiveModel implementation to integrate into an Rails 3 application.
22
33
  email: b.phifty@gmail.com
23
34
  executables: []
@@ -70,9 +81,13 @@ files:
70
81
  - spec/lib/core_extension/string_spec.rb
71
82
  - spec/lib/core_extension/array_spec.rb
72
83
  - spec/fake_transport_helper.rb
84
+ - spec/integration/views_spec.rb
85
+ - spec/integration/associations_spec.rb
73
86
  - spec/integration/design/membership.design
74
87
  - spec/integration/design/user.design
75
- - spec/integration/basic_spec.rb
88
+ - spec/integration/saved_model_spec.rb
89
+ - spec/integration/new_model_spec.rb
90
+ - spec/integration/models.rb
76
91
  - spec/fake_transport.yml
77
92
  has_rdoc: true
78
93
  homepage: http://github.com/phifty/couchmodel
@@ -122,4 +137,7 @@ test_files:
122
137
  - spec/lib/couch_model_spec.rb
123
138
  - spec/lib/core_extension/string_spec.rb
124
139
  - spec/lib/core_extension/array_spec.rb
125
- - spec/integration/basic_spec.rb
140
+ - spec/integration/views_spec.rb
141
+ - spec/integration/associations_spec.rb
142
+ - spec/integration/saved_model_spec.rb
143
+ - spec/integration/new_model_spec.rb
@@ -1,185 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "couch_model"))
3
-
4
- CouchModel::Configuration.design_directory = File.join File.dirname(__FILE__), "design"
5
-
6
- DATABASE = {
7
- :url => "http://localhost:5984/test",
8
- :create_if_missing => true,
9
- :delete_if_exists => true,
10
- :push_design => true
11
- }.freeze unless defined?(DATABASE)
12
-
13
- class User < CouchModel::Base
14
-
15
- setup_database DATABASE
16
-
17
- key_accessor :username
18
- key_accessor :email, :default => "no email"
19
-
20
- has_many :memberships,
21
- :class_name => "Membership",
22
- :view_name => :by_user_id_and_created_at,
23
- :query => lambda { |created_at| { :startkey => [ self.id, (created_at || nil) ], :endkey => [ self.id, (created_at || { }) ] } }
24
-
25
- end
26
-
27
- class Membership < CouchModel::Base
28
-
29
- setup_database DATABASE
30
-
31
- key_accessor :created_at
32
-
33
- belongs_to :user, :class_name => "User"
34
-
35
- end
36
-
37
- describe "integration" do
38
-
39
- use_real_transport!
40
-
41
- context "on new models" do
42
-
43
- before :each do
44
- @user = User.new :username => "user"
45
- end
46
-
47
- describe "setup" do
48
-
49
- it "should have been created the database" do
50
- User.database.exists?.should be_true
51
- end
52
-
53
- it "should have been created the design" do
54
- User.design.exists?.should be_true
55
- end
56
-
57
- it "should setup unique databases" do
58
- User.database.should === Membership.database
59
- end
60
-
61
- it "should setup designs for each model" do
62
- User.design.should_not == Membership.design
63
- end
64
-
65
- end
66
-
67
- describe "save" do
68
-
69
- it "should create the model" do
70
- @user.save
71
- @user.should_not be_new
72
- end
73
-
74
- it "should return true" do
75
- @user.save.should be_true
76
- end
77
-
78
- end
79
-
80
- end
81
-
82
- context "on saved models" do
83
-
84
- before :each do
85
- @user_one = User.create :username => "user one"
86
- @user_two = User.create :username => "user two"
87
- @membership_one = Membership.create :created_at => "yesterday", :user => @user_one
88
- @membership_two = Membership.create :created_at => "yesterday", :user => @user_two
89
- end
90
-
91
- describe "save" do
92
-
93
- before :each do
94
- @user_one.username = "new username"
95
- end
96
-
97
- it "should update the model" do
98
- @user_one.save
99
- @user_one.username.should == "new username"
100
- end
101
-
102
- it "should return true" do
103
- @user_one.save.should be_true
104
- end
105
-
106
- end
107
-
108
- describe "destroy" do
109
-
110
- it "should return true" do
111
- @user_one.destroy.should be_true
112
- end
113
-
114
- it "should set the model to new" do
115
- @user_one.destroy
116
- @user_one.should be_new
117
- end
118
-
119
- end
120
-
121
- describe "all" do
122
-
123
- it "should include the saved user" do
124
- begin
125
- User.all.should include(@user_one)
126
- rescue Object => error
127
- puts error.backtrace
128
- raise error
129
- end
130
- User.all.should include(@user_two)
131
- end
132
-
133
- end
134
-
135
- describe "count" do
136
-
137
- it "should return the number of users" do
138
- User.count.should >= 2
139
- end
140
-
141
- end
142
-
143
- describe "belongs_to" do
144
-
145
- it "should return the related model" do
146
- @membership_one.user.should == @user_one
147
- @membership_two.user.should == @user_two
148
- end
149
-
150
- end
151
-
152
- describe "has_many" do
153
-
154
- it "should include the related model" do
155
- @user_one.memberships.should include(@membership_one)
156
- @user_two.memberships.should include(@membership_two)
157
- end
158
-
159
- it "should not include the not-related model" do
160
- @user_one.memberships.should_not include(@membership_two)
161
- @user_two.memberships.should_not include(@membership_one)
162
- end
163
-
164
- it "should use the selector" do
165
- @user_one.memberships("yesterday").should include(@membership_one)
166
- @user_one.memberships("today").should_not include(@membership_one)
167
- end
168
-
169
- end
170
-
171
- describe "user_count" do
172
-
173
- before :each do
174
- @rows = User.user_count :returns => :rows
175
- end
176
-
177
- it "should return the user count" do
178
- @rows.first.value.should >= 2
179
- end
180
-
181
- end
182
-
183
- end
184
-
185
- end