mongo_db 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_db/driver/core/collection.rb +92 -43
- data/lib/mongo_db/driver/core.rb +2 -3
- data/lib/mongo_db/driver/more/{collection.rb → collection_finders.rb} +16 -20
- data/lib/mongo_db/driver/more.rb +5 -2
- data/lib/mongo_db/driver/spec.rb +8 -8
- data/lib/mongo_db/model/model_helper.rb +154 -0
- data/lib/mongo_db/model/model_serializer.rb +3 -0
- data/lib/mongo_db/model.rb +13 -1
- data/lib/mongo_db/support.rb +4 -4
- data/readme.md +78 -17
- data/spec/driver/core/collection_spec.rb +11 -11
- data/spec/driver/core/crud_spec.rb +11 -11
- data/spec/driver/core/hash_helper_spec.rb +7 -2
- data/spec/{driver_example_spec.rb → driver/example_spec.rb} +25 -28
- data/spec/driver/fixes_spec.rb +12 -0
- data/spec/driver/more/querying_spec.rb +16 -16
- data/spec/driver/spec_helper.rb +2 -2
- data/spec/model/{object/callbacks.rb → callbacks.rb} +13 -13
- data/spec/model/{model/query.rb → example.rb} +0 -0
- data/spec/model/{model/crud.rb → model_crud.rb} +25 -25
- data/spec/model/{object/validation.rb → validation.rb} +18 -18
- data/spec/{model/object → object}/crud_shared.rb +8 -8
- data/spec/object/example_spec.rb +64 -0
- data/spec/{model/object/crud.rb → object/object_crud_spec.rb} +17 -17
- data/spec/object/spec_helper.rb +3 -0
- data/spec/test.rb +9 -4
- metadata +17 -17
- data/lib/mongo_db/driver/core/hash_helper.rb +0 -67
- data/lib/old/advanced_finders.rb +0 -26
- data/lib/old/query.rb +0 -91
- data/lib/old/query_spec.rb +0 -77
- data/spec/model/spec_helper.rb +0 -1
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'mongo_db/model'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
describe "Object example" do
|
5
|
+
defaults = nil
|
6
|
+
before(:all){defaults = Mongo.defaults.clone}
|
7
|
+
after :all do
|
8
|
+
Mongo.defaults = defaults
|
9
|
+
Object.send :remove_const, :Unit if Object.const_defined? :Unit
|
10
|
+
end
|
11
|
+
|
12
|
+
it do
|
13
|
+
# let's define the game unit
|
14
|
+
class Unit
|
15
|
+
attr_reader :name, :stats
|
16
|
+
|
17
|
+
# don't forget to allow creating object with no arguments
|
18
|
+
def initialize name = nil, stats = nil
|
19
|
+
@name, @stats = name, stats
|
20
|
+
end
|
21
|
+
|
22
|
+
class Stats
|
23
|
+
attr_accessor :attack, :life, :shield
|
24
|
+
|
25
|
+
def initialize attack = nil, life = nil, shield = nil
|
26
|
+
@attack, @life, @shield = attack, life, shield
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# connecting to MongoDB
|
32
|
+
require 'mongo_db/model'
|
33
|
+
Mongo.defaults.merge! symbolize: true, multi: true, safe: true
|
34
|
+
connection = Mongo::Connection.new
|
35
|
+
db = connection.db 'default_test'
|
36
|
+
|
37
|
+
# create
|
38
|
+
zeratul = Unit.new('Zeratul', Unit::Stats.new(85, 300, 100))
|
39
|
+
tassadar = Unit.new('Tassadar', Unit::Stats.new(0, 80, 300))
|
40
|
+
|
41
|
+
db.units.save zeratul
|
42
|
+
db.units.save tassadar
|
43
|
+
|
44
|
+
# udate (we made error - mistakenly set Tassadar's attack as zero, let's fix it)
|
45
|
+
tassadar.stats.attack = 20
|
46
|
+
db.units.save tassadar
|
47
|
+
|
48
|
+
# querying first & all, there's also :each, the same as :all
|
49
|
+
db.units.first name: 'Zeratul' # => zeratul
|
50
|
+
db.units.all name: 'Zeratul' # => [zeratul]
|
51
|
+
db.units.all name: 'Zeratul' do |unit|
|
52
|
+
unit # => zeratul
|
53
|
+
end
|
54
|
+
|
55
|
+
# simple finders (bang versions also availiable)
|
56
|
+
db.units.by_name 'Zeratul' # => zeratul
|
57
|
+
db.units.first_by_name 'Zeratul' # => zeratul
|
58
|
+
db.units.all_by_name 'Zeratul' # => [zeratul]
|
59
|
+
|
60
|
+
# query sugar, use {life: {_lt: 100}} instead of {life: {:$lt => 100}}
|
61
|
+
Mongo.defaults.merge! convert_underscore_to_dollar: true
|
62
|
+
db.units.all('stats.life' => {_lt: 100}) # => [tassadar]
|
63
|
+
end
|
64
|
+
end
|
@@ -1,37 +1,37 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require 'object/spec_helper'
|
2
2
|
require 'object/crud_shared'
|
3
3
|
|
4
4
|
describe "Object CRUD" do
|
5
|
-
|
6
|
-
|
5
|
+
with_mongo
|
6
|
+
|
7
7
|
describe 'simple' do
|
8
8
|
before do
|
9
9
|
class Person
|
10
|
-
def initialize name, info; @name, @info = name, info end
|
10
|
+
def initialize name = nil, info = nil; @name, @info = name, info end
|
11
11
|
attr_accessor :name, :info
|
12
12
|
def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
|
13
13
|
end
|
14
|
-
|
15
|
-
@zeratul =
|
14
|
+
|
15
|
+
@zeratul = Person.new 'Zeratul', 'Dark Templar'
|
16
16
|
end
|
17
17
|
after{remove_constants :Person}
|
18
|
-
|
18
|
+
|
19
19
|
it_should_behave_like "object CRUD"
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
describe 'embedded' do
|
23
|
-
before do
|
23
|
+
before do
|
24
24
|
class Player
|
25
25
|
attr_accessor :missions
|
26
|
-
def == o; [self.class, self.missions]
|
27
|
-
|
26
|
+
def == o; [self.class, self.missions] == [o.class, o.respond_to(:missions)] end
|
27
|
+
|
28
28
|
class Mission
|
29
|
-
def initialize name, stats; @name, @stats = name, stats end
|
29
|
+
def initialize name = nil, stats = nil; @name, @stats = name, stats end
|
30
30
|
attr_accessor :name, :stats
|
31
|
-
def == o; [self.class, self.name, self.stats]
|
32
|
-
end
|
31
|
+
def == o; [self.class, self.name, self.stats] == [o.class, o.respond_to(:name), o.respond_to(:stats)] end
|
32
|
+
end
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
@player = Player.new
|
36
36
|
@player.missions = [
|
37
37
|
Player::Mission.new('Wasteland', {buildings: 5, units: 10}),
|
@@ -39,7 +39,7 @@ describe "Object CRUD" do
|
|
39
39
|
]
|
40
40
|
end
|
41
41
|
after{remove_constants :Player}
|
42
|
-
|
42
|
+
|
43
43
|
it_should_behave_like 'embedded object CRUD'
|
44
|
-
end
|
44
|
+
end
|
45
45
|
end
|
data/spec/test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-14 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
16
|
-
requirement: &
|
16
|
+
requirement: &2844710 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '1.3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2844710
|
25
25
|
description:
|
26
26
|
email:
|
27
27
|
executables: []
|
@@ -32,33 +32,33 @@ files:
|
|
32
32
|
- readme.md
|
33
33
|
- lib/mongo_db/driver/core/collection.rb
|
34
34
|
- lib/mongo_db/driver/core/database.rb
|
35
|
-
- lib/mongo_db/driver/core/hash_helper.rb
|
36
35
|
- lib/mongo_db/driver/core.rb
|
37
|
-
- lib/mongo_db/driver/more/
|
36
|
+
- lib/mongo_db/driver/more/collection_finders.rb
|
38
37
|
- lib/mongo_db/driver/more.rb
|
39
38
|
- lib/mongo_db/driver/spec.rb
|
40
39
|
- lib/mongo_db/driver.rb
|
41
40
|
- lib/mongo_db/gems.rb
|
41
|
+
- lib/mongo_db/model/model_helper.rb
|
42
|
+
- lib/mongo_db/model/model_serializer.rb
|
42
43
|
- lib/mongo_db/model.rb
|
43
44
|
- lib/mongo_db/support.rb
|
44
|
-
- lib/old/advanced_finders.rb
|
45
|
-
- lib/old/query.rb
|
46
|
-
- lib/old/query_spec.rb
|
47
45
|
- spec/driver/core/collection_spec.rb
|
48
46
|
- spec/driver/core/crud_spec.rb
|
49
47
|
- spec/driver/core/database_spec.rb
|
50
48
|
- spec/driver/core/hash_helper_spec.rb
|
49
|
+
- spec/driver/example_spec.rb
|
50
|
+
- spec/driver/fixes_spec.rb
|
51
51
|
- spec/driver/more/querying_spec.rb
|
52
52
|
- spec/driver/spec_helper.rb
|
53
|
-
- spec/driver_example_spec.rb
|
54
53
|
- spec/migration/migration_spec.rb
|
55
|
-
- spec/model/
|
56
|
-
- spec/model/
|
57
|
-
- spec/model/
|
58
|
-
- spec/model/
|
59
|
-
- spec/
|
60
|
-
- spec/
|
61
|
-
- spec/
|
54
|
+
- spec/model/callbacks.rb
|
55
|
+
- spec/model/example.rb
|
56
|
+
- spec/model/model_crud.rb
|
57
|
+
- spec/model/validation.rb
|
58
|
+
- spec/object/crud_shared.rb
|
59
|
+
- spec/object/example_spec.rb
|
60
|
+
- spec/object/object_crud_spec.rb
|
61
|
+
- spec/object/spec_helper.rb
|
62
62
|
- spec/test.rb
|
63
63
|
homepage: http://github.com/alexeypetrushin/mongo_db
|
64
64
|
licenses: []
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
require 'date'
|
3
|
-
|
4
|
-
class Mongo::Ext::HashHelper
|
5
|
-
# SIMPLE_TYPES = [
|
6
|
-
# String, Symbol,
|
7
|
-
# Numeric,
|
8
|
-
# Regexp,
|
9
|
-
# Array,
|
10
|
-
# TrueClass, FalseClass,
|
11
|
-
# Date, DateTime,
|
12
|
-
# BSON::ObjectId
|
13
|
-
# ].to_set
|
14
|
-
|
15
|
-
QUERY_KEYWORDS = [
|
16
|
-
:_lt, :_lte, :_gt, :_gte,
|
17
|
-
:_all, :_exists, :_mod, :_ne, :_in, :_nin,
|
18
|
-
:_nor, :_or, :_and,
|
19
|
-
:_size, :_type
|
20
|
-
].to_set
|
21
|
-
|
22
|
-
UPDATE_KEYWORDS = [
|
23
|
-
:_inc, :_set, :_unset, :_push, :_pushAll, :_addToSet, :_pop, :_pull, :_pullAll, :_rename, :_bit
|
24
|
-
].to_set
|
25
|
-
|
26
|
-
class << self
|
27
|
-
# symbolizing hashes
|
28
|
-
def symbolize o
|
29
|
-
convert o do |k, v, result|
|
30
|
-
k = k.to_sym if k.is_a? String
|
31
|
-
result[k] = v
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# replaces :_lt to :$lt in query
|
36
|
-
def convert_underscore_to_dollar_in_selector selector
|
37
|
-
convert selector do |k, v, result|
|
38
|
-
k = "$#{k.to_s[1..-1]}".to_sym if QUERY_KEYWORDS.include?(k)
|
39
|
-
result[k] = v
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# replaces :_set to :$set in query
|
44
|
-
def convert_underscore_to_dollar_in_update update
|
45
|
-
convert update do |k, v, result|
|
46
|
-
k = "$#{k.to_s[1..-1]}".to_sym if UPDATE_KEYWORDS.include?(k)
|
47
|
-
result[k] = v
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# converts hashes (also works with nested & arrays)
|
52
|
-
def convert o, &block
|
53
|
-
if o.is_a? Hash
|
54
|
-
result = {}
|
55
|
-
o.each do |k, v|
|
56
|
-
v = convert v, &block
|
57
|
-
block.call k, v, result
|
58
|
-
end
|
59
|
-
result
|
60
|
-
elsif o.is_a? Array
|
61
|
-
o.collect{|v| convert v, &block}
|
62
|
-
else
|
63
|
-
o
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
data/lib/old/advanced_finders.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
before do
|
2
|
-
@zeratul = {name: 'Zeratul', stats: {attack: 85, life: 300, shield: 100}}
|
3
|
-
@tassadar = {name: 'Tassadar', stats: {attack: 20, life: 80, shield: 300}}
|
4
|
-
|
5
|
-
db.units.save @zeratul
|
6
|
-
db.units.save @tassadar
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
it "selector" do
|
19
|
-
db.units.select{|u|
|
20
|
-
u.stats.life < 100
|
21
|
-
u.name.exist
|
22
|
-
u.name.in
|
23
|
-
}.first.should == @tassadar
|
24
|
-
|
25
|
-
db.units.select{|u| }
|
26
|
-
end
|
data/lib/old/query.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
class Mongo::Ext::Query
|
2
|
-
class Dsl < BasicObject
|
3
|
-
class Statement < ::Array
|
4
|
-
def add_to hash
|
5
|
-
operator, value = self[-2, -1]
|
6
|
-
path = self[0..-3]
|
7
|
-
|
8
|
-
current = hash
|
9
|
-
path.each_with_index do |key, index|
|
10
|
-
if index == path.size - 1
|
11
|
-
raise "dupliate key :#{key}!" if current.include? key
|
12
|
-
current[key] = value
|
13
|
-
else
|
14
|
-
raise "dupliate key :#{key}!" if current.include?(key) and !current[key].is_a?(Hash)
|
15
|
-
current[key] ||= {}
|
16
|
-
current = current[key]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
nil
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def initialize &block
|
24
|
-
@statements = [Statement.new]
|
25
|
-
block.call self
|
26
|
-
statements.pop if statements.last.empty?
|
27
|
-
end
|
28
|
-
|
29
|
-
{
|
30
|
-
:== => :==,
|
31
|
-
:!= => :$ne,
|
32
|
-
:< => :$lt,
|
33
|
-
:<= => :$lte,
|
34
|
-
:> => :$gt,
|
35
|
-
:>= => :$gte,
|
36
|
-
}.each do |ruby, mongo|
|
37
|
-
define_method ruby do |arg|
|
38
|
-
emit mongo
|
39
|
-
emit arg, true
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def to_hash
|
44
|
-
h = {}
|
45
|
-
statements.each{|s| h.add_to h}
|
46
|
-
h
|
47
|
-
end
|
48
|
-
|
49
|
-
protected
|
50
|
-
attr_reader :statements
|
51
|
-
|
52
|
-
def emit statement = nil, terminate = false
|
53
|
-
statements.last << statement if statement
|
54
|
-
statements << Statement.new if terminate
|
55
|
-
nil
|
56
|
-
end
|
57
|
-
|
58
|
-
def method_missing m, *args, &block
|
59
|
-
raise "invalid usage, there can be only one argument (#{args})!" if args.size > 1
|
60
|
-
|
61
|
-
if args.empty?
|
62
|
-
emit m
|
63
|
-
self
|
64
|
-
else
|
65
|
-
emit m
|
66
|
-
emit args.first, true
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def p *args
|
71
|
-
::Kernel.p *args
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def initialize collection, &block
|
76
|
-
@collection = collection
|
77
|
-
@hash_query = Dsl.new(&block).to_hash
|
78
|
-
end
|
79
|
-
|
80
|
-
def first
|
81
|
-
collection.first to_hash
|
82
|
-
end
|
83
|
-
|
84
|
-
def all &block
|
85
|
-
collection.first to_hash, &block
|
86
|
-
end
|
87
|
-
alias_method :each, :all
|
88
|
-
|
89
|
-
protected
|
90
|
-
attr_reader :collection, :hash_query
|
91
|
-
end
|
data/lib/old/query_spec.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'driver/spec_helper'
|
2
|
-
|
3
|
-
describe "Query" do
|
4
|
-
dsl_class = Mongo::Ext::Query::Dsl
|
5
|
-
dsl_class.class_eval do
|
6
|
-
public :statements
|
7
|
-
end
|
8
|
-
|
9
|
-
it "operators" do
|
10
|
-
value = :value
|
11
|
-
dsl_class.new do |o|
|
12
|
-
o.key == value
|
13
|
-
o.key != value
|
14
|
-
o.key < value
|
15
|
-
o.key <= value
|
16
|
-
o.key > value
|
17
|
-
o.key >= value
|
18
|
-
o.key.all value
|
19
|
-
o.key.exists true
|
20
|
-
o.key.mod value
|
21
|
-
o.key.in value
|
22
|
-
o.key.nin value
|
23
|
-
o.key.size value
|
24
|
-
o.key.type value
|
25
|
-
end.statements.should == [
|
26
|
-
[:key, :==, :value],
|
27
|
-
[:key, :$ne, :value],
|
28
|
-
[:key, :$lt, :value],
|
29
|
-
[:key, :$lte, :value],
|
30
|
-
[:key, :$gt, :value],
|
31
|
-
[:key, :$gte, :value],
|
32
|
-
[:key, :all, :value],
|
33
|
-
[:key, :exists, true],
|
34
|
-
[:key, :mod, :value],
|
35
|
-
[:key, :in, :value],
|
36
|
-
[:key, :nin, :value],
|
37
|
-
[:key, :size, :value],
|
38
|
-
[:key, :type, :value]
|
39
|
-
]
|
40
|
-
end
|
41
|
-
|
42
|
-
it ":nor, :or, :and"
|
43
|
-
|
44
|
-
describe "statement" do
|
45
|
-
def process_statement *args
|
46
|
-
s = Mongo::Ext::Query::Dsl::Statement.new
|
47
|
-
s.push *args
|
48
|
-
result = {}
|
49
|
-
s.add_to result
|
50
|
-
result
|
51
|
-
end
|
52
|
-
|
53
|
-
it "basics", focus: true do
|
54
|
-
p process_statement(:age, :$gt, 34)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# it "to_hash" do
|
59
|
-
# dsl.new do |o|
|
60
|
-
# # o.name == 'Jim'
|
61
|
-
# o.age > 34
|
62
|
-
# end.to_hash.should == {
|
63
|
-
# # name: 'Jim',
|
64
|
-
# :$gt => {age: 34}
|
65
|
-
# }
|
66
|
-
# end
|
67
|
-
#
|
68
|
-
# it do
|
69
|
-
# dsl.new do |unit|
|
70
|
-
# unit.name == 'Zeratul'
|
71
|
-
# unit.stats.life > 100
|
72
|
-
# unit.stats.attack != 0
|
73
|
-
# end.statement.should == {
|
74
|
-
# name: 1
|
75
|
-
# }
|
76
|
-
# end
|
77
|
-
end
|
data/spec/model/spec_helper.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'mongo_model'
|