mongo_mapper 0.13.0.beta1 → 0.13.0.beta2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 157572885e8b22a43d1585eb772fcbf1c95f6aa1
4
- data.tar.gz: 29cff77a4aba848e0ec1bde7a7fe2ba3f68b767a
3
+ metadata.gz: c064a0c4fc8a69754fd97b1d4d111b47533c5a9f
4
+ data.tar.gz: 93299ab0ef2f65d42de3a218315c4575ea6569a9
5
5
  SHA512:
6
- metadata.gz: cc75dcd206c828239a06f49cb9374497f9e0b425201921a5dcdcc169b5d1eba5a1005fbc2ad299ee908b311b046c42c2e5b97e339e668117f7a0c5ab815d98d4
7
- data.tar.gz: 003c0ac6bb4aaec67634ee5aedaa252f4d81f9a40d6e496ab108b5c408d22891495ec969e07d9bdc8b8315d25e0e309ac1ccdd0f29c2feac70257857a2c0e2a5
6
+ metadata.gz: 2552482a4e28cdc65871040dc6425ddf1a6030e9a1896ddb1b4bdd0442ca11c3fd0a01edf3838768fde2102376ddb957af2ce7ae563d07b92ab623ab71d6c2fc
7
+ data.tar.gz: e9a796fbf22d16874f9f862ff030e2aa29eb53fadc373cba5affdfd8fafee02fef0a04d33805520dac387fc6c4c9b935689cbf65ade1633152eacc4ffc085432
File without changes
@@ -13,7 +13,7 @@ module MongoMapper
13
13
  protected
14
14
  def find_target
15
15
  (@_values || []).map do |hash|
16
- child = polymorphic_class(hash).load(hash)
16
+ child = polymorphic_class(hash).load(hash, true)
17
17
  assign_references(child)
18
18
  child
19
19
  end
@@ -13,7 +13,7 @@ module MongoMapper
13
13
  private
14
14
  def find_target
15
15
  (@_values ||= []).map do |attrs|
16
- klass.load(attrs).tap do |child|
16
+ klass.load(attrs, true).tap do |child|
17
17
  assign_references(child)
18
18
  end
19
19
  end
@@ -7,11 +7,11 @@ module MongoMapper
7
7
  @value = value.respond_to?(:attributes) ? value.attributes.merge(association.type_key_name => value.class.name) : value
8
8
  reset
9
9
  end
10
-
10
+
11
11
  protected
12
12
  def find_target
13
13
  if @value
14
- child = polymorphic_class(@value).load(@value)
14
+ child = polymorphic_class(@value).load(@value, true)
15
15
  assign_references(child)
16
16
  child
17
17
  end
@@ -27,4 +27,4 @@ module MongoMapper
27
27
  end
28
28
  end
29
29
  end
30
- end
30
+ end
@@ -12,9 +12,9 @@ module MongoMapper
12
12
 
13
13
  def replace(doc)
14
14
  if doc.respond_to?(:attributes)
15
- @target = klass.load(doc.attributes)
15
+ @target = klass.load(doc.attributes, true)
16
16
  else
17
- @target = klass.load(doc)
17
+ @target = klass.load(doc, true)
18
18
  end
19
19
  assign_references(@target)
20
20
  loaded
@@ -29,7 +29,7 @@ module MongoMapper
29
29
 
30
30
  def find_target
31
31
  if @value
32
- klass.load(@value).tap do |child|
32
+ klass.load(@value, true).tap do |child|
33
33
  assign_references(child)
34
34
  end
35
35
  end
@@ -77,13 +77,13 @@ module MongoMapper
77
77
  end
78
78
 
79
79
  # load is overridden in identity map to ensure same objects are loaded
80
- def load(attrs)
80
+ def load(attrs, with_cast = false)
81
81
  return nil if attrs.nil?
82
82
  begin
83
83
  attrs['_type'] ? attrs['_type'].constantize : self
84
84
  rescue NameError
85
85
  self
86
- end.allocate.initialize_from_database(attrs)
86
+ end.allocate.initialize_from_database(attrs, with_cast)
87
87
  end
88
88
 
89
89
  private
@@ -106,19 +106,34 @@ module MongoMapper
106
106
  end
107
107
 
108
108
  def create_accessors_for(key)
