omnidata 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +20 -0
- data/README.md +34 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/omnidata.rb +8 -0
- data/lib/omnidata/adapters/abstract_adapter.rb +23 -0
- data/lib/omnidata/adapters/adapter_manager.rb +43 -0
- data/lib/omnidata/adapters/mongodb_adapter.rb +51 -0
- data/lib/omnidata/model.rb +14 -0
- data/lib/omnidata/orm.rb +29 -0
- data/lib/omnidata/persistence.rb +74 -0
- data/omnidata.gemspec +76 -0
- data/spec/adapters/adapter_manager_spec.rb +29 -0
- data/spec/adapters/mongodb_adapter_spec.rb +34 -0
- data/spec/fixtures/example_models.rb +15 -0
- data/spec/shared_orm.rb +18 -0
- data/spec/shared_persistence.rb +57 -0
- data/spec/spec_helper.rb +15 -0
- metadata +168 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
active_support (3.0.0)
|
5
|
+
activesupport (= 3.0.0)
|
6
|
+
activesupport (3.0.0)
|
7
|
+
diff-lcs (1.1.3)
|
8
|
+
git (1.2.5)
|
9
|
+
jeweler (1.8.3)
|
10
|
+
bundler (~> 1.0)
|
11
|
+
git (>= 1.2.5)
|
12
|
+
rake
|
13
|
+
rdoc
|
14
|
+
json (1.7.0)
|
15
|
+
rake (0.9.2.2)
|
16
|
+
rdoc (3.12)
|
17
|
+
json (~> 1.4)
|
18
|
+
rspec (2.8.0)
|
19
|
+
rspec-core (~> 2.8.0)
|
20
|
+
rspec-expectations (~> 2.8.0)
|
21
|
+
rspec-mocks (~> 2.8.0)
|
22
|
+
rspec-core (2.8.0)
|
23
|
+
rspec-expectations (2.8.0)
|
24
|
+
diff-lcs (~> 1.1.2)
|
25
|
+
rspec-mocks (2.8.0)
|
26
|
+
virtus (0.4.0)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
active_support
|
33
|
+
bundler
|
34
|
+
jeweler (~> 1.8.3)
|
35
|
+
rdoc (~> 3.12)
|
36
|
+
rspec (~> 2.8.0)
|
37
|
+
virtus
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Dorren Chen
|
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,34 @@
|
|
1
|
+
# omnidata
|
2
|
+
|
3
|
+
warning:
|
4
|
+
**Currently in super alpha stage, everything's subjected to change.**
|
5
|
+
|
6
|
+
Omnidata allows you to define your models in persistence agnostic fashion, so
|
7
|
+
model can be saved in any database you like, mongodb, couchdb, redis, etc.
|
8
|
+
|
9
|
+
Gem uses [Virtus](https://github.com/solnic/virtus#readme) for defining attributes.
|
10
|
+
|
11
|
+
For example:
|
12
|
+
|
13
|
+
Omnidata.setup_database(:db1, {:adapter => 'mongodb', :database => 'mydb'})
|
14
|
+
Omnidata.setup_database(:db2, {:adapter => 'mongodb', :database => 'mydb2'})
|
15
|
+
|
16
|
+
class User
|
17
|
+
include Omnidata::Model
|
18
|
+
use_database :db1
|
19
|
+
|
20
|
+
attribute :name, String
|
21
|
+
attribute :age, Integer
|
22
|
+
end
|
23
|
+
|
24
|
+
user = User.new(:name => 'Jack Hunt', :age => 27)
|
25
|
+
user.save
|
26
|
+
User.find(user.id)
|
27
|
+
|
28
|
+
# to switch to another db temporarily.
|
29
|
+
User.with_database(:db2) do
|
30
|
+
User.find(user.id)
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "omnidata"
|
18
|
+
gem.homepage = "http://github.com/dorren/omnidata"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Omnidata is a persistence agnostic ORM gem}
|
21
|
+
gem.description = %Q{Omnidata let you define your model free from any specific database.}
|
22
|
+
gem.email = "dorrenchen@gmail.com"
|
23
|
+
gem.authors = ["Dorren Chen"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "omnidata #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/omnidata.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'omnidata/adapters/adapter_manager'
|
2
|
+
|
3
|
+
module Omnidata
|
4
|
+
# shortcut method
|
5
|
+
def self.setup_database(name, options)
|
6
|
+
Adapters::AdapterManager.instance.add(name, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.adapter(name)
|
10
|
+
Adapters::AdapterManager.instance.adapter(name)
|
11
|
+
end
|
12
|
+
|
13
|
+
module Adapters
|
14
|
+
class AbstractAdapter
|
15
|
+
attr_accessor :name
|
16
|
+
attr_reader :config
|
17
|
+
|
18
|
+
def initialize(options)
|
19
|
+
@config = options.dup
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Omnidata
|
4
|
+
module Adapters
|
5
|
+
class AdapterError < StandardError
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
class AdapterManager
|
10
|
+
include Singleton
|
11
|
+
|
12
|
+
attr_reader :adapters
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
reset
|
16
|
+
end
|
17
|
+
|
18
|
+
def reset
|
19
|
+
@adapters = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def add(name, options)
|
23
|
+
if adapter(name)
|
24
|
+
raise AdapterError.new("adapter #{name} exists")
|
25
|
+
end
|
26
|
+
|
27
|
+
adapter = build_adapter(options)
|
28
|
+
adapter.name = name
|
29
|
+
@adapters[name] = adapter
|
30
|
+
end
|
31
|
+
|
32
|
+
def build_adapter(options)
|
33
|
+
opts = options.dup
|
34
|
+
name = opts.delete(:adapter).capitalize
|
35
|
+
"Omnidata::Adapters::#{name}Adapter".constantize.new(opts)
|
36
|
+
end
|
37
|
+
|
38
|
+
def adapter(name)
|
39
|
+
@adapters[name]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
gem 'mongo'
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
module Omnidata
|
5
|
+
module Adapters
|
6
|
+
class MongodbAdapter < AbstractAdapter
|
7
|
+
def database
|
8
|
+
@db ||= begin
|
9
|
+
host = config[:host]
|
10
|
+
port = config[:port]
|
11
|
+
Mongo::Connection.new(host, port).db(config[:database])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def find(pk, table_name)
|
16
|
+
find_one(pk, table_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_one(pk, table_name)
|
20
|
+
table(table_name).find_one("_id" => build_key(pk))
|
21
|
+
end
|
22
|
+
|
23
|
+
def create(table_name, attrs)
|
24
|
+
key = table(table_name).insert(attrs)
|
25
|
+
key.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
def update(pk, table_name, attrs)
|
29
|
+
table(table_name).update({"_id" => pk}, {"$set" => attrs})
|
30
|
+
end
|
31
|
+
|
32
|
+
def count(table_name)
|
33
|
+
table(table_name).count
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy(pk, table_name)
|
37
|
+
table(table_name).remove("_id" => build_key(pk))
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def build_key(pk)
|
42
|
+
pk.kind_of?(String) ? BSON::ObjectId(pk) : pk
|
43
|
+
end
|
44
|
+
|
45
|
+
def table(name)
|
46
|
+
database.collection(name)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/lib/omnidata/orm.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Omnidata
|
4
|
+
module Orm
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
attr_accessor :id
|
9
|
+
end
|
10
|
+
|
11
|
+
def new_record?
|
12
|
+
self.id.nil?
|
13
|
+
rescue
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def table_name
|
20
|
+
@table_name || self.name.tableize.gsub("/", "_")
|
21
|
+
end
|
22
|
+
|
23
|
+
def table_name=(str)
|
24
|
+
@table_name = str
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
module Omnidata
|
5
|
+
module Persistence
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
end
|
10
|
+
|
11
|
+
def save
|
12
|
+
if new_record?
|
13
|
+
self.id = self.class.create(attributes)
|
14
|
+
else
|
15
|
+
self.class.update(id, attributes)
|
16
|
+
end
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def destroy
|
21
|
+
self.class.destroy(id)
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
attr_reader :adapter
|
26
|
+
|
27
|
+
def use_database(name)
|
28
|
+
adapter = Adapters::AdapterManager.instance.adapter(name)
|
29
|
+
raise Adapters::AdapterError.new("adapter #{name} does not exist") if adapter.nil?
|
30
|
+
@adapter = adapter
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_model(pk, attrs)
|
34
|
+
model = new(attrs)
|
35
|
+
model.id = pk
|
36
|
+
model
|
37
|
+
end
|
38
|
+
|
39
|
+
def find(pk)
|
40
|
+
attrs = adapter.find(pk, table_name)
|
41
|
+
if (attrs)
|
42
|
+
build_model(pk, attrs)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def create(attrs)
|
47
|
+
key = adapter.create(table_name, attrs)
|
48
|
+
build_model(key, attrs)
|
49
|
+
end
|
50
|
+
|
51
|
+
def update(pk, attrs)
|
52
|
+
adapter.update(pk, table_name, attrs)
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def destroy(pk)
|
57
|
+
adapter.destroy(pk, table_name)
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def count
|
62
|
+
adapter.count(table_name)
|
63
|
+
end
|
64
|
+
|
65
|
+
def with_database(name, &block)
|
66
|
+
old = self.adapter.name
|
67
|
+
self.use_database(name)
|
68
|
+
block.call
|
69
|
+
self.use_database(old)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
data/omnidata.gemspec
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "omnidata"
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dorren Chen"]
|
12
|
+
s.date = "2012-04-28"
|
13
|
+
s.description = "Omnidata let you define your model free from any specific database."
|
14
|
+
s.email = "dorrenchen@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.md",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/omnidata.rb",
|
29
|
+
"lib/omnidata/adapters/abstract_adapter.rb",
|
30
|
+
"lib/omnidata/adapters/adapter_manager.rb",
|
31
|
+
"lib/omnidata/adapters/mongodb_adapter.rb",
|
32
|
+
"lib/omnidata/model.rb",
|
33
|
+
"lib/omnidata/orm.rb",
|
34
|
+
"lib/omnidata/persistence.rb",
|
35
|
+
"omnidata.gemspec",
|
36
|
+
"spec/adapters/adapter_manager_spec.rb",
|
37
|
+
"spec/adapters/mongodb_adapter_spec.rb",
|
38
|
+
"spec/fixtures/example_models.rb",
|
39
|
+
"spec/shared_orm.rb",
|
40
|
+
"spec/shared_persistence.rb",
|
41
|
+
"spec/spec_helper.rb"
|
42
|
+
]
|
43
|
+
s.homepage = "http://github.com/dorren/omnidata"
|
44
|
+
s.licenses = ["MIT"]
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
s.rubygems_version = "1.8.23"
|
47
|
+
s.summary = "Omnidata is a persistence agnostic ORM gem"
|
48
|
+
|
49
|
+
if s.respond_to? :specification_version then
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_runtime_dependency(%q<active_support>, [">= 0"])
|
54
|
+
s.add_runtime_dependency(%q<virtus>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
56
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
57
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
58
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<active_support>, [">= 0"])
|
61
|
+
s.add_dependency(%q<virtus>, [">= 0"])
|
62
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
63
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
64
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
65
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
66
|
+
end
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<active_support>, [">= 0"])
|
69
|
+
s.add_dependency(%q<virtus>, [">= 0"])
|
70
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
71
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
72
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
73
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Omnidata::Adapters::AdapterManager do
|
4
|
+
let(:mgr) { Omnidata::Adapters::AdapterManager.instance }
|
5
|
+
let(:config) {{:adapter => 'mongodb', :database => 'mydb'}}
|
6
|
+
|
7
|
+
before(:each){ mgr.reset }
|
8
|
+
|
9
|
+
it "should init" do
|
10
|
+
mgr.adapters.should be_empty
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should store named configurations" do
|
14
|
+
adapter = mgr.add(:db1, config)
|
15
|
+
adapter.should be_a_kind_of(Omnidata::Adapters::AbstractAdapter)
|
16
|
+
mgr.adapter(:db1).should == adapter
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not add twice" do
|
20
|
+
adapter = mgr.add(:db1, config)
|
21
|
+
lambda { mgr.add(:db1, config) }.should raise_error(Omnidata::Adapters::AdapterError)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should use shortcut method" do
|
25
|
+
mgr.adapters.should be_empty
|
26
|
+
Omnidata.setup_database(:db1, config)
|
27
|
+
mgr.adapters.should_not be_empty
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shared_orm'
|
3
|
+
require 'shared_persistence'
|
4
|
+
|
5
|
+
describe Omnidata::Adapters::MongodbAdapter do
|
6
|
+
let(:mgr) { Omnidata::Adapters::AdapterManager.instance }
|
7
|
+
let(:user) { Example::User.new }
|
8
|
+
let(:config) {{:adapter => 'mongodb', :database => 'mydb'}}
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
mgr.reset
|
12
|
+
Omnidata.setup_database(:db1, config)
|
13
|
+
Omnidata.setup_database(:db2, {:adapter => 'mongodb', :database => 'mydb2'})
|
14
|
+
|
15
|
+
user.class.use_database(:db1)
|
16
|
+
end
|
17
|
+
|
18
|
+
before(:each) do
|
19
|
+
db = user.class.adapter.database
|
20
|
+
table_name = user.class.table_name
|
21
|
+
db.collection(table_name).remove
|
22
|
+
|
23
|
+
Omnidata.adapter(:db2).database.collection(user.class.table_name).remove
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should find adapter class" do
|
27
|
+
user.class.adapter.should be_an_instance_of Omnidata::Adapters::MongodbAdapter
|
28
|
+
end
|
29
|
+
|
30
|
+
include_examples 'Orm'
|
31
|
+
include_examples 'Persistence'
|
32
|
+
end
|
33
|
+
|
34
|
+
|
data/spec/shared_orm.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples 'Orm' do
|
4
|
+
|
5
|
+
it "should be new record" do
|
6
|
+
user.should be_new_record
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should have default table name" do
|
10
|
+
user.class.table_name.should == 'example_users'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should change table name" do
|
14
|
+
user.class.table_name = 'users'
|
15
|
+
user.class.table_name.should == 'users'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "Persistence" do
|
4
|
+
before(:each) do
|
5
|
+
user.name = 'someone'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should raise error if adapter not found" do
|
9
|
+
lambda { user.class.use_database(:bad_db) }.should raise_error(Omnidata::Adapters::AdapterError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should create" do
|
13
|
+
user.should be_new_record
|
14
|
+
user.save
|
15
|
+
user.should_not be_new_record
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "with saved model" do
|
19
|
+
before(:each) do
|
20
|
+
@user = Example::User.create(:name => 'Jack')
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should find" do
|
24
|
+
user = Example::User.find(@user.id.to_s)
|
25
|
+
user.id.should == @user.id
|
26
|
+
user.name.should == 'Jack'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not find" do
|
30
|
+
user2 = Example::User.create(:name => 'Jill')
|
31
|
+
user2.destroy
|
32
|
+
|
33
|
+
user = Example::User.find(user2.id) # mongo requires valid id, not just random str.
|
34
|
+
user.should be_nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should destroy" do
|
38
|
+
lambda { @user.destroy }.should change(Example::User, :count).from(1).to(0)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should count" do
|
43
|
+
user.class.count.should == 0
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be able to switch database" do
|
47
|
+
user.class.adapter.name.should be(:db1)
|
48
|
+
|
49
|
+
user.class.with_database(:db2) do
|
50
|
+
user.class.adapter.name.should be(:db2)
|
51
|
+
user.class.create(:name => 'Jack')
|
52
|
+
user.class.count.should == 1
|
53
|
+
end
|
54
|
+
user.class.adapter.name.should be(:db1)
|
55
|
+
user.class.count.should == 0
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'backports' if RUBY_VERSION < '1.9.3'
|
5
|
+
require 'rspec'
|
6
|
+
require 'omnidata'
|
7
|
+
require 'fixtures/example_models'
|
8
|
+
|
9
|
+
# Requires supporting files with custom matchers and macros, etc,
|
10
|
+
# in ./support/ and its subdirectories.
|
11
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omnidata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dorren Chen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: active_support
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: virtus
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.8.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.8.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rdoc
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.12'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.12'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: bundler
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: jeweler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.8.3
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.8.3
|
110
|
+
description: Omnidata let you define your model free from any specific database.
|
111
|
+
email: dorrenchen@gmail.com
|
112
|
+
executables: []
|
113
|
+
extensions: []
|
114
|
+
extra_rdoc_files:
|
115
|
+
- LICENSE.txt
|
116
|
+
- README.md
|
117
|
+
files:
|
118
|
+
- .document
|
119
|
+
- .rspec
|
120
|
+
- Gemfile
|
121
|
+
- Gemfile.lock
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- VERSION
|
126
|
+
- lib/omnidata.rb
|
127
|
+
- lib/omnidata/adapters/abstract_adapter.rb
|
128
|
+
- lib/omnidata/adapters/adapter_manager.rb
|
129
|
+
- lib/omnidata/adapters/mongodb_adapter.rb
|
130
|
+
- lib/omnidata/model.rb
|
131
|
+
- lib/omnidata/orm.rb
|
132
|
+
- lib/omnidata/persistence.rb
|
133
|
+
- omnidata.gemspec
|
134
|
+
- spec/adapters/adapter_manager_spec.rb
|
135
|
+
- spec/adapters/mongodb_adapter_spec.rb
|
136
|
+
- spec/fixtures/example_models.rb
|
137
|
+
- spec/shared_orm.rb
|
138
|
+
- spec/shared_persistence.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
homepage: http://github.com/dorren/omnidata
|
141
|
+
licenses:
|
142
|
+
- MIT
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
segments:
|
154
|
+
- 0
|
155
|
+
hash: -1325255740575885006
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
|
+
none: false
|
158
|
+
requirements:
|
159
|
+
- - ! '>='
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
requirements: []
|
163
|
+
rubyforge_project:
|
164
|
+
rubygems_version: 1.8.23
|
165
|
+
signing_key:
|
166
|
+
specification_version: 3
|
167
|
+
summary: Omnidata is a persistence agnostic ORM gem
|
168
|
+
test_files: []
|