orm_adapter_activeresource 0.0.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.
- data/.gitignore +16 -0
- data/Gemfile +7 -0
- data/Rakefile +8 -0
- data/lib/orm_adapter/adapters/activeresource.rb +78 -0
- data/lib/orm_adapter_activeresource.rb +2 -0
- data/orm_adapter_activeresource.gemspec +21 -0
- data/spec/orm_adapter/adapters/activeresource_server/config.ru +4 -0
- data/spec/orm_adapter/adapters/activeresource_server/controllers.rb +55 -0
- data/spec/orm_adapter/adapters/activeresource_server/models.rb +7 -0
- data/spec/orm_adapter/adapters/activeresource_server/server.log +829 -0
- data/spec/orm_adapter/adapters/activeresource_server/server.rb +41 -0
- data/spec/orm_adapter/adapters/activeresource_spec.rb +67 -0
- data/spec/orm_adapter/base_spec.rb +56 -0
- data/spec/orm_adapter/example_app_shared.rb +183 -0
- data/spec/orm_adapter_spec.rb +21 -0
- data/spec/spec_helper.rb +6 -0
- metadata +82 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
ENV['RAILS_ENV'] = 'test'
|
3
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../../../Gemfile', __FILE__)
|
4
|
+
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
|
5
|
+
require 'rails/all'
|
6
|
+
Bundler.require(:default, Rails.env) if defined?(Bundler)
|
7
|
+
|
8
|
+
class Rails::Application::Configuration
|
9
|
+
def database_configuration
|
10
|
+
{'test' => {'adapter' => 'sqlite3', 'database' => '/tmp/orm_adapter_test.sqlite'}}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveSupport.on_load(:before_initialize) do
|
15
|
+
ActiveRecord::Migration.suppress_messages do
|
16
|
+
ActiveRecord::Schema.define(:version => 0) do
|
17
|
+
create_table(:users, :force => true) {|t| t.string :name; t.integer :rating; }
|
18
|
+
create_table(:notes, :force => true) {|t| t.belongs_to :owner, :polymorphic => true }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ActiveResourceServer
|
24
|
+
class Application < Rails::Application
|
25
|
+
config.active_support.deprecation = :log
|
26
|
+
config.secret_token = ActiveSupport::SecureRandom.hex(30)
|
27
|
+
config.logger = Logger.new ::Rails.root.to_s + "/server.log"
|
28
|
+
|
29
|
+
initializer 'routes' do |app|
|
30
|
+
app.routes.draw do
|
31
|
+
resources :users
|
32
|
+
resources :notes
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require File.expand_path('../models', __FILE__)
|
39
|
+
require File.expand_path('../controllers', __FILE__)
|
40
|
+
|
41
|
+
ActiveResourceServer::Application.initialize!
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'orm_adapter/example_app_shared'
|
3
|
+
|
4
|
+
module ActiveResourceOrmSpec
|
5
|
+
class User < ActiveResource::Base
|
6
|
+
self.site = "http://localhost:31777/"
|
7
|
+
schema do
|
8
|
+
string 'name'
|
9
|
+
integer 'rating'
|
10
|
+
end
|
11
|
+
def notes; Note.find(:all, :params => {:owner_id => id}) || []; end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Note < ActiveResource::Base
|
15
|
+
self.site = "http://localhost:31777/"
|
16
|
+
schema do
|
17
|
+
integer 'owner_id'
|
18
|
+
end
|
19
|
+
def owner; User.find(owner_id); end
|
20
|
+
end
|
21
|
+
|
22
|
+
# here be the specs!
|
23
|
+
describe OrmAdapter::ActiveResource do
|
24
|
+
before(:all) do
|
25
|
+
@server_pid = fork do
|
26
|
+
begin
|
27
|
+
server_dir = File.expand_path(__FILE__ + '/../activeresource_server')
|
28
|
+
server_log = server_dir + '/server.log'
|
29
|
+
FileUtils.rm(server_log) rescue nil
|
30
|
+
$stdout.reopen('/dev/null', 'w')
|
31
|
+
$stderr.reopen('/dev/null', 'w')
|
32
|
+
exec('bundle exec rackup -p 31777 ' + server_dir + '/config.ru > ' + server_log + ' 2>&1')
|
33
|
+
rescue => e
|
34
|
+
puts e.inspect
|
35
|
+
end
|
36
|
+
end
|
37
|
+
sleep 5
|
38
|
+
end
|
39
|
+
after(:all) do
|
40
|
+
Process.kill "KILL", @server_pid
|
41
|
+
Process.waitpid @server_pid
|
42
|
+
Process.kill "KILL", (@server_pid + 1)
|
43
|
+
end
|
44
|
+
|
45
|
+
before do
|
46
|
+
User.find(:all).each {|u| u.destroy }
|
47
|
+
Note.find(:all).each {|n| n.destroy }
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "the OrmAdapter class" do
|
51
|
+
subject { ActiveResource::Base::OrmAdapter }
|
52
|
+
|
53
|
+
specify "#model_classes should return all model classes" do
|
54
|
+
subject.model_classes.should include(User, Note)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it_should_behave_like "example app with orm_adapter" do
|
59
|
+
let(:user_class) { User }
|
60
|
+
let(:note_class) { Note }
|
61
|
+
|
62
|
+
def create_model(klass, attrs = {})
|
63
|
+
klass.create(attrs)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OrmAdapter::Base do
|
4
|
+
subject { OrmAdapter::Base.new(Object) }
|
5
|
+
|
6
|
+
describe "#extract_conditions_and_order!" do
|
7
|
+
let(:conditions) { {:foo => 'bar'} }
|
8
|
+
let(:order) { [[:foo, :asc]] }
|
9
|
+
|
10
|
+
it "(<conditions>) should return [<conditions>, []]" do
|
11
|
+
subject.send(:extract_conditions_and_order!, conditions).should == [conditions, []]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "(:conditions => <conditions>) should return [<conditions>, []]" do
|
15
|
+
subject.send(:extract_conditions_and_order!, :conditions => conditions).should == [conditions, []]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "(:order => <order>) should return [{}, <order>]" do
|
19
|
+
subject.send(:extract_conditions_and_order!, :order => order).should == [{}, order]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "(:conditions => <conditions>, :order => <order>) should return [<conditions>, <order>]" do
|
23
|
+
subject.send(:extract_conditions_and_order!, :conditions => conditions, :order => order).should == [conditions, order]
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#normalize_order" do
|
27
|
+
specify "(nil) returns []" do
|
28
|
+
subject.send(:normalize_order, nil).should == []
|
29
|
+
end
|
30
|
+
|
31
|
+
specify ":foo returns [[:foo, :asc]]" do
|
32
|
+
subject.send(:normalize_order, :foo).should == [[:foo, :asc]]
|
33
|
+
end
|
34
|
+
|
35
|
+
specify "[:foo] returns [[:foo, :asc]]" do
|
36
|
+
subject.send(:normalize_order, [:foo]).should == [[:foo, :asc]]
|
37
|
+
end
|
38
|
+
|
39
|
+
specify "[:foo, :desc] returns [[:foo, :desc]]" do
|
40
|
+
subject.send(:normalize_order, [:foo, :desc]).should == [[:foo, :desc]]
|
41
|
+
end
|
42
|
+
|
43
|
+
specify "[:foo, [:bar, :asc], [:baz, :desc], :bing] returns [[:foo, :asc], [:bar, :asc], [:baz, :desc], [:bing, :asc]]" do
|
44
|
+
subject.send(:normalize_order, [:foo, [:bar, :asc], [:baz, :desc], :bing]).should == [[:foo, :asc], [:bar, :asc], [:baz, :desc], [:bing, :asc]]
|
45
|
+
end
|
46
|
+
|
47
|
+
specify "[[:foo, :wtf]] raises ArgumentError" do
|
48
|
+
lambda { subject.send(:normalize_order, [[:foo, :wtf]]) }.should raise_error(ArgumentError)
|
49
|
+
end
|
50
|
+
|
51
|
+
specify "[[:foo, :asc, :desc]] raises ArgumentError" do
|
52
|
+
lambda { subject.send(:normalize_order, [[:foo, :asc, :desc]]) }.should raise_error(ArgumentError)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
# to test your new orm_adapter, make an example app that matches the functionality
|
2
|
+
# found in the existing specs for example, look at spec/orm_adapter/adapters/active_record_spec.rb
|
3
|
+
#
|
4
|
+
# Then you can execute this shared spec as follows:
|
5
|
+
#
|
6
|
+
# it_should_behave_like "execute app with orm_adapter" do
|
7
|
+
# let(:user_class) { User }
|
8
|
+
# let(:note_class) { Note }
|
9
|
+
#
|
10
|
+
# # optionaly define the following functions if the ORM does not support
|
11
|
+
# # this syntax - this should NOT use the orm_adapter, because we're testing that
|
12
|
+
# def create_model(klass, attrs = {})
|
13
|
+
# klass.create!(attrs)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# def reload_model(model)
|
17
|
+
# model.class.find(model.id)
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
shared_examples_for "example app with orm_adapter" do
|
22
|
+
|
23
|
+
def create_model(klass, attrs = {})
|
24
|
+
klass.create!(attrs)
|
25
|
+
end
|
26
|
+
|
27
|
+
def reload_model(model)
|
28
|
+
model.class.find(model.id)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "an ORM class" do
|
32
|
+
subject { note_class }
|
33
|
+
|
34
|
+
it "#to_adapter should return an adapter instance" do
|
35
|
+
subject.to_adapter.should be_a(OrmAdapter::Base)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "#to_adapter should return an adapter for the receiver" do
|
39
|
+
subject.to_adapter.klass.should == subject
|
40
|
+
end
|
41
|
+
|
42
|
+
it "#to_adapter should be cached" do
|
43
|
+
subject.to_adapter.object_id.should == subject.to_adapter.object_id
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "adapter instance" do
|
48
|
+
let(:note_adapter) { note_class.to_adapter }
|
49
|
+
let(:user_adapter) { user_class.to_adapter }
|
50
|
+
|
51
|
+
describe "#get!(id)" do
|
52
|
+
it "should return the instance with id if it exists" do
|
53
|
+
user = create_model(user_class)
|
54
|
+
user_adapter.get!(user.id).should == user
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should allow to_key like arguments" do
|
58
|
+
user = create_model(user_class)
|
59
|
+
user_adapter.get!(user.to_key).should == user
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should raise an error if there is no instance with that id" do
|
63
|
+
lambda { user_adapter.get!("non-exitent id") }.should raise_error
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "#get(id)" do
|
68
|
+
it "should return the instance with id if it exists" do
|
69
|
+
user = create_model(user_class)
|
70
|
+
user_adapter.get(user.id).should == user
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should allow to_key like arguments" do
|
74
|
+
user = create_model(user_class)
|
75
|
+
user_adapter.get(user.to_key).should == user
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return nil if there is no instance with that id" do
|
79
|
+
user_adapter.get("non-exitent id").should be_nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#find_first" do
|
84
|
+
describe "(conditions)" do
|
85
|
+
it "should return first model matching conditions, if it exists" do
|
86
|
+
user = create_model(user_class, :name => "Fred")
|
87
|
+
user_adapter.find_first(:name => "Fred").should == user
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return nil if no conditions match" do
|
91
|
+
user_adapter.find_first(:name => "Betty").should == nil
|
92
|
+
end
|
93
|
+
|
94
|
+
it "when conditions contain associated object, should return first model if it exists" do
|
95
|
+
user = create_model(user_class)
|
96
|
+
user2 = create_model(user_class)
|
97
|
+
note = create_model(note_class, :owner => user2)
|
98
|
+
note2 = create_model(note_class, :owner => user)
|
99
|
+
note_adapter.find_first(:owner => user).should == note2
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "(:order => <order array>)" do
|
104
|
+
it "should return first model in specified order" do
|
105
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
106
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
107
|
+
user_adapter.find_first(:order => [:name, [:rating, :desc]]).should == user2
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "(:conditions => <conditions hash>, :order => <order array>)" do
|
112
|
+
it "should return first model matching conditions, in specified order" do
|
113
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
114
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
115
|
+
user_adapter.find_first(:conditions => {:name => "Fred"}, :order => [:rating, :desc]).should == user2
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "#find_all" do
|
121
|
+
describe "(conditions)" do
|
122
|
+
it "should return only models matching conditions" do
|
123
|
+
user1 = create_model(user_class, :name => "Fred")
|
124
|
+
user2 = create_model(user_class, :name => "Fred")
|
125
|
+
user3 = create_model(user_class, :name => "Betty")
|
126
|
+
user_adapter.find_all(:name => "Fred").should == [user1, user2]
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should return empty array if no conditions match" do
|
130
|
+
user_adapter.find_all(:name => "Fred").should == []
|
131
|
+
end
|
132
|
+
|
133
|
+
it "when conditions contain associated object, should return first model if it exists" do
|
134
|
+
user1, user2 = create_model(user_class), create_model(user_class)
|
135
|
+
note1 = create_model(note_class, :owner => user1)
|
136
|
+
note2 = create_model(note_class, :owner => user2)
|
137
|
+
note_adapter.find_all(:owner => user2).should == [note2]
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe "(:order => <order array>)" do
|
142
|
+
it "should return all models in specified order" do
|
143
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
144
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
145
|
+
user3 = create_model(user_class, :name => "Betty", :rating => 1)
|
146
|
+
user_adapter.find_all(:order => [:name, [:rating, :desc]]).should == [user3, user2, user1]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "(:conditions => <conditions hash>, :order => <order array>)" do
|
151
|
+
it "should return only models matching conditions, in specified order" do
|
152
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
153
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
154
|
+
user3 = create_model(user_class, :name => "Betty", :rating => 1)
|
155
|
+
user_adapter.find_all(:conditions => {:name => "Fred"}, :order => [:rating, :desc]).should == [user2, user1]
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe "#create!(attributes)" do
|
161
|
+
it "should create a model with the passed attributes" do
|
162
|
+
user = user_adapter.create!(:name => "Fred")
|
163
|
+
reload_model(user).name.should == "Fred"
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should raise error when create fails" do
|
167
|
+
lambda { user_adapter.create!(:user => create_model(note_class)) }.should raise_error
|
168
|
+
end
|
169
|
+
|
170
|
+
it "when attributes contain an associated object, should create a model with the attributes" do
|
171
|
+
user = create_model(user_class)
|
172
|
+
note = note_adapter.create!(:owner => user)
|
173
|
+
reload_model(note).owner.should == user
|
174
|
+
end
|
175
|
+
|
176
|
+
it "when attributes contain an has_many assoc, should create a model with the attributes" do
|
177
|
+
notes = [create_model(note_class), create_model(note_class)]
|
178
|
+
user = user_adapter.create!(:notes => notes)
|
179
|
+
reload_model(user).notes.should == notes
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OrmAdapter do
|
4
|
+
subject { OrmAdapter }
|
5
|
+
|
6
|
+
describe "when a new adapter is created (by inheriting form OrmAdapter::Base)" do
|
7
|
+
let!(:adapter) { Class.new(OrmAdapter::Base) }
|
8
|
+
|
9
|
+
its(:adapters) { should include(adapter) }
|
10
|
+
|
11
|
+
describe "and the adapter has a model class" do
|
12
|
+
let(:model) { Class.new }
|
13
|
+
|
14
|
+
before { adapter.stub!(:model_classes).and_return([model]) }
|
15
|
+
|
16
|
+
its(:model_classes) { should include(model) }
|
17
|
+
end
|
18
|
+
|
19
|
+
after { OrmAdapter.adapters.delete(adapter) }
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: orm_adapter_activeresource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lukas Fittl
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-07 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: orm_adapter
|
16
|
+
requirement: &2153624600 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.7
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2153624600
|
25
|
+
description: Extends orm_adapter with support for ActiveResource
|
26
|
+
email: lukas@fittl.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Rakefile
|
34
|
+
- lib/orm_adapter/adapters/activeresource.rb
|
35
|
+
- lib/orm_adapter_activeresource.rb
|
36
|
+
- orm_adapter_activeresource.gemspec
|
37
|
+
- spec/orm_adapter/adapters/activeresource_server/config.ru
|
38
|
+
- spec/orm_adapter/adapters/activeresource_server/controllers.rb
|
39
|
+
- spec/orm_adapter/adapters/activeresource_server/models.rb
|
40
|
+
- spec/orm_adapter/adapters/activeresource_server/server.log
|
41
|
+
- spec/orm_adapter/adapters/activeresource_server/server.rb
|
42
|
+
- spec/orm_adapter/adapters/activeresource_spec.rb
|
43
|
+
- spec/orm_adapter/base_spec.rb
|
44
|
+
- spec/orm_adapter/example_app_shared.rb
|
45
|
+
- spec/orm_adapter_spec.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
homepage: http://github.com/lfittl/orm_adapter_activeresource
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 1.3.6
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project: orm_adapter_activeresource
|
67
|
+
rubygems_version: 1.8.15
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Extends the orm_adapter ORM abstraction layer with support for Rails' ActiveResource
|
71
|
+
REST-based ORM.
|
72
|
+
test_files:
|
73
|
+
- spec/orm_adapter/adapters/activeresource_server/config.ru
|
74
|
+
- spec/orm_adapter/adapters/activeresource_server/controllers.rb
|
75
|
+
- spec/orm_adapter/adapters/activeresource_server/models.rb
|
76
|
+
- spec/orm_adapter/adapters/activeresource_server/server.log
|
77
|
+
- spec/orm_adapter/adapters/activeresource_server/server.rb
|
78
|
+
- spec/orm_adapter/adapters/activeresource_spec.rb
|
79
|
+
- spec/orm_adapter/base_spec.rb
|
80
|
+
- spec/orm_adapter/example_app_shared.rb
|
81
|
+
- spec/orm_adapter_spec.rb
|
82
|
+
- spec/spec_helper.rb
|