109
- accessors_module.module_eval <<-end_eval
110
- def #{key.name}
111
- read_key(:#{key.name})
112
- end
109
+ accessors = ""
110
+ if key.read_accessor?
111
+ accessors << <<-end_eval
112
+ def #{key.name}
113
+ read_key(:#{key.name})
114
+ end
115
+
116
+ def #{key.name}_before_type_cast
117
+ read_key_before_type_cast(:#{key.name})
118
+ end
119
+ end_eval
120
+ end
113
121
 
114
- def #{key.name}=(value)
115
- write_key(:#{key.name}, value)
116
- end
122
+ if key.write_accessor?
123
+ accessors << <<-end_eval
124
+ def #{key.name}=(value)
125
+ write_key(:#{key.name}, value)
126
+ end
127
+ end_eval
128
+ end
117
129
 
118
- def #{key.name}?
119
- read_key(:#{key.name}).present?
120
- end
121
- end_eval
130
+ if key.predicate_accessor?
131
+ accessors << <<-end_eval
132
+ def #{key.name}?
133
+ read_key(:#{key.name}).present?
134
+ end
135
+ end_eval
136
+ end
122
137
 
123
138
  if block_given?
124
139
  accessors_module.module_eval do
@@ -126,6 +141,7 @@ module MongoMapper
126
141
  end
127
142
  end
128
143
 
144
+ accessors_module.module_eval accessors
129
145
  include accessors_module
130
146
  end
131
147
 
@@ -194,11 +210,11 @@ module MongoMapper
194
210
  yield self if block_given?
195
211
  end
196
212
 
197
- def initialize_from_database(attrs={})
213
+ def initialize_from_database(attrs={}, with_cast = false)
198
214
  @_new = false
199
215
  init_ivars
200
216
  initialize_default_values(attrs)
201
- load_from_database(attrs)
217
+ load_from_database(attrs, with_cast)
202
218
  self
203
219
  end
204
220
 
@@ -334,14 +350,14 @@ module MongoMapper
334
350
  @_dynamic_attributes = {} # Dumpable
335
351
  end
336
352
 
337
- def load_from_database(attrs)
353
+ def load_from_database(attrs, with_cast = false)
338
354
  return if attrs == nil || attrs.blank?
339
355
 
340
356
  attrs.each do |key, value|
341
357
  if !@__mm_keys.key?(key) && respond_to?(:"#{key}=")
342
358
  self.send(:"#{key}=", value)
343
359
  else
344
- internal_write_key key, value, false
360
+ internal_write_key key, value, with_cast
345
361
  end
346
362
  end
347
363
  end
@@ -3,7 +3,7 @@ module MongoMapper
3
3
  module Plugins
4
4
  module Keys
5
5
  class Key
6
- attr_accessor :name, :type, :options, :default, :ivar, :abbr
6
+ attr_accessor :name, :type, :options, :default, :ivar, :abbr, :accessors
7
7
 
8
8
  ID_STR = '_id'
9
9
 
@@ -15,6 +15,7 @@ module MongoMapper
15
15
  @embeddable = type.respond_to?(:embeddable?) ? type.embeddable? : false
16
16
  @is_id = @name == ID_STR
17
17
  @typecast = @options[:typecast]
18
+ @accessors = Array(@options[:accessors]).compact.map &:to_s
18
19
  @has_default = !!options.key?(:default)
19
20
  self.default = self.options[:default] if default?
20
21
 
@@ -28,7 +29,7 @@ module MongoMapper
28
29
  "Accessors called `#{@name}` have been created instead."
29
30
  end
30
31
  @ivar = :"@#{name}" if valid_ruby_name?
31
- validate_key_name! unless dynamic?
32
+ validate_key_name! unless dynamic? or !any_accessor?
32
33
  end
33
34
 
34
35
  def persisted_name
@@ -89,6 +90,24 @@ module MongoMapper
89
90
  !!@name.match(/\A[a-z_][a-z0-9_]*\z/i)
90
91
  end
91
92
 
93
+ def read_accessor?
94
+ any_accessor? ["read"]
95
+ end
96
+
97
+ def write_accessor?
98
+ any_accessor? ["write"]
99
+ end
100
+
101
+ def predicate_accessor?
102
+ any_accessor? ["present", "predicate", "boolean"]
103
+ end
104
+
105
+ def any_accessor?(arr_opt = [])
106
+ return true if @accessors.empty?
107
+ return false unless (@accessors & ["skip", "none"]).empty?
108
+ return !(@accessors & arr_opt).empty?
109
+ end
110
+
92
111
  private
93
112
  def typecast_class
94
113
  @typecast_class ||= options[:typecast].constantize
File without changes
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module MongoMapper
3
- Version = '0.13.0.beta1'
3
+ Version = '0.13.0.beta2'
4
4
  end
@@ -9,6 +9,7 @@ describe "EmbeddedDocument" do
9
9
 
10
10
  @pet_klass = EDoc('Pet') do
11
11
  key :name, String
12
+ key :flag, Boolean
12
13
  end
13
14
 
14
15
  @klass.many :pets, :class => @pet_klass
@@ -294,4 +295,22 @@ describe "EmbeddedDocument" do
294
295
  pets[0].name.should == "Sasha"
295
296
  end
296
297
  end
297
- end
298
+
299
+ context "Issue #536" do
300
+ it "should update attributes with string keys" do
301
+ person = @klass.create(:pets => [@pet_klass.new(:name => 'Rasmus', :flag => true)])
302
+ person.update_attributes!({"pets" => ["name" => "sparky", "flag" => "false"]})
303
+ person.reload
304
+ person.pets.first.name.should == "sparky"
305
+ person.pets.first.flag.should be_false
306
+ end
307
+
308
+ it "should update attributes with symbol keys" do
309
+ person = @klass.create(:pets => [@pet_klass.new(:name => 'Rasmus', :flag => true)])
310
+ person.update_attributes!({pets: [:name => "sparky", :flag => "false"]})
311
+ person.reload
312
+ person.pets.first.name.should == "sparky"
313
+ person.pets.first.flag.should be_false
314
+ end
315
+ end
316
+ end
@@ -52,6 +52,19 @@ describe "Keys" do
52
52
  expect { doc.new }.to_not raise_error
53
53
  end
54
54
 
55
+ it "should permit for key overrides" do
56
+ doc = Class.new do
57
+ include MongoMapper::Document
58
+ key :class, String, :accessors => :skip
59
+ end
60
+
61
+ doc.collection.insert('class' => 'String')
62
+ doc.all.first.tap do |d|
63
+ d.class.should == doc
64
+ d["class"].should == "String"
65
+ d.attributes["class"].should == "String"
66
+ end
67
+ end
55
68
 
56
69
  context "key segmenting" do
57
70
  let(:doc) {
@@ -43,7 +43,7 @@ describe "Scopes" do
43
43
 
44
44
  # Ordering is important here; where needs to happen before foo_id is defined
45
45
  # in order to produce the behavior we're testing against regression.
46
- scope :type, where(type: "bar")
46
+ scope :type, where(:type => "bar")
47
47
  key :foo_id, ObjectId
48
48
  }}
49
49
  before {
File without changes
@@ -70,6 +70,10 @@ describe "Key" do
70
70
  it "should permit bad names if __dynamic" do
71
71
  expect { Key.new(:"id.bar", :__dynamic => true) }.to_not raise_error
72
72
  end
73
+
74
+ it "should permit bad names if it is not to create accessors" do
75
+ expect { Key.new(:"id.bar", :accessors => :skip) }.to_not raise_error
76
+ end
73
77
  end
74
78
 
75
79
  context "A key" do
@@ -138,6 +142,40 @@ describe "Key" do
138
142
  end
139
143
  end
140
144
 
145
+ context "with the :attributes option" do
146
+ subject { @key }
147
+ before { @key = Key.new(:test, String, :accessors => accessor) }
148
+
149
+ context "with :read" do
150
+ let(:accessor) { :read }
151
+ its(:read_accessor?) { should be_true }
152
+ its(:write_accessor?) { should be_false }
153
+ its(:predicate_accessor?) { should be_false }
154
+ end
155
+
156
+ context "with :write" do
157
+ let(:accessor) { :write }
158
+ its(:read_accessor?) { should be_false }
159
+ its(:write_accessor?) { should be_true }
160
+ its(:predicate_accessor?) { should be_false }
161
+ end
162
+
163
+ context "with :predicate" do
164
+ let(:accessor) { :predicate }
165
+ its(:read_accessor?) { should be_false }
166
+ its(:write_accessor?) { should be_false }
167
+ its(:predicate_accessor?) { should be_true }
168
+ end
169
+
170
+ context "with an array of options" do
171
+ let(:accessor) { [:read, :write] }
172
+
173
+ its(:read_accessor?) { should be_true }
174
+ its(:write_accessor?) { should be_true }
175
+ its(:predicate_accessor?) { should be_false }
176
+ end
177
+ end
178
+
141
179
  context "setting a value with a custom type" do
142
180
  it "should correctly typecast" do
143
181
  key = Key.new(:foo, FooType)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0.beta1
4
+ version: 0.13.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-26 00:00:00.000000000 Z
11
+ date: 2013-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: 0.6.4
47
+ version: 0.6.5
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 0.6.4
54
+ version: 0.6.5
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mongo
57
57
  requirement: !ruby/object:Gem::Requirement