mongodb_model 0.0.8 → 0.0.9
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/lib/mongo/model/assignment.rb +4 -8
- data/lib/mongo/model/attribute_convertors.rb +0 -12
- data/lib/mongo/model/callbacks.rb +3 -0
- data/lib/mongo/model/crud.rb +13 -10
- data/lib/mongo/model/db.rb +4 -1
- data/lib/mongo/model/model.rb +6 -0
- data/lib/mongo/model/query.rb +19 -12
- data/spec/associations_spec.rb +1 -1
- data/spec/query_spec.rb +14 -0
- metadata +1 -1
@@ -35,14 +35,10 @@ module Mongo::Model::Assignment
|
|
35
35
|
force = options[:force]
|
36
36
|
attributes.each do |n, v|
|
37
37
|
n = n.to_sym
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
send "#{n}=", v
|
43
|
-
else
|
44
|
-
raise "mass assignment for :#{n} attribute not allowed!"
|
45
|
-
end
|
38
|
+
type, mass_assignment = rules[n]
|
39
|
+
if mass_assignment or force
|
40
|
+
v = type.cast(v) if type
|
41
|
+
send "#{n}=", v
|
46
42
|
else
|
47
43
|
raise "mass assignment for :#{n} attribute not allowed!"
|
48
44
|
end
|
@@ -37,18 +37,6 @@ module Mongo::Model::AttributeConvertors
|
|
37
37
|
self.send "#{name}=", from_string.call(value)
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
41
|
-
def available_as_yaml name
|
42
|
-
raise "delimiter not specified for :#{name} field!" unless delimiter
|
43
|
-
method = "#{name}_as_string"
|
44
|
-
define_method method do
|
45
|
-
self.send(name).join(delimiter)
|
46
|
-
end
|
47
|
-
define_method "#{method}=" do |value|
|
48
|
-
value = (value || "").split(delimiter.strip).collect{|s| s.strip}
|
49
|
-
self.send "#{name}=", value
|
50
|
-
end
|
51
|
-
end
|
52
40
|
end
|
53
41
|
|
54
42
|
end
|
data/lib/mongo/model/crud.rb
CHANGED
@@ -20,20 +20,23 @@ module Mongo::Model::Crud
|
|
20
20
|
end
|
21
21
|
|
22
22
|
module ClassMethods
|
23
|
-
def build attributes = {}, options = {}
|
24
|
-
self.new
|
23
|
+
def build attributes = {}, options = {}, &block
|
24
|
+
model = self.new
|
25
|
+
model.set attributes, options
|
26
|
+
block.call model if block
|
27
|
+
model
|
25
28
|
end
|
26
29
|
|
27
|
-
def create attributes = {}, options = {}
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
def create attributes = {}, options = {}, &block
|
31
|
+
model = build attributes, options, &block
|
32
|
+
model.save
|
33
|
+
model
|
31
34
|
end
|
32
35
|
|
33
|
-
def create! attributes = {}, options = {}
|
34
|
-
|
35
|
-
raise(Mongo::Error, "can't create #{attributes.inspect}!")
|
36
|
-
|
36
|
+
def create! attributes = {}, options = {}, &block
|
37
|
+
model = build attributes, options, &block
|
38
|
+
model.save || raise(Mongo::Error, "can't create #{attributes.inspect}!")
|
39
|
+
model
|
37
40
|
end
|
38
41
|
|
39
42
|
def destroy_all selector = {}, options = {}
|
data/lib/mongo/model/db.rb
CHANGED
data/lib/mongo/model/model.rb
CHANGED
@@ -28,5 +28,11 @@ module Mongo::Model
|
|
28
28
|
class << self
|
29
29
|
attr_accessor :db, :connection
|
30
30
|
attr_required :db, :connection
|
31
|
+
|
32
|
+
# Override this method to provide custom alias to db name translation,
|
33
|
+
# for example db_name = my_config[alias_name]
|
34
|
+
def resolve_db_alias alias_name
|
35
|
+
alias_name.to_s
|
36
|
+
end
|
31
37
|
end
|
32
38
|
end
|
data/lib/mongo/model/query.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
class Mongo::Model::Query < Object
|
2
|
-
attr_reader :
|
2
|
+
attr_reader :model_class, :selector, :options
|
3
3
|
|
4
|
-
def initialize
|
5
|
-
@
|
4
|
+
def initialize model_class, selector = {}, options = {} *args
|
5
|
+
@model_class, @selector, @options = model_class, selector, options
|
6
6
|
end
|
7
7
|
|
8
8
|
def class
|
@@ -10,35 +10,42 @@ class Mongo::Model::Query < Object
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def merge query
|
13
|
-
raise "can't merge queries with different models!" unless
|
14
|
-
self.class.new
|
13
|
+
raise "can't merge queries with different models!" unless model_class == query.model_class
|
14
|
+
self.class.new model_class, selector.merge(query.selector), options.merge(query.options)
|
15
15
|
end
|
16
16
|
|
17
17
|
def inspect
|
18
|
-
"#<Mongo::Model::Query: #{
|
18
|
+
"#<Mongo::Model::Query: #{model_class} #{@selector.inspect} #{@options.inspect}>"
|
19
19
|
end
|
20
20
|
alias_method :to_s, :inspect
|
21
21
|
|
22
22
|
def == o
|
23
|
-
self.class == o.class and ([
|
23
|
+
self.class == o.class and ([model_class, selector, options] == [o.model_class, o.selector, o.options])
|
24
24
|
end
|
25
25
|
|
26
26
|
def build attributes = {}, options = {}
|
27
|
-
|
27
|
+
model_class.build attributes, options do |model|
|
28
|
+
model.set! selector
|
29
|
+
end
|
28
30
|
end
|
29
31
|
|
30
32
|
def create attributes = {}, options = {}
|
31
|
-
|
33
|
+
model_class.create attributes, options do |model|
|
34
|
+
model.set! selector
|
35
|
+
end
|
32
36
|
end
|
33
37
|
|
34
38
|
def create! attributes = {}, options = {}
|
35
|
-
|
39
|
+
model_class.create! attributes, options do |model|
|
40
|
+
model.set! selector
|
41
|
+
end
|
36
42
|
end
|
37
43
|
|
38
44
|
protected
|
45
|
+
|
39
46
|
def method_missing method, *args, &block
|
40
|
-
|
41
|
-
result =
|
47
|
+
model_class.with_scope selector, options do
|
48
|
+
result = model_class.send method, *args, &block
|
42
49
|
result = self.merge result if result.is_a? self.class
|
43
50
|
result
|
44
51
|
end
|
data/spec/associations_spec.rb
CHANGED
data/spec/query_spec.rb
CHANGED
@@ -64,4 +64,18 @@ describe "Model Query" do
|
|
64
64
|
u = SpecialUnit.first
|
65
65
|
[u.name, u.age].should == ['Zeratul', 500]
|
66
66
|
end
|
67
|
+
|
68
|
+
it "build should assign protected attributes" do
|
69
|
+
class SpecialUnit < Unit
|
70
|
+
attr_accessor :age, :status
|
71
|
+
assign do
|
72
|
+
name String, true
|
73
|
+
age Integer, true
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
-> {SpecialUnit.query(name: 'Zeratul').build age: 500, status: 'active'}.should raise_error(/not allowed/)
|
78
|
+
u = SpecialUnit.query(name: 'Zeratul', status: 'active').build age: 500
|
79
|
+
u.status.should == 'active'
|
80
|
+
end
|
67
81
|
end
|