uorm 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 +19 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/uorm.rb +23 -0
- data/lib/uorm/attributes.rb +59 -0
- data/lib/uorm/callbacks.rb +29 -0
- data/lib/uorm/dsl.rb +53 -0
- data/lib/uorm/em-mongo.rb +26 -0
- data/lib/uorm/em-mongo/db.rb +21 -0
- data/lib/uorm/em-mongo/object_id.rb +15 -0
- data/lib/uorm/em-mongo/persistance.rb +47 -0
- data/lib/uorm/field.rb +13 -0
- data/lib/uorm/field_collection.rb +7 -0
- data/lib/uorm/type.rb +63 -0
- data/lib/uorm/version.rb +3 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/support/be_collection_of.rb +8 -0
- data/spec/urm_spec.rb +160 -0
- data/uorm.gemspec +26 -0
- metadata +150 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
uorm (0.0.1)
|
|
5
|
+
activesupport
|
|
6
|
+
em-synchrony
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activesupport (3.2.9)
|
|
12
|
+
i18n (~> 0.6)
|
|
13
|
+
multi_json (~> 1.0)
|
|
14
|
+
bson (1.8.0)
|
|
15
|
+
activesupport
|
|
16
|
+
bson_ext (1.8.0)
|
|
17
|
+
bson (~> 1.8.0)
|
|
18
|
+
diff-lcs (1.1.3)
|
|
19
|
+
em-mongo (0.4.3)
|
|
20
|
+
bson (>= 1.1.3)
|
|
21
|
+
eventmachine (>= 0.12.10)
|
|
22
|
+
em-synchrony (1.0.2)
|
|
23
|
+
eventmachine (>= 1.0.0.beta.1)
|
|
24
|
+
eventmachine (1.0.0)
|
|
25
|
+
i18n (0.6.1)
|
|
26
|
+
multi_json (1.5.0)
|
|
27
|
+
rspec (2.12.0)
|
|
28
|
+
rspec-core (~> 2.12.0)
|
|
29
|
+
rspec-expectations (~> 2.12.0)
|
|
30
|
+
rspec-mocks (~> 2.12.0)
|
|
31
|
+
rspec-core (2.12.2)
|
|
32
|
+
rspec-expectations (2.12.1)
|
|
33
|
+
diff-lcs (~> 1.1.3)
|
|
34
|
+
rspec-mocks (2.12.1)
|
|
35
|
+
|
|
36
|
+
PLATFORMS
|
|
37
|
+
ruby
|
|
38
|
+
|
|
39
|
+
DEPENDENCIES
|
|
40
|
+
bson_ext
|
|
41
|
+
em-mongo
|
|
42
|
+
rspec
|
|
43
|
+
uorm!
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Dennis Rogenius
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Uorm
|
|
2
|
+
|
|
3
|
+
TODO: Write a gem description
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'uorm'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install uorm
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
TODO: Write usage instructions here
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
1. Fork it
|
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/uorm.rb
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'uorm/version'
|
|
2
|
+
|
|
3
|
+
require 'active_support/callbacks'
|
|
4
|
+
require 'active_support/core_ext/hash'
|
|
5
|
+
|
|
6
|
+
module Uorm
|
|
7
|
+
autoload :Type, 'uorm/type'
|
|
8
|
+
autoload :Field, 'uorm/field'
|
|
9
|
+
autoload :FieldCollection, 'uorm/field_collection'
|
|
10
|
+
autoload :Attributes, 'uorm/attributes'
|
|
11
|
+
autoload :DSL, 'uorm/dsl'
|
|
12
|
+
autoload :Callbacks, 'uorm/callbacks'
|
|
13
|
+
|
|
14
|
+
module EM
|
|
15
|
+
autoload :Mongo, 'uorm/em-mongo'
|
|
16
|
+
|
|
17
|
+
module Mongo
|
|
18
|
+
require 'uorm/em-mongo/object_id'
|
|
19
|
+
autoload :DB, 'uorm/em-mongo/db'
|
|
20
|
+
autoload :Persistance, 'uorm/em-mongo/persistance'
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module Uorm
|
|
2
|
+
module Attributes
|
|
3
|
+
def self.included base
|
|
4
|
+
base.instance_eval do
|
|
5
|
+
def field name, options
|
|
6
|
+
fields << Field.new(name, options[:type])
|
|
7
|
+
|
|
8
|
+
define_method name do
|
|
9
|
+
get_attribute name
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
define_method :"#{name}=" do |value|
|
|
13
|
+
set_attribute name, fields.get(name).convert(value)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def fields
|
|
18
|
+
@fields ||= FieldCollection.new
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :attributes
|
|
24
|
+
|
|
25
|
+
def initialize values = {}
|
|
26
|
+
initialize_attributes values
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def as_json
|
|
30
|
+
attributes.inject({}) do |hash, (key, value)|
|
|
31
|
+
hash[key] = (field = fields.get(key)) ? field.as_json(value) : value
|
|
32
|
+
hash
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def fields
|
|
39
|
+
self.class.fields
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def initialize_attributes values
|
|
43
|
+
@attributes = {}
|
|
44
|
+
update_attributes values
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def update_attributes attrs
|
|
48
|
+
attrs.each { |key, value| send(:"#{key}=", value) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def set_attribute name, value
|
|
52
|
+
@attributes[name.to_sym] = value
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def get_attribute name
|
|
56
|
+
@attributes[name.to_sym]
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Uorm
|
|
2
|
+
module Callbacks
|
|
3
|
+
|
|
4
|
+
def self.included base
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
include ::ActiveSupport::Callbacks
|
|
7
|
+
define_callbacks :save, :create, :update, :delete, scope: [:kind, :name]
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def perform_create
|
|
14
|
+
run_callbacks(:save) do
|
|
15
|
+
run_callbacks(:create) { super }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def perform_update
|
|
20
|
+
run_callbacks(:save) do
|
|
21
|
+
run_callbacks(:update) { super }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def perform_delete
|
|
26
|
+
run_callbacks(:delete) { super }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/uorm/dsl.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module Uorm
|
|
2
|
+
module DSL
|
|
3
|
+
def self.included base
|
|
4
|
+
base.instance_eval do
|
|
5
|
+
def all args = {}
|
|
6
|
+
persistance.all(args).map { |attrs| new attrs }
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def find args
|
|
10
|
+
new persistance.find args
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def create attrs
|
|
14
|
+
new(attrs).save
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def new?
|
|
20
|
+
persistance.new? self
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def save
|
|
24
|
+
new? ? perform_create : perform_update
|
|
25
|
+
self
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def update attrs
|
|
29
|
+
update_attributes attrs
|
|
30
|
+
perform_update
|
|
31
|
+
self
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def delete
|
|
35
|
+
perform_delete
|
|
36
|
+
self
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def perform_create
|
|
42
|
+
persistance.create self
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def perform_update
|
|
46
|
+
persistance.update self
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def perform_delete
|
|
50
|
+
persistance.delete self
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Uorm
|
|
2
|
+
module EM::Mongo
|
|
3
|
+
def self.included base
|
|
4
|
+
base.instance_eval do
|
|
5
|
+
field :id, type: Type::ObjectId
|
|
6
|
+
|
|
7
|
+
def persistance
|
|
8
|
+
@persistance ||= Persistance.new self, "#{self.to_s.downcase}s"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def collection
|
|
12
|
+
persistance.collection
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize values = {}
|
|
17
|
+
values[:id] ||= values.delete('_id') || values.delete(:_id)
|
|
18
|
+
super values
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def persistance
|
|
22
|
+
self.class.persistance
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Uorm
|
|
2
|
+
module EM::Mongo
|
|
3
|
+
class DB
|
|
4
|
+
class << self
|
|
5
|
+
attr_accessor :name
|
|
6
|
+
|
|
7
|
+
def collection coll
|
|
8
|
+
db.collection coll
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def db
|
|
12
|
+
@db ||= connection.db name
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def connection
|
|
16
|
+
@connection ||= ::EM::Mongo::Connection.new
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Uorm
|
|
2
|
+
module EM::Mongo
|
|
3
|
+
class Persistance
|
|
4
|
+
attr_reader :collection, :model
|
|
5
|
+
|
|
6
|
+
def initialize model, collection_name
|
|
7
|
+
@model = model
|
|
8
|
+
@collection = DB.collection collection_name
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def new? object
|
|
12
|
+
object.id ? false : true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def all query = {}
|
|
16
|
+
collection.find format_query(query)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def find id
|
|
20
|
+
collection.find_one _id: BSON::ObjectId(id.to_s)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def create object
|
|
24
|
+
object.id = collection.insert object.attributes.except(:id)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def update object
|
|
28
|
+
collection.update({_id: object.id}, object.attributes.except(:id))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def delete object
|
|
32
|
+
collection.remove _id: object.id
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def format_query hash, field = nil
|
|
38
|
+
hash.inject({}) do |memo, (key, value)|
|
|
39
|
+
raise "#{key} field unknown" unless current = field || model.fields.get(key)
|
|
40
|
+
|
|
41
|
+
memo[key] = value.is_a?(Hash) ? format_query(value, current) : current.convert(value)
|
|
42
|
+
memo
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/uorm/field.rb
ADDED
data/lib/uorm/type.rb
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module Uorm
|
|
2
|
+
module Type
|
|
3
|
+
class Integer
|
|
4
|
+
class << self
|
|
5
|
+
def convert value
|
|
6
|
+
value.to_i
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def as_json value
|
|
10
|
+
convert value
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class Boolean
|
|
16
|
+
class << self
|
|
17
|
+
def convert value
|
|
18
|
+
value ? true : false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def as_json value
|
|
22
|
+
convert value
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Float
|
|
28
|
+
class << self
|
|
29
|
+
def convert value
|
|
30
|
+
value.to_f
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def as_json value
|
|
34
|
+
convert value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class Time
|
|
40
|
+
class << self
|
|
41
|
+
def convert value
|
|
42
|
+
::Time.parse value.to_s
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def as_json value
|
|
46
|
+
value.to_i
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class String
|
|
52
|
+
class << self
|
|
53
|
+
def convert value
|
|
54
|
+
value.to_s
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def as_json value
|
|
58
|
+
convert value
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/uorm/version.rb
ADDED
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'uorm'
|
|
2
|
+
|
|
3
|
+
require 'em-mongo'
|
|
4
|
+
require 'em-synchrony'
|
|
5
|
+
require 'em-synchrony/em-mongo'
|
|
6
|
+
|
|
7
|
+
Uorm::EM::Mongo::DB.name = 'uorm_test'
|
|
8
|
+
|
|
9
|
+
require 'support/be_collection_of'
|
|
10
|
+
|
|
11
|
+
# monkey patch to wrap all tests in the reactor
|
|
12
|
+
module RSpec
|
|
13
|
+
module Core
|
|
14
|
+
class ExampleGroup
|
|
15
|
+
class << self
|
|
16
|
+
|
|
17
|
+
alias_method :run_alias, :run
|
|
18
|
+
|
|
19
|
+
def run(reporter)
|
|
20
|
+
if EM.reactor_running?
|
|
21
|
+
run_alias reporter
|
|
22
|
+
else
|
|
23
|
+
out = nil
|
|
24
|
+
EM.synchrony do
|
|
25
|
+
out = run_alias reporter
|
|
26
|
+
EM.next_tick { EM.stop }
|
|
27
|
+
end
|
|
28
|
+
out
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
data/spec/urm_spec.rb
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
class CallbackTester
|
|
4
|
+
class << self
|
|
5
|
+
def after_create model
|
|
6
|
+
model.id.to_s.should_not == ''
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def after_save model
|
|
10
|
+
model.is_a?(AModel).should == true
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class AModel
|
|
16
|
+
include Uorm::DSL
|
|
17
|
+
include Uorm::Attributes
|
|
18
|
+
include Uorm::EM::Mongo
|
|
19
|
+
include Uorm::Callbacks
|
|
20
|
+
|
|
21
|
+
field :reference, type: Uorm::Type::ObjectId
|
|
22
|
+
field :name, type: Uorm::Type::String
|
|
23
|
+
field :count, type: Uorm::Type::Integer
|
|
24
|
+
field :dollars, type: Uorm::Type::Float
|
|
25
|
+
field :favorite, type: Uorm::Type::Boolean
|
|
26
|
+
field :created, type: Uorm::Type::Time
|
|
27
|
+
|
|
28
|
+
set_callback :save, :after, CallbackTester
|
|
29
|
+
set_callback :create, :after, CallbackTester
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
describe AModel do
|
|
33
|
+
after { AModel.collection.remove }
|
|
34
|
+
|
|
35
|
+
let(:attributes) do
|
|
36
|
+
{
|
|
37
|
+
reference: '50995b8c15529d4676000001',
|
|
38
|
+
name: 'whatever',
|
|
39
|
+
count: 5.to_s,
|
|
40
|
+
dollars: 2.3.to_s,
|
|
41
|
+
favorite: true.to_s,
|
|
42
|
+
created: Time.utc(0).to_s
|
|
43
|
+
}
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe '.new' do
|
|
47
|
+
subject { AModel.new attributes }
|
|
48
|
+
|
|
49
|
+
its(:reference) { should == BSON::ObjectId('50995b8c15529d4676000001') }
|
|
50
|
+
its(:name) { should == 'whatever' }
|
|
51
|
+
its(:count) { should == 5 }
|
|
52
|
+
its(:dollars) { should == 2.3 }
|
|
53
|
+
its(:favorite) { should be_true }
|
|
54
|
+
its(:created) { should == Time.utc(0) }
|
|
55
|
+
|
|
56
|
+
its(:as_json) do
|
|
57
|
+
should == {
|
|
58
|
+
reference: '50995b8c15529d4676000001',
|
|
59
|
+
name: "whatever",
|
|
60
|
+
count: 5,
|
|
61
|
+
dollars: 2.3,
|
|
62
|
+
favorite: true,
|
|
63
|
+
created: Time.utc(0).to_i,
|
|
64
|
+
id: nil
|
|
65
|
+
}
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe '.all' do
|
|
70
|
+
it 'returns a collection of all objects' do
|
|
71
|
+
AModel.collection.insert name: 'findme'
|
|
72
|
+
|
|
73
|
+
AModel.all.should be_collection_of AModel
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it 'returns a collection of objects with the query' do
|
|
77
|
+
AModel.collection.insert name: 'findme'
|
|
78
|
+
|
|
79
|
+
AModel.all(name: 'findme').should be_collection_of AModel
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it 'converts time strings into time objects' do
|
|
83
|
+
AModel.collection.insert created: Time.parse('2001-01-02')
|
|
84
|
+
|
|
85
|
+
AModel.all(created: { '$gt' => '2001-01-01' }).
|
|
86
|
+
should be_collection_of AModel
|
|
87
|
+
|
|
88
|
+
AModel.all(created: { '$lt' => Time.parse('2001-01-01')}).
|
|
89
|
+
should be_empty
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'converts reference strings into BSON::ObjectId objects' do
|
|
93
|
+
AModel.collection.insert reference: BSON::ObjectId('50995b8c15529d4676000001')
|
|
94
|
+
|
|
95
|
+
AModel.all(reference: '50995b8c15529d4676000001').
|
|
96
|
+
should be_collection_of AModel
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it 'converts multiple query parameters' do
|
|
100
|
+
AModel.collection.insert reference: BSON::ObjectId('50995b8c15529d4676000001'), created: Time.parse('2003-01-01')
|
|
101
|
+
|
|
102
|
+
AModel.all(reference: '50995b8c15529d4676000001', created: '2003-01-01').
|
|
103
|
+
should be_collection_of AModel
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
describe '.find' do
|
|
108
|
+
it 'returns the first object with the given id' do
|
|
109
|
+
model = AModel.create attributes
|
|
110
|
+
AModel.find(model.id).should be_instance_of AModel
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it 'converts id BSON::ObjectId' do
|
|
114
|
+
model = AModel.create attributes
|
|
115
|
+
AModel.find(model.id.to_s).should be_instance_of AModel
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
describe '.create' do
|
|
120
|
+
it 'adds the object to the collection' do
|
|
121
|
+
expect { AModel.create attributes }.to change { AModel.collection.count }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it 'returns an object with the newly created attributes' do
|
|
125
|
+
AModel.create(attributes).should be_instance_of AModel
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
describe '#save' do
|
|
130
|
+
it 'creates the object if it does not have an id' do
|
|
131
|
+
model = AModel.new attributes
|
|
132
|
+
expect { model.save }.to change { model.id }
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it 'updates the object if it does exist' do
|
|
136
|
+
model = AModel.create attributes
|
|
137
|
+
expect { model.save }.to_not change { model.id }
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
describe '#update' do
|
|
142
|
+
it 'updates the object with the given attributes' do
|
|
143
|
+
model = AModel.create attributes
|
|
144
|
+
expect { model.update name: 'new' }.to change { model.name }
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
it 'updates the collection with the given attributes' do
|
|
148
|
+
model = AModel.create attributes
|
|
149
|
+
expect { model.update name: 'another' }.
|
|
150
|
+
to change { AModel.collection.find(name: 'another').count }
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
describe '#delete' do
|
|
155
|
+
it 'removes the object from the collection' do
|
|
156
|
+
model = AModel.create attributes
|
|
157
|
+
expect { model.delete }.to change { AModel.collection.count }
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
data/uorm.gemspec
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'uorm/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |gem|
|
|
7
|
+
gem.name = "uorm"
|
|
8
|
+
gem.version = Uorm::VERSION
|
|
9
|
+
gem.authors = ["Dennis Rogenius"]
|
|
10
|
+
gem.email = ["denro03@gmail.com"]
|
|
11
|
+
gem.description = %q{A modular, flexible and lightweight ORM}
|
|
12
|
+
gem.summary = %q{A small ORM for ruby with focus on weight and modularity}
|
|
13
|
+
gem.homepage = ""
|
|
14
|
+
|
|
15
|
+
gem.files = `git ls-files`.split($/)
|
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
18
|
+
gem.require_paths = ["lib"]
|
|
19
|
+
|
|
20
|
+
gem.add_dependency 'activesupport'
|
|
21
|
+
gem.add_dependency 'em-synchrony'
|
|
22
|
+
|
|
23
|
+
gem.add_development_dependency 'rspec'
|
|
24
|
+
gem.add_development_dependency 'em-mongo'
|
|
25
|
+
gem.add_development_dependency 'bson_ext'
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: uorm
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Dennis Rogenius
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-12-27 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: activesupport
|
|
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: em-synchrony
|
|
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: '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: '0'
|
|
62
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: em-mongo
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
66
|
+
requirements:
|
|
67
|
+
- - ! '>='
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
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: '0'
|
|
78
|
+
- !ruby/object:Gem::Dependency
|
|
79
|
+
name: bson_ext
|
|
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
|
+
description: A modular, flexible and lightweight ORM
|
|
95
|
+
email:
|
|
96
|
+
- denro03@gmail.com
|
|
97
|
+
executables: []
|
|
98
|
+
extensions: []
|
|
99
|
+
extra_rdoc_files: []
|
|
100
|
+
files:
|
|
101
|
+
- .gitignore
|
|
102
|
+
- Gemfile
|
|
103
|
+
- Gemfile.lock
|
|
104
|
+
- LICENSE.txt
|
|
105
|
+
- README.md
|
|
106
|
+
- Rakefile
|
|
107
|
+
- lib/uorm.rb
|
|
108
|
+
- lib/uorm/attributes.rb
|
|
109
|
+
- lib/uorm/callbacks.rb
|
|
110
|
+
- lib/uorm/dsl.rb
|
|
111
|
+
- lib/uorm/em-mongo.rb
|
|
112
|
+
- lib/uorm/em-mongo/db.rb
|
|
113
|
+
- lib/uorm/em-mongo/object_id.rb
|
|
114
|
+
- lib/uorm/em-mongo/persistance.rb
|
|
115
|
+
- lib/uorm/field.rb
|
|
116
|
+
- lib/uorm/field_collection.rb
|
|
117
|
+
- lib/uorm/type.rb
|
|
118
|
+
- lib/uorm/version.rb
|
|
119
|
+
- spec/spec_helper.rb
|
|
120
|
+
- spec/support/be_collection_of.rb
|
|
121
|
+
- spec/urm_spec.rb
|
|
122
|
+
- uorm.gemspec
|
|
123
|
+
homepage: ''
|
|
124
|
+
licenses: []
|
|
125
|
+
post_install_message:
|
|
126
|
+
rdoc_options: []
|
|
127
|
+
require_paths:
|
|
128
|
+
- lib
|
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
|
+
none: false
|
|
131
|
+
requirements:
|
|
132
|
+
- - ! '>='
|
|
133
|
+
- !ruby/object:Gem::Version
|
|
134
|
+
version: '0'
|
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
|
+
none: false
|
|
137
|
+
requirements:
|
|
138
|
+
- - ! '>='
|
|
139
|
+
- !ruby/object:Gem::Version
|
|
140
|
+
version: '0'
|
|
141
|
+
requirements: []
|
|
142
|
+
rubyforge_project:
|
|
143
|
+
rubygems_version: 1.8.24
|
|
144
|
+
signing_key:
|
|
145
|
+
specification_version: 3
|
|
146
|
+
summary: A small ORM for ruby with focus on weight and modularity
|
|
147
|
+
test_files:
|
|
148
|
+
- spec/spec_helper.rb
|
|
149
|
+
- spec/support/be_collection_of.rb
|
|
150
|
+
- spec/urm_spec.rb
|