ki 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/ki.gemspec +2 -2
- data/lib/db.rb +14 -6
- data/lib/model.rb +6 -6
- data/lib/util.rb +10 -0
- data/spec/db_spec.rb +7 -2
- data/spec/integration/integration_spec.rb +14 -14
- data/spec/model_spec.rb +7 -7
- data/spec/modules/callbacks_spec.rb +4 -4
- data/spec/modules/query_interface_spec.rb +1 -1
- data/spec/modules/restrictions_spec.rb +11 -2
- data/spec/req_spec.rb +10 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/ki.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "ki"
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Cristian Mircea Messel"]
|
12
|
-
s.date = "2013-03-
|
12
|
+
s.date = "2013-03-31"
|
13
13
|
s.description = "Fuck setting up a database for your prototypes. Some call it an ORM with a RESTful interface. Some say its for prototyping."
|
14
14
|
s.email = "mess110@gmail.com"
|
15
15
|
s.executables = ["ki"]
|
data/lib/db.rb
CHANGED
@@ -17,16 +17,24 @@ module Ki
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def find_all name
|
20
|
-
@db[name].find.collect{ |row| row }
|
20
|
+
@db[name].find.collect{ |row| row }.stringify_ids
|
21
21
|
end
|
22
22
|
|
23
23
|
def find name, id
|
24
|
-
@db[name].find( { '_id' => BSON::ObjectId(id) }).first
|
24
|
+
@db[name].find( { '_id' => BSON::ObjectId(id) }).to_a.stringify_ids.first
|
25
25
|
end
|
26
26
|
|
27
27
|
def find_by name, hash
|
28
|
-
|
29
|
-
|
28
|
+
if hash['_id']
|
29
|
+
hash['_id'] = BSON::ObjectId(hash['_id'])
|
30
|
+
end
|
31
|
+
|
32
|
+
if hash['id']
|
33
|
+
hash['_id'] = BSON::ObjectId(hash['id'])
|
34
|
+
hash.delete('id')
|
35
|
+
end
|
36
|
+
|
37
|
+
@db[name].find(hash).to_a.stringify_ids
|
30
38
|
end
|
31
39
|
|
32
40
|
def create name, hash
|
@@ -36,8 +44,8 @@ module Ki
|
|
36
44
|
# document doesn't update if :_id is in the hash
|
37
45
|
# all prev object data is deleted
|
38
46
|
def update name, hash
|
39
|
-
id = hash['
|
40
|
-
hash.delete('
|
47
|
+
id = hash['id']
|
48
|
+
hash.delete('id')
|
41
49
|
@db[name].update({:_id => BSON::ObjectId(id)}, hash)
|
42
50
|
id
|
43
51
|
end
|
data/lib/model.rb
CHANGED
@@ -37,20 +37,20 @@ module Ki
|
|
37
37
|
def create
|
38
38
|
check_for_required_attributes
|
39
39
|
check_for_unique_attributes
|
40
|
-
@hash['
|
40
|
+
@hash['id'] = @db.create(class_name, @req.params)
|
41
41
|
end
|
42
42
|
|
43
43
|
def update
|
44
44
|
raise 'params missing' unless @req.params
|
45
|
-
raise 'param
|
45
|
+
raise 'param id missing' unless @req.params['id']
|
46
46
|
check_for_required_attributes
|
47
|
-
@hash['
|
47
|
+
@hash['id'] = @db.update(class_name, @req.params)
|
48
48
|
end
|
49
49
|
|
50
50
|
def delete
|
51
51
|
raise 'params missing' unless @req.params
|
52
|
-
raise 'param
|
53
|
-
@hash = @db.delete(class_name, @req.params['
|
52
|
+
raise 'param id missing' unless @req.params['id']
|
53
|
+
@hash = @db.delete(class_name, @req.params['id'])
|
54
54
|
end
|
55
55
|
|
56
56
|
def to_json
|
@@ -105,7 +105,7 @@ module Ki
|
|
105
105
|
send "after_#{m.to_s}".to_sym
|
106
106
|
after_all
|
107
107
|
rescue BSON::InvalidObjectId
|
108
|
-
raise "invalid
|
108
|
+
raise "invalid id format: #{params['id']}"
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
data/lib/util.rb
CHANGED
data/spec/db_spec.rb
CHANGED
@@ -35,9 +35,14 @@ describe Ki::Db do
|
|
35
35
|
@db.find('zoidberg', post_id)['foo'].should == [1,2]
|
36
36
|
end
|
37
37
|
|
38
|
+
it "should find by _id, id, and objid" do
|
39
|
+
post_id = @db.create 'zoidberg', { :foo => [1,2] }
|
40
|
+
@db.find_by('zoidberg', {'id' => post_id})[0]['id'].should == post_id
|
41
|
+
end
|
42
|
+
|
38
43
|
it "should update a document" do
|
39
44
|
post_id = @db.create 'zoidberg', { :foo => [1,2], :bar => 'highj' }
|
40
|
-
@db.update 'zoidberg', { '
|
45
|
+
@db.update 'zoidberg', { 'id' => post_id, :foo => 3.1415 }
|
41
46
|
doc = @db.find('zoidberg', post_id)
|
42
47
|
doc['foo'].should == 3.1415
|
43
48
|
doc['bar'].should == nil
|
@@ -72,7 +77,7 @@ describe Ki::Db do
|
|
72
77
|
it "should transform _id in BSON::ObjectId when using find_by" do
|
73
78
|
hash = { :foo => [1,2], :bar => 'highj' }
|
74
79
|
id = @db.create 'zoidberg', hash
|
75
|
-
hash['
|
80
|
+
hash['id'] = id
|
76
81
|
r = @db.find_by 'zoidberg', hash
|
77
82
|
r.length.should == 1
|
78
83
|
end
|
@@ -46,16 +46,16 @@ describe 'Integration Tests' do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
expect {
|
49
|
-
User.new(ReqFactory.new(:post, { 'mail' => 'cool@beans.com'})).hash['
|
49
|
+
User.new(ReqFactory.new(:post, { 'mail' => 'cool@beans.com'})).hash['id']
|
50
50
|
}.to raise_error 'password missing'
|
51
51
|
|
52
52
|
expect {
|
53
|
-
id = User.new(ReqFactory.new(:post, { 'mail' => 'cool@beans.com', 'password' => '123'})).hash['
|
54
|
-
User.new(ReqFactory.new(:get, { '
|
53
|
+
id = User.new(ReqFactory.new(:post, { 'mail' => 'cool@beans.com', 'password' => '123'})).hash['id']
|
54
|
+
User.new(ReqFactory.new(:get, { 'id' => id })).hash[0]['password'].should == '123!'
|
55
55
|
}.to change(User, :count).by(1)
|
56
56
|
|
57
57
|
expect {
|
58
|
-
id = User.new(ReqFactory.new(:post, { 'mail' => 'cool@beans.com', 'password' => '123'})).hash['
|
58
|
+
id = User.new(ReqFactory.new(:post, { 'mail' => 'cool@beans.com', 'password' => '123'})).hash['id']
|
59
59
|
}.to raise_error 'mail not unique'
|
60
60
|
|
61
61
|
expect {
|
@@ -68,7 +68,7 @@ describe 'Integration Tests' do
|
|
68
68
|
|
69
69
|
# create storage
|
70
70
|
storage = Storage.new(ReqFactory.new(:post, {'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [1,2,3]}))
|
71
|
-
id = storage.hash['
|
71
|
+
id = storage.hash['id']
|
72
72
|
r = Storage.new(ReqFactory.new(:get, { 'mail' => 'cool@beans.com', 'password' => '123'}))
|
73
73
|
r.hash[0]['all_my_data'].should == [1,2,3]
|
74
74
|
r.hash[0]['password'].should == nil
|
@@ -78,36 +78,36 @@ describe 'Integration Tests' do
|
|
78
78
|
}.to raise_error 'not authorized'
|
79
79
|
|
80
80
|
expect {
|
81
|
-
Storage.new(ReqFactory.new(:delete, {'
|
81
|
+
Storage.new(ReqFactory.new(:delete, {'id' => id}))
|
82
82
|
}.to raise_error 'not authorized'
|
83
83
|
|
84
84
|
expect {
|
85
85
|
Storage.new(ReqFactory.new(:delete, {'mail' => 'cool@beans.com', 'password' => '123'}))
|
86
|
-
}.to raise_error 'param
|
86
|
+
}.to raise_error 'param id missing'
|
87
87
|
|
88
88
|
# delete from storage
|
89
89
|
expect {
|
90
|
-
Storage.new(ReqFactory.new(:delete, {'
|
90
|
+
Storage.new(ReqFactory.new(:delete, {'id' => id, 'mail' => 'cool@beans.com', 'password' => '123'}))
|
91
91
|
}.to change(Storage, :count).by(-1)
|
92
92
|
|
93
93
|
# not allowed to delete user
|
94
94
|
expect {
|
95
|
-
id = User.new(ReqFactory.new(:post, { 'mail' => 'other@beans.com', 'password' => '123'})).hash['
|
96
|
-
p User.new(ReqFactory.new(:delete, { '
|
95
|
+
id = User.new(ReqFactory.new(:post, { 'mail' => 'other@beans.com', 'password' => '123'})).hash['id']
|
96
|
+
p User.new(ReqFactory.new(:delete, { 'id' => id }))
|
97
97
|
}.to raise_error 'action forbidden'
|
98
98
|
|
99
99
|
storage = Storage.new(ReqFactory.new(:post, {'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [4,4,5]}))
|
100
|
-
id = storage.hash['
|
100
|
+
id = storage.hash['id']
|
101
101
|
expect {
|
102
102
|
Storage.new(ReqFactory.new(:put, {'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [1]}))
|
103
|
-
}.to raise_error 'param
|
103
|
+
}.to raise_error 'param id missing'
|
104
104
|
|
105
105
|
expect {
|
106
106
|
Storage.new(ReqFactory.new(:put, {'mail' => 'cool@beans.com', 'password' => '1234', 'all_my_data' => [1]}))
|
107
107
|
}.to raise_error 'not authorized'
|
108
108
|
|
109
|
-
storage = Storage.new(ReqFactory.new(:put, {'
|
110
|
-
storage.hash['
|
109
|
+
storage = Storage.new(ReqFactory.new(:put, {'id' => id, 'mail' => 'cool@beans.com', 'password' => '123', 'all_my_data' => [1]}))
|
110
|
+
storage.hash['id'].should == id
|
111
111
|
storage = Storage.find(id)
|
112
112
|
storage['all_my_data'].should == [1]
|
113
113
|
end
|
data/spec/model_spec.rb
CHANGED
@@ -17,13 +17,13 @@ describe Ki::Model do
|
|
17
17
|
@db.db['zoidberg'].count.should == 0
|
18
18
|
hash = Zoidberg.new(post).hash
|
19
19
|
@db.db['zoidberg'].count.should == 1
|
20
|
-
@db.find('zoidberg', hash['
|
20
|
+
@db.find('zoidberg', hash['id'])['user'].should == 'homer'
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should get a model depending on id" do
|
24
24
|
id = @db.create 'zoidberg', { :name => 'homer' }
|
25
25
|
|
26
|
-
get = ReqFactory.new(:get, {'
|
26
|
+
get = ReqFactory.new(:get, {'id' => id})
|
27
27
|
Zoidberg.new(get).hash[0]['name'].should == 'homer'
|
28
28
|
end
|
29
29
|
|
@@ -50,7 +50,7 @@ describe Ki::Model do
|
|
50
50
|
|
51
51
|
it "should only accept valid object ids" do
|
52
52
|
expect {
|
53
|
-
Zoidberg.new(ReqFactory.new(:get_with_params, { :
|
53
|
+
Zoidberg.new(ReqFactory.new(:get_with_params, { :id => 'homer' }))
|
54
54
|
}.to raise_error
|
55
55
|
end
|
56
56
|
|
@@ -58,7 +58,7 @@ describe Ki::Model do
|
|
58
58
|
id = @db.create 'zoidberg', { :name => 'homer' }
|
59
59
|
@db.find('zoidberg', id)['name'].should == 'homer'
|
60
60
|
|
61
|
-
Zoidberg.new(ReqFactory.new(:put, {'
|
61
|
+
Zoidberg.new(ReqFactory.new(:put, {'id' => id, :name => 'bart'}))
|
62
62
|
@db.find('zoidberg', id)['name'].should == 'bart'
|
63
63
|
end
|
64
64
|
|
@@ -70,7 +70,7 @@ describe Ki::Model do
|
|
70
70
|
|
71
71
|
it "should raise error if id is invalid" do
|
72
72
|
expect {
|
73
|
-
Zoidberg.new(ReqFactory.new(:put, {'
|
73
|
+
Zoidberg.new(ReqFactory.new(:put, {'id' => 'f*ck', :name => 'bart'}))
|
74
74
|
}.to raise_error
|
75
75
|
end
|
76
76
|
|
@@ -80,12 +80,12 @@ describe Ki::Model do
|
|
80
80
|
}.to raise_error
|
81
81
|
|
82
82
|
expect {
|
83
|
-
Zoidberg.new(ReqFactory.new(:delete, {'
|
83
|
+
Zoidberg.new(ReqFactory.new(:delete, {'id' => 'f*ck', :name => 'bart'}))
|
84
84
|
}.to raise_error
|
85
85
|
|
86
86
|
id = @db.create 'zoidberg', { :name => 'homer' }
|
87
87
|
expect {
|
88
|
-
Zoidberg.new(ReqFactory.new(:delete, {'
|
88
|
+
Zoidberg.new(ReqFactory.new(:delete, {'id' => id}))
|
89
89
|
}.to change(@db.db['zoidberg'], :count).by(-1)
|
90
90
|
end
|
91
91
|
end
|
@@ -8,8 +8,8 @@ describe Ki::Callbacks do
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
id = Foo.new(ReqFactory.new(:post, {:foo => 'bar'})).hash['
|
12
|
-
Foo.new(ReqFactory.new(:get, { '
|
11
|
+
id = Foo.new(ReqFactory.new(:post, {:foo => 'bar'})).hash['id']
|
12
|
+
Foo.new(ReqFactory.new(:get, { 'id' => id })).hash[0]['meaning_of_life'].should == 42
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should handle before and after all" do
|
@@ -19,7 +19,7 @@ describe Ki::Callbacks do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
id = Foo.new(ReqFactory.new(:post, {:foo => 'bar'})).hash['
|
23
|
-
Foo.new(ReqFactory.new(:get, { '
|
22
|
+
id = Foo.new(ReqFactory.new(:post, {:foo => 'bar'})).hash['id']
|
23
|
+
Foo.new(ReqFactory.new(:get, { 'id' => id })).hash[0]['cool'].should be_true
|
24
24
|
end
|
25
25
|
end
|
@@ -71,7 +71,7 @@ describe Ki::QueryInterface do
|
|
71
71
|
|
72
72
|
id = Software.create({ :version => 1.1 })
|
73
73
|
expect {
|
74
|
-
Software.update({'
|
74
|
+
Software.update({'id' => id, :version => 1.2})
|
75
75
|
Software.find(id)['version'].should == 1.2
|
76
76
|
}.to change(Software, :count).by(0)
|
77
77
|
end
|
@@ -21,14 +21,23 @@ describe Ki::Restrictions do
|
|
21
21
|
Foo.new(ReqFactory.new(:post_homer))
|
22
22
|
end
|
23
23
|
|
24
|
+
it "should restrict search when find is forbidden" do
|
25
|
+
class Foo < Ki::Model
|
26
|
+
forbid :find
|
27
|
+
end
|
28
|
+
expect {
|
29
|
+
Foo.new(ReqFactory.new(:search, {'foo' => 'bar'} ))
|
30
|
+
}.to raise_exception
|
31
|
+
end
|
32
|
+
|
24
33
|
it "should only restrict :delete" do
|
25
34
|
class Foo < Ki::Model
|
26
35
|
forbid :delete
|
27
36
|
end
|
28
37
|
Foo.forbidden_actions.include?(:delete).should be_true
|
29
|
-
id = Foo.new(ReqFactory.new(:post_homer)).hash['
|
38
|
+
id = Foo.new(ReqFactory.new(:post_homer)).hash['id']
|
30
39
|
expect {
|
31
|
-
Foo.new(ReqFactory.new(:delete, { '
|
40
|
+
Foo.new(ReqFactory.new(:delete, { 'id' => id } ))
|
32
41
|
}.to raise_exception
|
33
42
|
end
|
34
43
|
|
data/spec/req_spec.rb
CHANGED
@@ -16,4 +16,14 @@ describe Ki::Req do
|
|
16
16
|
hash = Book.new(search).hash
|
17
17
|
hash[0]['title'].should == 'Foundation'
|
18
18
|
end
|
19
|
+
|
20
|
+
it "should format the id properly" do
|
21
|
+
post = ReqFactory.new(:post, { :title => 'Foundation' })
|
22
|
+
id = Book.new(post).hash['id']
|
23
|
+
|
24
|
+
get = ReqFactory.new(:get, { :title => 'Foundation' })
|
25
|
+
Book.new(get).hash[0]['id'].should == id
|
26
|
+
|
27
|
+
update = ReqFactory.new(:put, { 'id' => id, :title => 'Foundation' })
|
28
|
+
end
|
19
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ki
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -343,7 +343,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
343
343
|
version: '0'
|
344
344
|
segments:
|
345
345
|
- 0
|
346
|
-
hash: -
|
346
|
+
hash: -1443684808405340499
|
347
347
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
348
348
|
none: false
|
349
349
|
requirements:
|