mongoid 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.5
1
+ 1.0.6
@@ -50,6 +50,7 @@ require "mongoid/errors"
50
50
  require "mongoid/field"
51
51
  require "mongoid/fields"
52
52
  require "mongoid/finders"
53
+ require "mongoid/identity"
53
54
  require "mongoid/indexes"
54
55
  require "mongoid/memoization"
55
56
  require "mongoid/named_scope"
@@ -41,7 +41,7 @@ module Mongoid #:nodoc:
41
41
  object = type ? type.instantiate : @klass.instantiate
42
42
  object.parentize(@parent, @association_name)
43
43
  object.write_attributes(attrs)
44
- object.generate_key
44
+ object.identify
45
45
  @documents << object
46
46
  object
47
47
  end
@@ -38,7 +38,7 @@ module Mongoid #:nodoc:
38
38
  if Mongoid.allow_dynamic_fields && !respond_to?("#{key}=")
39
39
  @attributes[key] = value
40
40
  else
41
- set_unless_blank(key, value)
41
+ send("#{key}=", value)
42
42
  end
43
43
  end
44
44
  end
@@ -136,10 +136,6 @@ module Mongoid #:nodoc:
136
136
  end
137
137
  end
138
138
 
139
- # Sets the attirbute value unless it is a blank string.
140
- def set_unless_blank(name, value)
141
- send("#{name}=", value) unless (value.is_a?(String) && value.blank?)
142
- end
143
139
  end
144
140
 
145
141
  module ClassMethods
@@ -84,7 +84,7 @@ module Mongoid #:nodoc:
84
84
  # end
85
85
  def key(*fields)
86
86
  self.primary_key = fields
87
- before_save :generate_key
87
+ before_save :identify
88
88
  end
89
89
 
90
90
  # Macro for setting the collection name to store in.
@@ -138,12 +138,8 @@ module Mongoid #:nodoc:
138
138
  end
139
139
 
140
140
  # Generate an id for this +Document+.
141
- def generate_key
142
- if primary_key
143
- @attributes[:_id] = extract_keys.join(" ").identify
144
- else
145
- @attributes[:_id] = Mongo::ObjectID.new.to_s unless id
146
- end
141
+ def identify
142
+ Identity.create(self)
147
143
  end
148
144
 
149
145
  # Instantiate a new +Document+, setting the Document's attributes if
@@ -165,12 +161,12 @@ module Mongoid #:nodoc:
165
161
  process(defaults.merge(attrs))
166
162
  @new_record = true if id.nil?
167
163
  document = yield self if block_given?
168
- generate_key; generate_type; document
164
+ identify
169
165
  end
170
166
 
171
167
  # Returns the class name plus its attributes.
172
168
  def inspect
173
- attrs = fields.map { |name, field| "#{name}: #{@attributes[name] || 'nil'}" } * ", "
169
+ attrs = fields.map { |name, field| "#{name}: #{@attributes[name].inspect}" } * ", "
174
170
  "#<#{self.class.name} _id: #{id}, #{attrs}>"
175
171
  end
176
172
 
@@ -276,16 +272,6 @@ module Mongoid #:nodoc:
276
272
  clear ? @attributes.delete(name) : @attributes.insert(name, child.attributes)
277
273
  notify
278
274
  end
279
-
280
- protected
281
- def generate_type
282
- @attributes[:_type] ||= self.class.name
283
- end
284
-
285
- def extract_keys
286
- primary_key.collect { |key| @attributes[key] }.reject { |val| val.nil? }
287
- end
288
-
289
275
  end
290
276
 
291
277
  end
@@ -21,7 +21,7 @@ module Mongoid #:nodoc:
21
21
  klass = type ? type : options.klass
22
22
  child = klass.instantiate(:_parent => parent)
23
23
  child.write_attributes(self.merge(:_type => klass.name))
24
- child.generate_key
24
+ child.identify
25
25
  child.assimilate(parent, options)
26
26
  end
27
27
  end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ class Identity #:nodoc:
