mongodb_model 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- if rule = rules[n]
39
- type, mass_assignment = rule
40
- if mass_assignment or force
41
- v = type.cast(v) if type
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
@@ -22,5 +22,8 @@ module Mongo::Model::Callbacks
22
22
  end
23
23
  end
24
24
  end
25
+
26
+ alias_method :before_validation, :before_validate
27
+ alias_method :after_validation, :after_validate
25
28
  end
26
29
  end
@@ -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.set attributes, options
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
- o = build attributes, options
29
- o.save
30
- o
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
- o = create attributes
35
- raise(Mongo::Error, "can't create #{attributes.inspect}!") if o.new_record?
36
- o
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 = {}
@@ -5,7 +5,10 @@ module Mongo::Model::Db
5
5
  self._db = if v.is_a? ::Proc
6
6
  v
7
7
  elsif v.is_a? ::Symbol
8
- -> {::Mongo::Model.connection.db v.to_s}
8
+ -> do
9
+ db_name = ::Mongo::Model.resolve_db_alias v
10
+ ::Mongo::Model.connection.db db_name
11
+ end
9
12
  else
10
13
  -> {v}
11
14
  end
@@ -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
@@ -1,8 +1,8 @@
1
1
  class Mongo::Model::Query < Object
2
- attr_reader :model, :selector, :options
2
+ attr_reader :model_class, :selector, :options
3
3
 
4
- def initialize model, selector = {}, options = {} *args
5
- @model, @selector, @options = model, selector, options
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 model == query.model
14
- self.class.new model, selector.merge(query.selector), options.merge(query.options)
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: #{model} #{@selector.inspect} #{@options.inspect}>"
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 ([model, selector, options] == [o.model, o.selector, o.options])
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
- model.build selector.merge(attributes), options
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
- model.create selector.merge(attributes), options
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
- model.create! selector.merge(attributes), options
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
- model.with_scope selector, options do
41
- result = model.send method, *args, &block
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
@@ -24,7 +24,7 @@ describe 'Associations' do
24
24
  attr_accessor :text, :post_id
25
25
 
26
26
  def == o
27
- [self.class, text, post_id] == [o.class, o.text, o.post_id]
27
+ self.class == o.class and [text, post_id] == [o.text, o.post_id]
28
28
  end
29
29
 
30
30
  timestamps!
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: