couchmodel 0.1.0.beta2 → 0.1.0.beta3
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.
- data/README.rdoc +13 -10
- data/lib/couch_model/active_model.rb +3 -2
- data/lib/couch_model/base/setup.rb +9 -5
- data/lib/couch_model/base.rb +1 -1
- data/lib/couch_model/configuration.rb +5 -1
- data/lib/couch_model/database.rb +6 -9
- data/lib/couch_model/transport.rb +1 -1
- data/spec/fake_transport.yml +1 -1
- data/spec/integration/basic_spec.rb +43 -2
- data/spec/lib/couch_model/active_model_spec.rb +5 -0
- data/spec/lib/couch_model/configuration_spec.rb +12 -25
- data/spec/lib/couch_model/core/setup_spec.rb +13 -5
- data/spec/lib/couch_model/database_spec.rb +29 -47
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -29,9 +29,10 @@ To define a model, it's necessary to create a subclass of <tt>CouchModel::Base</
|
|
29
29
|
|
30
30
|
class User < CouchModel::Base
|
31
31
|
|
32
|
-
setup_database :url
|
33
|
-
:
|
34
|
-
:delete_if_exists
|
32
|
+
setup_database :url => "http://localhost:5984/test",
|
33
|
+
:create_if_missing => true,
|
34
|
+
:delete_if_exists => false,
|
35
|
+
:push_design => true
|
35
36
|
|
36
37
|
key_accessor :name
|
37
38
|
key_accessor :email
|
@@ -39,11 +40,13 @@ To define a model, it's necessary to create a subclass of <tt>CouchModel::Base</
|
|
39
40
|
end
|
40
41
|
|
41
42
|
The <tt>setup_database</tt> method defines a database for the model. The +url+ option is required and specifies the url
|
42
|
-
of the database in the scheme <
|
43
|
-
<tt>
|
44
|
-
initialized. If the option <tt>delete_if_exists</tt> is specified, the database will be deleted and re-created.
|
45
|
-
option <tt>
|
46
|
-
<tt>
|
43
|
+
of the database in the scheme <tt>[scheme]://[host]:[port]/[database_name]</tt>. If the option
|
44
|
+
<tt>create_if_missing</tt> is set to true, CouchModel will try to create the database when the model is
|
45
|
+
initialized. If the option <tt>delete_if_exists</tt> is specified, the database will be deleted and re-created. The
|
46
|
+
option <tt>push_design</tt> will make CouchModel upload the design document assigned to the model during it's
|
47
|
+
initialization process. If the option <tt>create_if_missing</tt> is not specified or false, the database setup be done
|
48
|
+
manually by calling <tt>CouchModel::Configuration.setup_databases</tt> and
|
49
|
+
<tt>CouchModel::Configuration.setup_designs</tt>.
|
47
50
|
|
48
51
|
The method <tt>key_accessor</tt> defined access methods to the given keys of the CouchDB document. It's also possible
|
49
52
|
to use <tt>key_reader</tt> and <tt>key_writer</tt> here.
|
@@ -55,7 +58,7 @@ Each defined model has a realted design document, that keeps all the views for t
|
|
55
58
|
CouchModel::Configuration.design_directory = "[directory]"
|
56
59
|
|
57
60
|
a directory is specfied that keeps all the design document. CouchModel will watch out for a file with the name
|
58
|
-
<
|
61
|
+
<tt>[design directory]/[model_name].design</tt> and will use it as the related design document. If no such file exists,
|
59
62
|
a design document will be created (but not saved to the file). The design ducument can be asscessed via
|
60
63
|
<tt>Model.design</tt>.
|
61
64
|
|
@@ -109,7 +112,7 @@ In this example, the <tt>belongs_to</tt> adds a <tt>key_accessor</tt> named <tt>
|
|
109
112
|
generates getters and setters for the session object itself (<tt>session</tt> and <tt>session=</tt>).
|
110
113
|
|
111
114
|
The <tt>has_many</tt> acts as a wrapper for the specified view. The previously defined view
|
112
|
-
<
|
115
|
+
<tt>by_user_id_and_created_at</tt> emits membership-documents by thier <tt>user_id</tt> and the <tt>created_at</tt>
|
113
116
|
date. The given query option specifes a method that returns a query hash for the specifed view. The arguments for this
|
114
117
|
method can be passed membership association method.
|
115
118
|
|
@@ -35,10 +35,11 @@ module CouchModel
|
|
35
35
|
|
36
36
|
alias destroyed? new?
|
37
37
|
|
38
|
-
alias
|
38
|
+
alias save_without_active_model save
|
39
39
|
|
40
40
|
def save
|
41
|
-
|
41
|
+
return false unless valid?
|
42
|
+
result = save_without_active_model
|
42
43
|
discard_changes!
|
43
44
|
result
|
44
45
|
end
|
@@ -50,15 +50,19 @@ module CouchModel
|
|
50
50
|
private
|
51
51
|
|
52
52
|
def initialize_database(options)
|
53
|
-
url
|
54
|
-
|
53
|
+
url = options[:url] || raise(ArgumentError, "no url was given to define the database")
|
54
|
+
delete_if_exists = options[:delete_if_exists] || false
|
55
|
+
create_if_missing = options[:create_if_missing] || false
|
55
56
|
|
56
57
|
uri = URI.parse url
|
57
58
|
server = Server.new :host => uri.host, :port => uri.port
|
58
59
|
database = Database.new :server => server, :name => uri.path.gsub("/", "")
|
59
60
|
@database = Configuration.register_database database
|
60
61
|
|
61
|
-
|
62
|
+
if @database === database
|
63
|
+
@database.delete_if_exists! if delete_if_exists
|
64
|
+
@database.create_if_missing! if create_if_missing
|
65
|
+
end
|
62
66
|
end
|
63
67
|
|
64
68
|
def initialize_design
|
@@ -71,8 +75,8 @@ module CouchModel
|
|
71
75
|
end
|
72
76
|
|
73
77
|
def push_design(options)
|
74
|
-
|
75
|
-
@design.push if
|
78
|
+
push_design = options[:push_design] || false
|
79
|
+
@design.push if push_design
|
76
80
|
end
|
77
81
|
|
78
82
|
def find_view(name)
|
data/lib/couch_model/base.rb
CHANGED
@@ -105,7 +105,7 @@ module CouchModel
|
|
105
105
|
end
|
106
106
|
|
107
107
|
def update
|
108
|
-
response = Transport.request :put, self.url, :json => self.attributes, :expected_status_code =>
|
108
|
+
response = Transport.request :put, self.url, :json => self.attributes, :expected_status_code => 201
|
109
109
|
self.rev = response["rev"]
|
110
110
|
true
|
111
111
|
rescue Transport::UnexpectedStatusCodeError
|
@@ -42,8 +42,12 @@ module CouchModel
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def setup_databases(options = { })
|
45
|
+
delete_if_exists = options[:delete_if_exists] || false
|
46
|
+
create_if_missing = options[:create_if_missing] || false
|
47
|
+
|
45
48
|
@@databases.each do |database|
|
46
|
-
database.
|
49
|
+
database.delete_if_exists! if delete_if_exists
|
50
|
+
database.create_if_missing! if create_if_missing
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
data/lib/couch_model/database.rb
CHANGED
@@ -28,19 +28,16 @@ module CouchModel
|
|
28
28
|
Transport.request :put, url, :expected_status_code => 201
|
29
29
|
end
|
30
30
|
|
31
|
+
def create_if_missing!
|
32
|
+
create! unless exists?
|
33
|
+
end
|
34
|
+
|
31
35
|
def delete!
|
32
36
|
Transport.request :delete, url, :expected_status_code => 200
|
33
37
|
end
|
34
38
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
if delete_if_exists
|
39
|
-
delete! if exists?
|
40
|
-
create!
|
41
|
-
else
|
42
|
-
create! unless exists?
|
43
|
-
end
|
39
|
+
def delete_if_exists!
|
40
|
+
delete! if exists?
|
44
41
|
end
|
45
42
|
|
46
43
|
def informations
|
@@ -49,7 +49,7 @@ module CouchModel
|
|
49
49
|
json = options[:json]
|
50
50
|
|
51
51
|
case request_class.to_s
|
52
|
-
when "Net::HTTP::Get"
|
52
|
+
when "Net::HTTP::Get", "Net::HTTP::Delete"
|
53
53
|
request_class.new uri.path +
|
54
54
|
(parameters.empty? ? "" : "?" + parameters.collect{ |key, value| "#{key}=#{URI.escape(value.to_s)}" }.reverse.join("&"))
|
55
55
|
when "Net::HTTP::Post", "Net::HTTP::Put"
|
data/spec/fake_transport.yml
CHANGED
@@ -3,9 +3,16 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "c
|
|
3
3
|
|
4
4
|
CouchModel::Configuration.design_directory = File.join File.dirname(__FILE__), "design"
|
5
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
|
+
|
6
13
|
class User < CouchModel::Base
|
7
14
|
|
8
|
-
setup_database
|
15
|
+
setup_database DATABASE
|
9
16
|
|
10
17
|
key_accessor :username
|
11
18
|
key_accessor :email
|
@@ -19,7 +26,7 @@ end
|
|
19
26
|
|
20
27
|
class Membership < CouchModel::Base
|
21
28
|
|
22
|
-
setup_database
|
29
|
+
setup_database DATABASE
|
23
30
|
|
24
31
|
key_accessor :created_at
|
25
32
|
|
@@ -64,6 +71,10 @@ describe "integration" do
|
|
64
71
|
@user.should_not be_new
|
65
72
|
end
|
66
73
|
|
74
|
+
it "should return true" do
|
75
|
+
@user.save.should be_true
|
76
|
+
end
|
77
|
+
|
67
78
|
end
|
68
79
|
|
69
80
|
end
|
@@ -83,6 +94,36 @@ describe "integration" do
|
|
83
94
|
@membership_two.save
|
84
95
|
end
|
85
96
|
|
97
|
+
describe "save" do
|
98
|
+
|
99
|
+
before :each do
|
100
|
+
@user_one.username = "new username"
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should update the model" do
|
104
|
+
@user_one.save
|
105
|
+
@user_one.username.should == "new username"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should return true" do
|
109
|
+
@user_one.save.should be_true
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "destroy" do
|
115
|
+
|
116
|
+
it "should return true" do
|
117
|
+
@user_one.destroy.should be_true
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should set the model to new" do
|
121
|
+
@user_one.destroy
|
122
|
+
@user_one.should be_new
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
86
127
|
describe "all" do
|
87
128
|
|
88
129
|
it "should include the saved user" do
|
@@ -42,38 +42,25 @@ describe CouchModel::Configuration do
|
|
42
42
|
|
43
43
|
before :each do
|
44
44
|
CouchModel::Configuration.class_variable_set :@@databases, [ ]
|
45
|
+
@database = CouchModel::Database.new :name => "test"
|
46
|
+
CouchModel::Configuration.register_database @database
|
47
|
+
|
48
|
+
@database.stub!(:delete_if_exists!)
|
49
|
+
@database.stub!(:create_if_missing!)
|
45
50
|
end
|
46
51
|
|
47
52
|
def do_setup
|
48
|
-
CouchModel::Configuration.setup_databases
|
53
|
+
CouchModel::Configuration.setup_databases :delete_if_exists => true, :create_if_missing => true
|
49
54
|
end
|
50
|
-
|
51
|
-
describe "for an existing database" do
|
52
|
-
|
53
|
-
before :each do
|
54
|
-
@database = CouchModel::Database.new :name => "test"
|
55
|
-
CouchModel::Configuration.register_database @database
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should not create the database" do
|
59
|
-
@database.should_not_receive(:create!)
|
60
|
-
do_setup
|
61
|
-
end
|
62
55
|
|
56
|
+
it "should call delete_if_exists!" do
|
57
|
+
@database.should_receive(:delete_if_exists!)
|
58
|
+
do_setup
|
63
59
|
end
|
64
60
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
@database = CouchModel::Database.new :name => "new_database"
|
69
|
-
CouchModel::Configuration.register_database @database
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should create the database" do
|
73
|
-
@database.should_receive(:create!)
|
74
|
-
do_setup
|
75
|
-
end
|
76
|
-
|
61
|
+
it "should call create_if_missing!" do
|
62
|
+
@database.should_receive(:create_if_missing!)
|
63
|
+
do_setup
|
77
64
|
end
|
78
65
|
|
79
66
|
end
|
@@ -41,10 +41,18 @@ describe SetupTestModel do
|
|
41
41
|
database.object_id.should == SetupTestModel.database.object_id
|
42
42
|
end
|
43
43
|
|
44
|
-
it "should
|
45
|
-
@options[:
|
44
|
+
it "should delete the database if requested" do
|
45
|
+
@options[:delete_if_exists] = true
|
46
46
|
database = SetupTestModel.database
|
47
|
-
database.should_receive(:
|
47
|
+
database.should_receive(:delete_if_exists!)
|
48
|
+
CouchModel::Database.stub!(:new).and_return(database)
|
49
|
+
do_setup
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should create the database if requested" do
|
53
|
+
@options[:create_if_missing] = true
|
54
|
+
database = SetupTestModel.database
|
55
|
+
database.should_receive(:create_if_missing!)
|
48
56
|
CouchModel::Database.stub!(:new).and_return(database)
|
49
57
|
do_setup
|
50
58
|
end
|
@@ -70,8 +78,8 @@ describe SetupTestModel do
|
|
70
78
|
SetupTestModel.test_view.should be_instance_of(CouchModel::Collection)
|
71
79
|
end
|
72
80
|
|
73
|
-
it "should push the design
|
74
|
-
@options[:
|
81
|
+
it "should push the design if requested" do
|
82
|
+
@options[:push_design] = true
|
75
83
|
@design.should_receive(:push)
|
76
84
|
do_setup
|
77
85
|
end
|
@@ -59,6 +59,26 @@ describe CouchModel::Database do
|
|
59
59
|
|
60
60
|
end
|
61
61
|
|
62
|
+
describe "create_if_missing!" do
|
63
|
+
|
64
|
+
before :each do
|
65
|
+
@database.stub!(:create!)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should not call create! if the database exists" do
|
69
|
+
@database.stub!(:exists?).and_return(true)
|
70
|
+
@database.should_not_receive(:create!)
|
71
|
+
@database.create_if_missing!
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should call create! if the database not exists" do
|
75
|
+
@database.stub!(:exists?).and_return(false)
|
76
|
+
@database.should_receive(:create!)
|
77
|
+
@database.create_if_missing!
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
62
82
|
describe "delete!" do
|
63
83
|
|
64
84
|
before :each do
|
@@ -73,60 +93,22 @@ describe CouchModel::Database do
|
|
73
93
|
|
74
94
|
end
|
75
95
|
|
76
|
-
describe "
|
96
|
+
describe "delete_if_exists!" do
|
77
97
|
|
78
98
|
before :each do
|
79
|
-
@database.stub!(:create!)
|
80
99
|
@database.stub!(:delete!)
|
81
|
-
@database.stub!(:exists?).and_return(true)
|
82
|
-
@options = { }
|
83
100
|
end
|
84
101
|
|
85
|
-
|
86
|
-
@database.
|
87
|
-
|
88
|
-
|
89
|
-
describe "with delete_if_exists set to true" do
|
90
|
-
|
91
|
-
before :each do
|
92
|
-
@options.merge! :delete_if_exists => true
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should delete an existing database" do
|
96
|
-
@database.should_receive(:delete!)
|
97
|
-
do_setup
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should not delete a not-existing database" do
|
101
|
-
@database.stub!(:exists?).and_return(false)
|
102
|
-
@database.should_not_receive(:delete!)
|
103
|
-
do_setup
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should create the database" do
|
107
|
-
@database.should_receive(:create!)
|
108
|
-
do_setup
|
109
|
-
end
|
110
|
-
|
102
|
+
it "should call delete! if the database exists" do
|
103
|
+
@database.stub!(:exists?).and_return(true)
|
104
|
+
@database.should_receive(:delete!)
|
105
|
+
@database.delete_if_exists!
|
111
106
|
end
|
112
107
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
it "should create a not-existing database" do
|
120
|
-
@database.stub!(:exists?).and_return(false)
|
121
|
-
@database.should_receive(:create!)
|
122
|
-
do_setup
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should not create an existing database" do
|
126
|
-
@database.should_not_receive(:create!)
|
127
|
-
do_setup
|
128
|
-
end
|
129
|
-
|
108
|
+
it "should not call delete! if the database not exists" do
|
109
|
+
@database.stub!(:exists?).and_return(false)
|
110
|
+
@database.should_not_receive(:delete!)
|
111
|
+
@database.delete_if_exists!
|
130
112
|
end
|
131
113
|
|
132
114
|
end
|
metadata
CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
|
|
6
6
|
- 0
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.1.0.
|
9
|
+
- beta3
|
10
|
+
version: 0.1.0.beta3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Philipp Br\xC3\xBCll"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-03-
|
18
|
+
date: 2010-03-10 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|