orm_adapter 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/.travis.yml +4 -0
- data/Gemfile.lock.development +2 -4
- data/History.txt +5 -0
- data/README.rdoc +2 -0
- data/Rakefile +23 -42
- data/lib/orm_adapter/adapters/active_record.rb +10 -14
- data/lib/orm_adapter/adapters/data_mapper.rb +5 -0
- data/lib/orm_adapter/adapters/mongo_mapper.rb +5 -0
- data/lib/orm_adapter/adapters/mongoid.rb +5 -0
- data/lib/orm_adapter/base.rb +10 -1
- data/lib/orm_adapter/version.rb +1 -1
- data/orm_adapter.gemspec +2 -2
- data/spec/orm_adapter/adapters/active_record_spec.rb +1 -1
- data/spec/orm_adapter/base_spec.rb +7 -0
- data/spec/orm_adapter/example_app_shared.rb +21 -3
- metadata +34 -29
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.travis.yml
ADDED
data/Gemfile.lock.development
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
orm_adapter (0.0
|
4
|
+
orm_adapter (0.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
@@ -104,8 +104,6 @@ GEM
|
|
104
104
|
diff-lcs (~> 1.1.2)
|
105
105
|
rspec-mocks (2.8.0)
|
106
106
|
sqlite3 (1.3.5)
|
107
|
-
sqlite3-ruby (1.3.3)
|
108
|
-
sqlite3 (>= 1.3.3)
|
109
107
|
stringex (1.3.0)
|
110
108
|
tzinfo (0.3.31)
|
111
109
|
uuidtools (2.1.2)
|
@@ -127,5 +125,5 @@ DEPENDENCIES
|
|
127
125
|
orm_adapter!
|
128
126
|
rake (>= 0.8.7)
|
129
127
|
rspec (>= 2.4.0)
|
130
|
-
sqlite3
|
128
|
+
sqlite3 (>= 1.3.2)
|
131
129
|
yard (>= 0.6.0)
|
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -54,6 +54,8 @@ orm_adapter is an extraction from {pickle}[http://github.com/ianwhite/pickle] by
|
|
54
54
|
|
55
55
|
{Luke Cunningham}[http://github.com/icaruswings] contributes the Mongo Mapper adapter.
|
56
56
|
|
57
|
+
{Fred Wu}[http://github.com/fredwu] contributes the #destroy methods.
|
58
|
+
|
57
59
|
|
58
60
|
== Development
|
59
61
|
|
data/Rakefile
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
2
8
|
require 'rake'
|
3
9
|
require 'rspec/core/rake_task'
|
4
|
-
require 'yard'
|
5
|
-
require 'git'
|
6
10
|
$:.push File.expand_path("../lib", __FILE__)
|
7
11
|
require "orm_adapter/version"
|
8
12
|
|
@@ -10,47 +14,24 @@ task :default => :spec
|
|
10
14
|
|
11
15
|
RSpec::Core::RakeTask.new(:spec)
|
12
16
|
|
13
|
-
|
14
|
-
|
17
|
+
begin
|
18
|
+
require 'yard'
|
19
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
20
|
+
t.files = ['lib/**/*.rb', 'README.rdoc']
|
21
|
+
end
|
22
|
+
rescue LoadError
|
23
|
+
task :doc do
|
24
|
+
puts "install yard to generate the docs"
|
25
|
+
end
|
15
26
|
end
|
16
27
|
|
17
|
-
|
18
|
-
system "gem build orm_adapter.gemspec"
|
19
|
-
end
|
28
|
+
Bundler::GemHelper.install_tasks
|
20
29
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
tag = "v#{OrmAdapter::VERSION}"
|
28
|
-
git = Git.open('.')
|
29
|
-
|
30
|
-
if (git.tag(tag) rescue nil)
|
31
|
-
raise "** repo is already tagged with: #{tag}"
|
32
|
-
end
|
33
|
-
|
34
|
-
git.add_tag(tag)
|
35
|
-
git.push('origin', tag)
|
36
|
-
end
|
37
|
-
|
38
|
-
task :pre => [:spec, :build] do
|
39
|
-
git = Git.open('.')
|
40
|
-
|
41
|
-
if File.exists?("Gemfile.lock") && File.read("Gemfile.lock") != File.read("Gemfile.lock.development")
|
42
|
-
cp "Gemfile.lock", "Gemfile.lock.development"
|
43
|
-
raise "** Gemfile.lock.development has been updated, please commit these changes."
|
44
|
-
end
|
45
|
-
|
46
|
-
if git.status.changed.any? || git.status.added.any? || git.status.deleted.any?
|
47
|
-
raise "** repo is not clean, try committing some files"
|
48
|
-
end
|
49
|
-
|
50
|
-
if git.object('HEAD').sha != git.object('origin/master').sha
|
51
|
-
raise "** origin does not match HEAD, have you pushed?"
|
52
|
-
end
|
30
|
+
task :release => :check_gemfile
|
31
|
+
|
32
|
+
task :check_gemfile do
|
33
|
+
if File.exists?("Gemfile.lock") && File.read("Gemfile.lock") != File.read("Gemfile.lock.development")
|
34
|
+
cp "Gemfile.lock", "Gemfile.lock.development"
|
35
|
+
raise "** Gemfile.lock.development has been updated, please commit these changes."
|
53
36
|
end
|
54
|
-
|
55
|
-
task :all => ['release:github', 'release:rubygems']
|
56
37
|
end
|
@@ -1,26 +1,17 @@
|
|
1
|
-
|
2
|
-
require 'activerecord'
|
3
|
-
rescue LoadError
|
4
|
-
require 'active_record'
|
5
|
-
end
|
1
|
+
require 'active_record'
|
6
2
|
|
7
3
|
module OrmAdapter
|
8
4
|
class ActiveRecord < Base
|
9
5
|
# Do not consider these to be part of the class list
|
10
6
|
def self.except_classes
|
11
7
|
@@except_classes ||= [
|
12
|
-
"CGI::Session::ActiveRecordStore::Session",
|
13
8
|
"ActiveRecord::SessionStore::Session"
|
14
9
|
]
|
15
10
|
end
|
16
11
|
|
17
12
|
# Gets a list of the available models for this adapter
|
18
13
|
def self.model_classes
|
19
|
-
|
20
|
-
klasses = ::ActiveRecord::Base.__send__(:descendants) # Rails 3
|
21
|
-
rescue
|
22
|
-
klasses = ::ActiveRecord::Base.__send__(:subclasses) # Rails 2
|
23
|
-
end
|
14
|
+
klasses = ::ActiveRecord::Base.__send__(:descendants)
|
24
15
|
|
25
16
|
klasses.select do |klass|
|
26
17
|
!klass.abstract_class? && !except_classes.include?(klass.name)
|
@@ -39,19 +30,19 @@ module OrmAdapter
|
|
39
30
|
|
40
31
|
# @see OrmAdapter::Base#get
|
41
32
|
def get(id)
|
42
|
-
klass.
|
33
|
+
klass.where(klass.primary_key => wrap_key(id)).first
|
43
34
|
end
|
44
35
|
|
45
36
|
# @see OrmAdapter::Base#find_first
|
46
37
|
def find_first(options)
|
47
38
|
conditions, order = extract_conditions_and_order!(options)
|
48
|
-
klass.
|
39
|
+
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).first
|
49
40
|
end
|
50
41
|
|
51
42
|
# @see OrmAdapter::Base#find_all
|
52
43
|
def find_all(options)
|
53
44
|
conditions, order = extract_conditions_and_order!(options)
|
54
|
-
klass.
|
45
|
+
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).all
|
55
46
|
end
|
56
47
|
|
57
48
|
# @see OrmAdapter::Base#create!
|
@@ -59,6 +50,11 @@ module OrmAdapter
|
|
59
50
|
klass.create!(attributes)
|
60
51
|
end
|
61
52
|
|
53
|
+
# @see OrmAdapter::Base#destroy
|
54
|
+
def destroy(object)
|
55
|
+
object.destroy && true if valid_object?(object)
|
56
|
+
end
|
57
|
+
|
62
58
|
protected
|
63
59
|
|
64
60
|
# Introspects the klass to convert and objects in conditions into foreign key and type fields
|
data/lib/orm_adapter/base.rb
CHANGED
@@ -42,7 +42,7 @@ module OrmAdapter
|
|
42
42
|
#
|
43
43
|
# User.to_adapter.get(@user.to_key) == @user
|
44
44
|
#
|
45
|
-
def get
|
45
|
+
def get(id)
|
46
46
|
raise NotSupportedError
|
47
47
|
end
|
48
48
|
|
@@ -78,8 +78,17 @@ module OrmAdapter
|
|
78
78
|
raise NotSupportedError
|
79
79
|
end
|
80
80
|
|
81
|
+
# Destroy an instance by passing in the instance itself.
|
82
|
+
def destroy(object)
|
83
|
+
raise NotSupportedError
|
84
|
+
end
|
85
|
+
|
81
86
|
protected
|
82
87
|
|
88
|
+
def valid_object?(object)
|
89
|
+
object.class == klass
|
90
|
+
end
|
91
|
+
|
83
92
|
def wrap_key(key)
|
84
93
|
key.is_a?(Array) ? key.first : key
|
85
94
|
end
|
data/lib/orm_adapter/version.rb
CHANGED
data/orm_adapter.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.require_paths = ["lib"]
|
20
|
-
|
20
|
+
|
21
21
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
22
22
|
s.add_development_dependency "git", ">= 1.2.5"
|
23
23
|
s.add_development_dependency "yard", ">= 0.6.0"
|
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.add_development_dependency "mongo_mapper", ">= 0.9.0"
|
28
28
|
s.add_development_dependency "bson_ext", ">= 1.3.0"
|
29
29
|
s.add_development_dependency "rspec", ">= 2.4.0"
|
30
|
-
s.add_development_dependency "sqlite3
|
30
|
+
s.add_development_dependency "sqlite3", ">= 1.3.2"
|
31
31
|
s.add_development_dependency "datamapper", ">= 1.0"
|
32
32
|
s.add_development_dependency "dm-sqlite-adapter", ">= 1.0"
|
33
33
|
s.add_development_dependency "dm-active_model", ">= 1.0"
|
@@ -37,7 +37,7 @@ else
|
|
37
37
|
subject { ActiveRecord::Base::OrmAdapter }
|
38
38
|
|
39
39
|
specify "#except_classes should return the names of active record session store classes" do
|
40
|
-
subject.except_classes.should == ["
|
40
|
+
subject.except_classes.should == ["ActiveRecord::SessionStore::Session"]
|
41
41
|
end
|
42
42
|
|
43
43
|
specify "#model_classes should return all of the non abstract model classes (that are not in except_classes)" do
|
@@ -23,6 +23,13 @@ describe OrmAdapter::Base do
|
|
23
23
|
subject.send(:extract_conditions_and_order!, :conditions => conditions, :order => order).should == [conditions, order]
|
24
24
|
end
|
25
25
|
|
26
|
+
describe "#valid_object?" do
|
27
|
+
it "determines whether an object is valid for the current model class" do
|
28
|
+
subject.send(:valid_object?, Object.new).should be_true
|
29
|
+
subject.send(:valid_object?, String.new).should be_false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
26
33
|
describe "#normalize_order" do
|
27
34
|
specify "(nil) returns []" do
|
28
35
|
subject.send(:normalize_order, nil).should == []
|
@@ -60,7 +60,7 @@ shared_examples_for "example app with orm_adapter" do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should raise an error if there is no instance with that id" do
|
63
|
-
lambda { user_adapter.get!("
|
63
|
+
lambda { user_adapter.get!("nonexistent id") }.should raise_error
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -76,10 +76,10 @@ shared_examples_for "example app with orm_adapter" do
|
|
76
76
|
end
|
77
77
|
|
78
78
|
it "should return nil if there is no instance with that id" do
|
79
|
-
user_adapter.get("
|
79
|
+
user_adapter.get("nonexistent id").should be_nil
|
80
80
|
end
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
describe "#find_first" do
|
84
84
|
describe "(conditions)" do
|
85
85
|
it "should return first model matching conditions, if it exists" do
|
@@ -177,5 +177,23 @@ shared_examples_for "example app with orm_adapter" do
|
|
177
177
|
reload_model(user).notes.should == notes
|
178
178
|
end
|
179
179
|
end
|
180
|
+
|
181
|
+
describe "#destroy(instance)" do
|
182
|
+
it "should destroy the instance if it exists" do
|
183
|
+
user = create_model(user_class)
|
184
|
+
user_adapter.destroy(user).should == true
|
185
|
+
user_adapter.get(user.id).should be_nil
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should return nil if passed with an invalid instance" do
|
189
|
+
user_adapter.destroy("nonexistent instance").should be_nil
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should not destroy the instance if it doesn't match the model class" do
|
193
|
+
user = create_model(user_class)
|
194
|
+
note_adapter.destroy(user).should be_nil
|
195
|
+
user_adapter.get(user.id).should == user
|
196
|
+
end
|
197
|
+
end
|
180
198
|
end
|
181
199
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: orm_adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-06-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
17
|
-
requirement: &
|
17
|
+
requirement: &70097699980460 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 1.0.0
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70097699980460
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: git
|
28
|
-
requirement: &
|
28
|
+
requirement: &70097699979600 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 1.2.5
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70097699979600
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: yard
|
39
|
-
requirement: &
|
39
|
+
requirement: &70097699979000 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 0.6.0
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *70097699979000
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rake
|
50
|
-
requirement: &
|
50
|
+
requirement: &70097699978040 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: 0.8.7
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *70097699978040
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: activerecord
|
61
|
-
requirement: &
|
61
|
+
requirement: &70097699977120 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: 3.0.0
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *70097699977120
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: mongoid
|
72
|
-
requirement: &
|
72
|
+
requirement: &70097699976120 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -77,10 +77,10 @@ dependencies:
|
|
77
77
|
version: 2.0.0.beta.20
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *70097699976120
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
82
|
name: mongo_mapper
|
83
|
-
requirement: &
|
83
|
+
requirement: &70097699975540 !ruby/object:Gem::Requirement
|
84
84
|
none: false
|
85
85
|
requirements:
|
86
86
|
- - ! '>='
|
@@ -88,10 +88,10 @@ dependencies:
|
|
88
88
|
version: 0.9.0
|
89
89
|
type: :development
|
90
90
|
prerelease: false
|
91
|
-
version_requirements: *
|
91
|
+
version_requirements: *70097699975540
|
92
92
|
- !ruby/object:Gem::Dependency
|
93
93
|
name: bson_ext
|
94
|
-
requirement: &
|
94
|
+
requirement: &70097699960720 !ruby/object:Gem::Requirement
|
95
95
|
none: false
|
96
96
|
requirements:
|
97
97
|
- - ! '>='
|
@@ -99,10 +99,10 @@ dependencies:
|
|
99
99
|
version: 1.3.0
|
100
100
|
type: :development
|
101
101
|
prerelease: false
|
102
|
-
version_requirements: *
|
102
|
+
version_requirements: *70097699960720
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: rspec
|
105
|
-
requirement: &
|
105
|
+
requirement: &70097699957980 !ruby/object:Gem::Requirement
|
106
106
|
none: false
|
107
107
|
requirements:
|
108
108
|
- - ! '>='
|
@@ -110,10 +110,10 @@ dependencies:
|
|
110
110
|
version: 2.4.0
|
111
111
|
type: :development
|
112
112
|
prerelease: false
|
113
|
-
version_requirements: *
|
113
|
+
version_requirements: *70097699957980
|
114
114
|
- !ruby/object:Gem::Dependency
|
115
|
-
name: sqlite3
|
116
|
-
requirement: &
|
115
|
+
name: sqlite3
|
116
|
+
requirement: &70097699956440 !ruby/object:Gem::Requirement
|
117
117
|
none: false
|
118
118
|
requirements:
|
119
119
|
- - ! '>='
|
@@ -121,10 +121,10 @@ dependencies:
|
|
121
121
|
version: 1.3.2
|
122
122
|
type: :development
|
123
123
|
prerelease: false
|
124
|
-
version_requirements: *
|
124
|
+
version_requirements: *70097699956440
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: datamapper
|
127
|
-
requirement: &
|
127
|
+
requirement: &70097699955740 !ruby/object:Gem::Requirement
|
128
128
|
none: false
|
129
129
|
requirements:
|
130
130
|
- - ! '>='
|
@@ -132,10 +132,10 @@ dependencies:
|
|
132
132
|
version: '1.0'
|
133
133
|
type: :development
|
134
134
|
prerelease: false
|
135
|
-
version_requirements: *
|
135
|
+
version_requirements: *70097699955740
|
136
136
|
- !ruby/object:Gem::Dependency
|
137
137
|
name: dm-sqlite-adapter
|
138
|
-
requirement: &
|
138
|
+
requirement: &70097699954920 !ruby/object:Gem::Requirement
|
139
139
|
none: false
|
140
140
|
requirements:
|
141
141
|
- - ! '>='
|
@@ -143,10 +143,10 @@ dependencies:
|
|
143
143
|
version: '1.0'
|
144
144
|
type: :development
|
145
145
|
prerelease: false
|
146
|
-
version_requirements: *
|
146
|
+
version_requirements: *70097699954920
|
147
147
|
- !ruby/object:Gem::Dependency
|
148
148
|
name: dm-active_model
|
149
|
-
requirement: &
|
149
|
+
requirement: &70097699953980 !ruby/object:Gem::Requirement
|
150
150
|
none: false
|
151
151
|
requirements:
|
152
152
|
- - ! '>='
|
@@ -154,7 +154,7 @@ dependencies:
|
|
154
154
|
version: '1.0'
|
155
155
|
type: :development
|
156
156
|
prerelease: false
|
157
|
-
version_requirements: *
|
157
|
+
version_requirements: *70097699953980
|
158
158
|
description: Provides a single point of entry for using basic features of ruby ORMs
|
159
159
|
email: ian.w.white@gmail.com
|
160
160
|
executables: []
|
@@ -162,6 +162,8 @@ extensions: []
|
|
162
162
|
extra_rdoc_files: []
|
163
163
|
files:
|
164
164
|
- .gitignore
|
165
|
+
- .rspec
|
166
|
+
- .travis.yml
|
165
167
|
- Gemfile
|
166
168
|
- Gemfile.lock.development
|
167
169
|
- History.txt
|
@@ -197,6 +199,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
197
199
|
- - ! '>='
|
198
200
|
- !ruby/object:Gem::Version
|
199
201
|
version: '0'
|
202
|
+
segments:
|
203
|
+
- 0
|
204
|
+
hash: 1483369582695834263
|
200
205
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
201
206
|
none: false
|
202
207
|
requirements:
|