4
+ class << self
5
+ # Create the identity for the +Document+.
6
+ #
7
+ # The id will be set in either in the form of a Mongo
8
+ # +ObjectID+ or a composite key set up by defining a key on the document.
9
+ #
10
+ # The _type will be set to the document's class name.
11
+ def create(doc)
12
+ identify(doc); type(doc); doc
13
+ end
14
+
15
+ protected
16
+ # Set the id for the document.
17
+ def identify(doc)
18
+ doc.id = compose(doc).join(" ").identify if doc.primary_key
19
+ doc.id = Mongo::ObjectID.new.to_s unless doc.id
20
+ end
21
+
22
+ # Set the _type field on the document.
23
+ def type(doc)
24
+ doc._type = doc.class.name
25
+ end
26
+
27
+ # Generates the composite key for a document.
28
+ def compose(doc)
29
+ doc.primary_key.collect { |key| doc.attributes[key] }.reject { |val| val.nil? }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "1.0.5"
8
+ s.version = "1.0.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
12
- s.date = %q{2010-01-13}
12
+ s.date = %q{2010-01-14}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
@@ -73,6 +73,7 @@ Gem::Specification.new do |s|
73
73
  "lib/mongoid/field.rb",
74
74
  "lib/mongoid/fields.rb",
75
75
  "lib/mongoid/finders.rb",
76
+ "lib/mongoid/identity.rb",
76
77
  "lib/mongoid/indexes.rb",
77
78
  "lib/mongoid/memoization.rb",
78
79
  "lib/mongoid/named_scope.rb",
@@ -136,6 +137,7 @@ Gem::Specification.new do |s|
136
137
  "spec/unit/mongoid/field_spec.rb",
137
138
  "spec/unit/mongoid/fields_spec.rb",
138
139
  "spec/unit/mongoid/finders_spec.rb",
140
+ "spec/unit/mongoid/identity_spec.rb",
139
141
  "spec/unit/mongoid/indexes_spec.rb",
140
142
  "spec/unit/mongoid/memoization_spec.rb",
141
143
  "spec/unit/mongoid/named_scope_spec.rb",
@@ -204,6 +206,7 @@ Gem::Specification.new do |s|
204
206
  "spec/unit/mongoid/field_spec.rb",
205
207
  "spec/unit/mongoid/fields_spec.rb",
206
208
  "spec/unit/mongoid/finders_spec.rb",
209
+ "spec/unit/mongoid/identity_spec.rb",
207
210
  "spec/unit/mongoid/indexes_spec.rb",
208
211
  "spec/unit/mongoid/memoization_spec.rb",
209
212
  "spec/unit/mongoid/named_scope_spec.rb",
@@ -60,7 +60,7 @@ class Person
60
60
  has_many_related :posts
61
61
 
62
62
  def score_with_rescoring=(score)
63
- @rescored = score + 20
63
+ @rescored = score.to_i + 20
64
64
  self.score_without_rescoring = score
65
65
  end
66
66
 
@@ -223,7 +223,7 @@ describe Mongoid::Attributes do
223
223
  attrs[:age].should == 30
224
224
  attrs[:terms].should == true
225
225
  attrs[:_id].should == "1"
226
- attrs[:score].should be_nil
226
+ attrs[:score].should == ""
227
227
  end
228
228
 
229
229
  end
@@ -4,18 +4,46 @@ describe Mongoid::Extensions::Integer::Conversions do
4
4
 
5
5
  describe "#set" do
6
6
 
7
- context "when string is a number" do
7
+ context "when the value is a number" do
8
8
 
9
- it "converts the string to an Integer" do
10
- Integer.set("32").should == 32
9
+ it "converts the number to an integer" do
10
+ Integer.set(3).should == 3
11
11
  end
12
12
 
13
13
  end
14
14
 
