orm_adapter-sequel 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 +3 -0
- data/History.txt +3 -0
- data/LICENSE +20 -0
- data/README.md +27 -0
- data/Rakefile +84 -0
- data/lib/orm_adapter-sequel/sequel.rb +146 -0
- data/lib/orm_adapter-sequel/version.rb +3 -0
- data/lib/orm_adapter-sequel.rb +3 -0
- data/orm_adapter-sequel.gemspec +31 -0
- data/spec/example_app_shared.rb +181 -0
- data/spec/sequel_spec.rb +63 -0
- data/spec/spec_helper.rb +7 -0
- metadata +224 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/History.txt
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Don Morrison (elskwid)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
ORM Adapter - Sequel Adapter
|
2
|
+
============================
|
3
|
+
|
4
|
+
Adds the Sequel ORM adapter to the ORM Adapter project
|
5
|
+
|
6
|
+
gem install orm_adapter-sequel
|
7
|
+
|
8
|
+
ORM Adapter
|
9
|
+
-----------
|
10
|
+
|
11
|
+
For more information see the excellent [orm_adapter](http://github.com/ianwhite/orm_adapter)
|
12
|
+
|
13
|
+
Development / Testing
|
14
|
+
-------------------
|
15
|
+
|
16
|
+
A Rake task is available to get the latest orm_adapter specs:
|
17
|
+
|
18
|
+
rake update_orm_specs
|
19
|
+
|
20
|
+
To run the tests:
|
21
|
+
|
22
|
+
rake spec
|
23
|
+
|
24
|
+
Copyright
|
25
|
+
---------
|
26
|
+
|
27
|
+
Copyright (c) 2011 Don Morrison (elskwid). See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'pathname'
|
4
|
+
require 'rake'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
require 'yard'
|
7
|
+
require 'git'
|
8
|
+
$:.push File.expand_path("../lib", __FILE__)
|
9
|
+
require "orm_adapter-sequel/version"
|
10
|
+
|
11
|
+
RakeFileUtils.verbose_flag = true
|
12
|
+
|
13
|
+
GEM_NAME = "orm_adapter-sequel"
|
14
|
+
|
15
|
+
task :default => :spec
|
16
|
+
|
17
|
+
directory 'tmp'
|
18
|
+
|
19
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
20
|
+
t.files = ['lib/**/*.rb', 'README.rdoc']
|
21
|
+
end
|
22
|
+
|
23
|
+
RSpec::Core::RakeTask.new(:spec)
|
24
|
+
|
25
|
+
task :spec
|
26
|
+
|
27
|
+
desc "Update the orm_adapter specs used for testing"
|
28
|
+
task :update_orm_specs => "tmp" do
|
29
|
+
spec_files = FileList["orm_adapter/example_app_shared.rb"]
|
30
|
+
orm = Pathname.new("tmp/orm_adapter")
|
31
|
+
from = orm + "spec"
|
32
|
+
to = Pathname.new("spec")
|
33
|
+
gemfile = from.parent + "Gemfile"
|
34
|
+
|
35
|
+
repo = if gemfile.exist?
|
36
|
+
Git.open(orm)
|
37
|
+
else
|
38
|
+
# checkout orm_adapter
|
39
|
+
Git.clone("https://github.com/ianwhite/orm_adapter.git", orm, :working_directory => orm)
|
40
|
+
end
|
41
|
+
# always pull to be sure
|
42
|
+
stat = repo.pull
|
43
|
+
# copy over the spec
|
44
|
+
spec_files.each do |f|
|
45
|
+
cp from.join(f), to.join(File.basename(f))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "Build the gem"
|
50
|
+
task :build do
|
51
|
+
system "gem build #{GEM_NAME}.gemspec"
|
52
|
+
end
|
53
|
+
|
54
|
+
namespace :release do
|
55
|
+
task :rubygems => :pre do
|
56
|
+
system "gem push #{GEM_NAME}-#{OrmAdapterSequel::VERSION}.gem"
|
57
|
+
end
|
58
|
+
|
59
|
+
task :github => :pre do
|
60
|
+
tag = "v#{OrmAdapterSequel::VERSION}"
|
61
|
+
git = Git.open('.')
|
62
|
+
|
63
|
+
if (git.tag(tag) rescue nil)
|
64
|
+
raise "** repo is already tagged with: #{tag}"
|
65
|
+
end
|
66
|
+
|
67
|
+
git.add_tag(tag)
|
68
|
+
git.push('origin', tag)
|
69
|
+
end
|
70
|
+
|
71
|
+
task :pre => [:spec, :build] do
|
72
|
+
git = Git.open('.')
|
73
|
+
|
74
|
+
if (git.status.changed + git.status.added + git.status.deleted).any?
|
75
|
+
raise "** repo is not clean, try committing some files"
|
76
|
+
end
|
77
|
+
|
78
|
+
if git.object('HEAD').sha != git.object('origin/master').sha
|
79
|
+
raise "** origin does not match HEAD, have you pushed?"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
task :all => ['release:github', 'release:rubygems']
|
84
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
class Sequel::Model
|
4
|
+
extend OrmAdapter::ToAdapter
|
5
|
+
plugin :active_model
|
6
|
+
|
7
|
+
# Sequel: track models that inherit from Sequel::Model
|
8
|
+
# .descendants is used in Rails 3 and DM
|
9
|
+
def self.descendants
|
10
|
+
@@descendants ||= []
|
11
|
+
end
|
12
|
+
|
13
|
+
# Sequel: hook to track descendants
|
14
|
+
def self.inherited(subclass)
|
15
|
+
super
|
16
|
+
descendants << subclass unless descendants.include?(subclass)
|
17
|
+
end
|
18
|
+
|
19
|
+
class OrmAdapter < ::OrmAdapter::Base
|
20
|
+
|
21
|
+
# Gets a list of the available models for this adapter
|
22
|
+
def self.model_classes
|
23
|
+
::Sequel::Model.descendants
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(klass)
|
27
|
+
@klass = klass
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get a list of column/property/field names
|
31
|
+
def column_names
|
32
|
+
klass.columns
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get an instance by id of the model. Raises an error if a model is not found.
|
36
|
+
# This should comply with ActiveModel#to_key API, i.e.:
|
37
|
+
#
|
38
|
+
# User.to_adapter.get!(@user.to_key) == @user
|
39
|
+
#
|
40
|
+
# Sequel: no built in finder/filter that raises an error so one is added here
|
41
|
+
def get!(id)
|
42
|
+
klass[wrap_key(id)] || raise(Error, "#{klass.name} not found with #{klass.primary_key} of #{wrap_key(id)}")
|
43
|
+
end
|
44
|
+
|
45
|
+
# Get an instance by id of the model. Returns nil if a model is not found.
|
46
|
+
# This should comply with ActiveModel#to_key API, i.e.:
|
47
|
+
#
|
48
|
+
# User.to_adapter.get(@user.to_key) == @user
|
49
|
+
#
|
50
|
+
def get(id)
|
51
|
+
klass.find(wrap_key(klass.primary_key => id))
|
52
|
+
end
|
53
|
+
|
54
|
+
# Find the first instance, optionally matching conditions, and specifying order
|
55
|
+
#
|
56
|
+
# You can call with just conditions, providing a hash
|
57
|
+
#
|
58
|
+
# User.to_adapter.find_first :name => "Fred", :age => 23
|
59
|
+
#
|
60
|
+
# Or you can specify :order, and :conditions as keys
|
61
|
+
#
|
62
|
+
# User.to_adapter.find_first :conditions => {:name => "Fred", :age => 23}
|
63
|
+
# User.to_adapter.find_first :order => [:age, :desc]
|
64
|
+
# User.to_adapter.find_first :order => :name, :conditions => {:age => 18}
|
65
|
+
#
|
66
|
+
# When specifying :order, it may be
|
67
|
+
# * a single arg e.g. <tt>:order => :name</tt>
|
68
|
+
# * a single pair with :asc, or :desc as last, e.g. <tt>:order => [:name, :desc]</tt>
|
69
|
+
# * an array of single args or pairs (with :asc or :desc as last), e.g. <tt>:order => [[:name, :asc], [:age, :desc]]</tt>
|
70
|
+
#
|
71
|
+
# Sequel: #.order doesn't like an array hence the *
|
72
|
+
def find_first(options)
|
73
|
+
conditions, order = extract_conditions_and_order!(options)
|
74
|
+
klass.filter(conditions_to_hash(conditions)).order(*order_clause(order)).first
|
75
|
+
end
|
76
|
+
|
77
|
+
# Find all models, optionally matching conditions, and specifying order
|
78
|
+
# @see OrmAdapter::Base#find_first for how to specify order and conditions
|
79
|
+
#
|
80
|
+
# Sequel: #.order doesn't like an array hence the *
|
81
|
+
def find_all(options)
|
82
|
+
conditions, order = extract_conditions_and_order!(options)
|
83
|
+
klass.filter(conditions_to_hash(conditions)).order(*order_clause(order)).all
|
84
|
+
end
|
85
|
+
|
86
|
+
# Create a model using attributes
|
87
|
+
#
|
88
|
+
# Sequel: no support for mass creation of associated objects so we fake it.
|
89
|
+
# * use a Sequel transaction
|
90
|
+
# * passed in key (col) names are checked against association names
|
91
|
+
# * an array of associated objects is created with the assoc method
|
92
|
+
# * the main object is created followed by all associated records
|
93
|
+
# * save is called to throw an error if found
|
94
|
+
#
|
95
|
+
def create!(attributes)
|
96
|
+
associated_objects = []
|
97
|
+
attrs = {}
|
98
|
+
|
99
|
+
klass.db.transaction do
|
100
|
+
attributes.each do |col, value|
|
101
|
+
if klass.associations.include?(col)
|
102
|
+
Array(value).each{|v| associated_objects << [association_method(col), v]}
|
103
|
+
else
|
104
|
+
attrs.merge!(col => value) # pass it on
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
obj = klass.create(attrs) # create main obj
|
109
|
+
associated_objects.each do |m,o|
|
110
|
+
obj.send(m, o)
|
111
|
+
end
|
112
|
+
obj.save
|
113
|
+
end # transaction
|
114
|
+
end
|
115
|
+
|
116
|
+
protected
|
117
|
+
|
118
|
+
def conditions_to_hash(conditions)
|
119
|
+
conditions.inject({}) do |cond_hash, (col, value)|
|
120
|
+
if value.is_a?(Sequel::Model)
|
121
|
+
# look up the column name for the assoc
|
122
|
+
key = klass.association_reflection(col)[:key]
|
123
|
+
cond_hash.merge(key => value.id)
|
124
|
+
else
|
125
|
+
cond_hash.merge(col => value)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def order_clause(order)
|
131
|
+
m = order.map {|pair| pair.first.send(pair.last)}
|
132
|
+
m.empty? ? nil : m
|
133
|
+
end
|
134
|
+
|
135
|
+
def association_method(col)
|
136
|
+
assoc = klass.association_reflection(col)
|
137
|
+
case assoc[:type]
|
138
|
+
when :one_to_many, :many_to_many
|
139
|
+
assoc.add_method
|
140
|
+
else # when :many_to_one, :one_to_one
|
141
|
+
assoc.setter_method
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "orm_adapter-sequel/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "orm_adapter-sequel"
|
6
|
+
s.version = OrmAdapterSequel::VERSION.dup
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Don Morrison"]
|
9
|
+
s.description = "Adds Sequel ORM adapter to the orm_adapter project"
|
10
|
+
s.summary = "Adds sequel adapter to orm_adapter which provides a single point of entry for using basic features of popular ruby ORMs."
|
11
|
+
s.email = "elskwid@gmail.com"
|
12
|
+
s.homepage = "http://github.com/elskwid/orm_adapter-sequel"
|
13
|
+
|
14
|
+
s.rubyforge_project = "orm_adapter-sequel"
|
15
|
+
s.required_rubygems_version = ">= 1.3.6"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "activemodel", ">= 3.0.0"
|
22
|
+
s.add_dependency "orm_adapter"
|
23
|
+
s.add_dependency "sequel", ">= 3.18.0"
|
24
|
+
|
25
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
26
|
+
s.add_development_dependency "git", ">= 1.2.5"
|
27
|
+
s.add_development_dependency "rake", ">= 0.8.7"
|
28
|
+
s.add_development_dependency "rspec", ">= 2.4.0"
|
29
|
+
s.add_development_dependency "sqlite3-ruby", ">= 1.3.2"
|
30
|
+
s.add_development_dependency "yard", ">= 0.6.0"
|
31
|
+
end
|
@@ -0,0 +1,181 @@
|
|
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
|
+
note = create_model(note_class, :owner => user)
|
97
|
+
note_adapter.find_first(:owner => user).should == note
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "(:order => <order array>)" do
|
102
|
+
it "should return first model in specified order" do
|
103
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
104
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
105
|
+
user_adapter.find_first(:order => [:name, [:rating, :desc]]).should == user2
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "(:conditions => <conditions hash>, :order => <order array>)" do
|
110
|
+
it "should return first model matching conditions, in specified order" do
|
111
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
112
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
113
|
+
user_adapter.find_first(:conditions => {:name => "Fred"}, :order => [:rating, :desc]).should == user2
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#find_all" do
|
119
|
+
describe "(conditions)" do
|
120
|
+
it "should return only models matching conditions" do
|
121
|
+
user1 = create_model(user_class, :name => "Fred")
|
122
|
+
user2 = create_model(user_class, :name => "Fred")
|
123
|
+
user3 = create_model(user_class, :name => "Betty")
|
124
|
+
user_adapter.find_all(:name => "Fred").should == [user1, user2]
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should return empty array if no conditions match" do
|
128
|
+
user_adapter.find_all(:name => "Fred").should == []
|
129
|
+
end
|
130
|
+
|
131
|
+
it "when conditions contain associated object, should return first model if it exists" do
|
132
|
+
user1, user2 = create_model(user_class), create_model(user_class)
|
133
|
+
note1 = create_model(note_class, :owner => user1)
|
134
|
+
note2 = create_model(note_class, :owner => user2)
|
135
|
+
note_adapter.find_all(:owner => user2).should == [note2]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "(:order => <order array>)" do
|
140
|
+
it "should return all models in specified order" do
|
141
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
142
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
143
|
+
user3 = create_model(user_class, :name => "Betty", :rating => 1)
|
144
|
+
user_adapter.find_all(:order => [:name, [:rating, :desc]]).should == [user3, user2, user1]
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe "(:conditions => <conditions hash>, :order => <order array>)" do
|
149
|
+
it "should return only models matching conditions, in specified order" do
|
150
|
+
user1 = create_model(user_class, :name => "Fred", :rating => 1)
|
151
|
+
user2 = create_model(user_class, :name => "Fred", :rating => 2)
|
152
|
+
user3 = create_model(user_class, :name => "Betty", :rating => 1)
|
153
|
+
user_adapter.find_all(:conditions => {:name => "Fred"}, :order => [:rating, :desc]).should == [user2, user1]
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe "#create!(attributes)" do
|
159
|
+
it "should create a model with the passed attributes" do
|
160
|
+
user = user_adapter.create!(:name => "Fred")
|
161
|
+
reload_model(user).name.should == "Fred"
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should raise error when create fails" do
|
165
|
+
lambda { user_adapter.create!(:user => create_model(note_class)) }.should raise_error
|
166
|
+
end
|
167
|
+
|
168
|
+
it "when attributes contain an associated object, should create a model with the attributes" do
|
169
|
+
user = create_model(user_class)
|
170
|
+
note = note_adapter.create!(:owner => user)
|
171
|
+
reload_model(note).owner.should == user
|
172
|
+
end
|
173
|
+
|
174
|
+
it "when attributes contain an has_many assoc, should create a model with the attributes" do
|
175
|
+
notes = [create_model(note_class), create_model(note_class)]
|
176
|
+
user = user_adapter.create!(:notes => notes)
|
177
|
+
reload_model(user).notes.should == notes
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
data/spec/sequel_spec.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'example_app_shared'
|
3
|
+
|
4
|
+
if !defined?(Sequel)
|
5
|
+
puts "** require 'sequel' to run the specs in #{__FILE__}"
|
6
|
+
else
|
7
|
+
|
8
|
+
DB = Sequel.sqlite # in memory db
|
9
|
+
|
10
|
+
DB.create_table! :users do
|
11
|
+
primary_key :id
|
12
|
+
String :name
|
13
|
+
Integer :rating
|
14
|
+
end
|
15
|
+
|
16
|
+
DB.create_table! :notes do
|
17
|
+
primary_key :id
|
18
|
+
String :body
|
19
|
+
Integer :owner_id
|
20
|
+
end
|
21
|
+
|
22
|
+
module SequelOrmSpec
|
23
|
+
|
24
|
+
class User < Sequel::Model
|
25
|
+
one_to_many :notes, :key => :owner_id
|
26
|
+
end
|
27
|
+
|
28
|
+
class Note < Sequel::Model
|
29
|
+
many_to_one :owner, :key => :owner_id, :class => User
|
30
|
+
end
|
31
|
+
|
32
|
+
# here be the specs!
|
33
|
+
describe Sequel::Model::OrmAdapter do
|
34
|
+
before do
|
35
|
+
User.dataset.delete
|
36
|
+
Note.dataset.delete
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "the OrmAdapter class" do
|
40
|
+
subject { Sequel::Model::OrmAdapter }
|
41
|
+
|
42
|
+
specify "#model_classes should return all model" do
|
43
|
+
subject.model_classes.should == [User, Note]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it_should_behave_like "example app with orm_adapter" do
|
48
|
+
let(:user_class) { User }
|
49
|
+
let(:note_class) { Note }
|
50
|
+
|
51
|
+
def create_model(klass, attrs = {})
|
52
|
+
klass.create(attrs)
|
53
|
+
end
|
54
|
+
|
55
|
+
def reload_model(model)
|
56
|
+
model.class[model.id]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end # if !defined?(Sequel)
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: orm_adapter-sequel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Don Morrison
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-09 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activemodel
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: orm_adapter
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: sequel
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 79
|
60
|
+
segments:
|
61
|
+
- 3
|
62
|
+
- 18
|
63
|
+
- 0
|
64
|
+
version: 3.18.0
|
65
|
+
type: :runtime
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: bundler
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 23
|
76
|
+
segments:
|
77
|
+
- 1
|
78
|
+
- 0
|
79
|
+
- 0
|
80
|
+
version: 1.0.0
|
81
|
+
type: :development
|
82
|
+
version_requirements: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: git
|
85
|
+
prerelease: false
|
86
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 21
|
92
|
+
segments:
|
93
|
+
- 1
|
94
|
+
- 2
|
95
|
+
- 5
|
96
|
+
version: 1.2.5
|
97
|
+
type: :development
|
98
|
+
version_requirements: *id005
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: rake
|
101
|
+
prerelease: false
|
102
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
hash: 49
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
- 8
|
111
|
+
- 7
|
112
|
+
version: 0.8.7
|
113
|
+
type: :development
|
114
|
+
version_requirements: *id006
|
115
|
+
- !ruby/object:Gem::Dependency
|
116
|
+
name: rspec
|
117
|
+
prerelease: false
|
118
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
hash: 31
|
124
|
+
segments:
|
125
|
+
- 2
|
126
|
+
- 4
|
127
|
+
- 0
|
128
|
+
version: 2.4.0
|
129
|
+
type: :development
|
130
|
+
version_requirements: *id007
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: sqlite3-ruby
|
133
|
+
prerelease: false
|
134
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
hash: 31
|
140
|
+
segments:
|
141
|
+
- 1
|
142
|
+
- 3
|
143
|
+
- 2
|
144
|
+
version: 1.3.2
|
145
|
+
type: :development
|
146
|
+
version_requirements: *id008
|
147
|
+
- !ruby/object:Gem::Dependency
|
148
|
+
name: yard
|
149
|
+
prerelease: false
|
150
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
151
|
+
none: false
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
hash: 7
|
156
|
+
segments:
|
157
|
+
- 0
|
158
|
+
- 6
|
159
|
+
- 0
|
160
|
+
version: 0.6.0
|
161
|
+
type: :development
|
162
|
+
version_requirements: *id009
|
163
|
+
description: Adds Sequel ORM adapter to the orm_adapter project
|
164
|
+
email: elskwid@gmail.com
|
165
|
+
executables: []
|
166
|
+
|
167
|
+
extensions: []
|
168
|
+
|
169
|
+
extra_rdoc_files: []
|
170
|
+
|
171
|
+
files:
|
172
|
+
- .gitignore
|
173
|
+
- Gemfile
|
174
|
+
- History.txt
|
175
|
+
- LICENSE
|
176
|
+
- README.md
|
177
|
+
- Rakefile
|
178
|
+
- lib/orm_adapter-sequel.rb
|
179
|
+
- lib/orm_adapter-sequel/sequel.rb
|
180
|
+
- lib/orm_adapter-sequel/version.rb
|
181
|
+
- orm_adapter-sequel.gemspec
|
182
|
+
- spec/example_app_shared.rb
|
183
|
+
- spec/sequel_spec.rb
|
184
|
+
- spec/spec_helper.rb
|
185
|
+
has_rdoc: true
|
186
|
+
homepage: http://github.com/elskwid/orm_adapter-sequel
|
187
|
+
licenses: []
|
188
|
+
|
189
|
+
post_install_message:
|
190
|
+
rdoc_options: []
|
191
|
+
|
192
|
+
require_paths:
|
193
|
+
- lib
|
194
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
195
|
+
none: false
|
196
|
+
requirements:
|
197
|
+
- - ">="
|
198
|
+
- !ruby/object:Gem::Version
|
199
|
+
hash: 3
|
200
|
+
segments:
|
201
|
+
- 0
|
202
|
+
version: "0"
|
203
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
204
|
+
none: false
|
205
|
+
requirements:
|
206
|
+
- - ">="
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
hash: 23
|
209
|
+
segments:
|
210
|
+
- 1
|
211
|
+
- 3
|
212
|
+
- 6
|
213
|
+
version: 1.3.6
|
214
|
+
requirements: []
|
215
|
+
|
216
|
+
rubyforge_project: orm_adapter-sequel
|
217
|
+
rubygems_version: 1.3.7
|
218
|
+
signing_key:
|
219
|
+
specification_version: 3
|
220
|
+
summary: Adds sequel adapter to orm_adapter which provides a single point of entry for using basic features of popular ruby ORMs.
|
221
|
+
test_files:
|
222
|
+
- spec/example_app_shared.rb
|
223
|
+
- spec/sequel_spec.rb
|
224
|
+
- spec/spec_helper.rb
|