15
- context "when string is not a number" do
15
+ context "when the string is not a number" do
16
+
17
+ context "when the string is non numerical" do
18
+
19
+ it "returns the string" do
20
+ Integer.set("foo").should == "foo"
21
+ end
22
+
23
+ end
24
+
25
+ context "when the string is numerical" do
26
+
27
+ it "returns the integer value for the string" do
28
+ Integer.set("3").should == 3
29
+ end
30
+
31
+ end
32
+
33
+ context "when the string is empty" do
34
+
35
+ it "returns an empty string" do
36
+ Integer.set("").should be_blank
37
+ end
38
+
39
+ end
40
+
41
+ context "when the string is nil" do
42
+
43
+ it "returns nil" do
44
+ Integer.set(nil).should be_nil
45
+ end
16
46
 
17
- it "returns the string" do
18
- Integer.set("foo").should == "foo"
19
47
  end
20
48
 
21
49
  end
@@ -25,7 +53,7 @@ describe Mongoid::Extensions::Integer::Conversions do
25
53
  describe "#get" do
26
54
 
27
55
  it "returns the integer" do
28
- Integer.get(44).should == 44
56
+ Integer.get(3).should == 3
29
57
  end
30
58
 
31
59
  end
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Identity do
4
+
5
+ describe ".create" do
6
+
7
+ let(:name) { Name.new }
8
+
9
+ it "sets the document _type to the class name" do
10
+ Mongoid::Identity.create(name)
11
+ name._type.should == "Name"
12
+ end
13
+
14
+ it "returns the document" do
15
+ Mongoid::Identity.create(name).should == name
16
+ end
17
+
18
+ context "when the document has a primary key" do
19
+
20
+ before do
21
+ @address = Address.allocate
22
+ @address.instance_variable_set(:@attributes, { :street => "Market St"})
23
+ end
24
+
25
+ it "sets the id to the composite key" do
26
+ Mongoid::Identity.create(@address)
27
+ @address.id.should == "market-st"
28
+ end
29
+
30
+ end
31
+
32
+ context "when the document has no primary key" do
33
+
34
+ context "when the document has no id" do
35
+
36
+ before do
37
+ @person = Person.allocate
38
+ @person.instance_variable_set(:@attributes, {})
39
+ @object_id = stub(:to_s => "1")
40
+ Mongo::ObjectID.expects(:new).returns(@object_id)
41
+ end
42
+
43
+ it "sets the id to a mongo object id" do
44
+ Mongoid::Identity.create(@person)
45
+ @person.id.should == "1"
46
+ end
47
+
48
+ end
49
+
50
+ context "when the document has an id" do
51
+
52
+ before do
53
+ @person = Person.allocate
54
+ @person.instance_variable_set(:@attributes, { :_id => "5" })
55
+ end
56
+
57
+ it "returns the existing id" do
58
+ Mongoid::Identity.create(@person)
59
+ @person.id.should == "5"
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-13 00:00:00 -05:00
12
+ date: 2010-01-14 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -139,6 +139,7 @@ files:
139
139
  - lib/mongoid/field.rb
140
140
  - lib/mongoid/fields.rb
141
141
  - lib/mongoid/finders.rb
142
+ - lib/mongoid/identity.rb
142
143
  - lib/mongoid/indexes.rb
143
144
  - lib/mongoid/memoization.rb
144
145
  - lib/mongoid/named_scope.rb
@@ -202,6 +203,7 @@ files:
202
203
  - spec/unit/mongoid/field_spec.rb
203
204
  - spec/unit/mongoid/fields_spec.rb
204
205
  - spec/unit/mongoid/finders_spec.rb
206
+ - spec/unit/mongoid/identity_spec.rb
205
207
  - spec/unit/mongoid/indexes_spec.rb
206
208
  - spec/unit/mongoid/memoization_spec.rb
207
209
  - spec/unit/mongoid/named_scope_spec.rb
@@ -292,6 +294,7 @@ test_files:
292
294
  - spec/unit/mongoid/field_spec.rb
293
295
  - spec/unit/mongoid/fields_spec.rb
294
296
  - spec/unit/mongoid/finders_spec.rb
297
+ - spec/unit/mongoid/identity_spec.rb
295
298
  - spec/unit/mongoid/indexes_spec.rb
296
299
  - spec/unit/mongoid/memoization_spec.rb
297
300
  - spec/unit/mongoid/named_scope_spec.rb