mongoid 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid.rb +1 -0
- data/lib/mongoid/attributes.rb +13 -0
- data/lib/mongoid/document.rb +4 -5
- data/lib/mongoid/extensions/integer/conversions.rb +1 -1
- data/mongoid.gemspec +4 -1
- data/spec/spec_helper.rb +2 -2
- data/spec/unit/mongoid/attributes_spec.rb +36 -0
- data/spec/unit/mongoid/document_spec.rb +19 -6
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +15 -2
- metadata +4 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.4
|
data/lib/mongoid.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Attributes #:nodoc:
|
3
|
+
# Process the provided attributes casting them to their proper values if a
|
4
|
+
# field exists for them on the +Document+.
|
5
|
+
def process(fields, params)
|
6
|
+
attributes = HashWithIndifferentAccess.new(params)
|
7
|
+
attributes.each_pair do |key, value|
|
8
|
+
attributes[key] = fields[key].value(value) if fields[key]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
class Document
|
3
3
|
include ActiveSupport::Callbacks
|
4
|
-
include Commands, Observable, Validatable
|
4
|
+
include Attributes, Commands, Observable, Validatable
|
5
5
|
extend Associations
|
6
6
|
|
7
7
|
attr_accessor :association_name, :parent
|
@@ -184,11 +184,10 @@ module Mongoid #:nodoc:
|
|
184
184
|
@attributes[:_id]
|
185
185
|
end
|
186
186
|
|
187
|
-
# Instantiate a new Document, setting the Document's
|
187
|
+
# Instantiate a new Document, setting the Document's attributes if given.
|
188
188
|
# If no attributes are provided, they will be initialized with an empty Hash.
|
189
189
|
def initialize(attributes = {})
|
190
|
-
@attributes =
|
191
|
-
@attributes = HashWithIndifferentAccess.new unless attributes
|
190
|
+
@attributes = process(fields, attributes)
|
192
191
|
generate_key
|
193
192
|
end
|
194
193
|
|
@@ -226,7 +225,7 @@ module Mongoid #:nodoc:
|
|
226
225
|
|
227
226
|
# Write to the attributes hash.
|
228
227
|
def write_attribute(name, value)
|
229
|
-
@attributes[name] = value
|
228
|
+
@attributes[name] = fields[name].value(value)
|
230
229
|
notify
|
231
230
|
end
|
232
231
|
|
data/mongoid.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.4"
|
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"]
|
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/mongoid/associations/factory.rb",
|
29
29
|
"lib/mongoid/associations/has_many_association.rb",
|
30
30
|
"lib/mongoid/associations/has_one_association.rb",
|
31
|
+
"lib/mongoid/attributes.rb",
|
31
32
|
"lib/mongoid/commands.rb",
|
32
33
|
"lib/mongoid/commands/create.rb",
|
33
34
|
"lib/mongoid/commands/delete.rb",
|
@@ -64,6 +65,7 @@ Gem::Specification.new do |s|
|
|
64
65
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
65
66
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
66
67
|
"spec/unit/mongoid/associations_spec.rb",
|
68
|
+
"spec/unit/mongoid/attributes_spec.rb",
|
67
69
|
"spec/unit/mongoid/commands/create_spec.rb",
|
68
70
|
"spec/unit/mongoid/commands/delete_all_spec.rb",
|
69
71
|
"spec/unit/mongoid/commands/delete_spec.rb",
|
@@ -103,6 +105,7 @@ Gem::Specification.new do |s|
|
|
103
105
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
104
106
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
105
107
|
"spec/unit/mongoid/associations_spec.rb",
|
108
|
+
"spec/unit/mongoid/attributes_spec.rb",
|
106
109
|
"spec/unit/mongoid/commands/create_spec.rb",
|
107
110
|
"spec/unit/mongoid/commands/delete_all_spec.rb",
|
108
111
|
"spec/unit/mongoid/commands/delete_spec.rb",
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Attributes do
|
4
|
+
|
5
|
+
describe "#process" do
|
6
|
+
|
7
|
+
context "when supplied hash has values" do
|
8
|
+
|
9
|
+
before do
|
10
|
+
@attributes = {
|
11
|
+
:_id => "1",
|
12
|
+
:title => "value",
|
13
|
+
:age => "30",
|
14
|
+
:terms => "true",
|
15
|
+
:name => {
|
16
|
+
:_id => "2", :first_name => "Test", :last_name => "User"
|
17
|
+
},
|
18
|
+
:addresses => [
|
19
|
+
{ :_id => "3", :street => "First Street" },
|
20
|
+
{ :_id => "4", :street => "Second Street" }
|
21
|
+
]
|
22
|
+
}
|
23
|
+
@fields = Person.fields.values
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns a properly cast HashWithIndifferentAccess" do
|
27
|
+
attrs = Person.new(@attributes).attributes
|
28
|
+
attrs[:age].should == 30
|
29
|
+
attrs[:terms].should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -307,12 +307,25 @@ describe Mongoid::Document do
|
|
307
307
|
context "with attributes" do
|
308
308
|
|
309
309
|
before do
|
310
|
-
@attributes =
|
311
|
-
|
312
|
-
|
313
|
-
|
310
|
+
@attributes = {
|
311
|
+
:_id => "1",
|
312
|
+
:title => "value",
|
313
|
+
:age => "30",
|
314
|
+
:terms => "true",
|
315
|
+
:name => {
|
316
|
+
:_id => "2", :first_name => "Test", :last_name => "User"
|
317
|
+
},
|
318
|
+
:addresses => [
|
319
|
+
{ :_id => "3", :street => "First Street" },
|
320
|
+
{ :_id => "4", :street => "Second Street" }
|
321
|
+
]
|
322
|
+
}
|
323
|
+
end
|
324
|
+
|
325
|
+
it "sets the attributes hash on the object properly casted" do
|
314
326
|
person = Person.new(@attributes)
|
315
|
-
person.attributes.should ==
|
327
|
+
person.attributes[:age].should == 30
|
328
|
+
person.attributes[:terms].should be_true
|
316
329
|
end
|
317
330
|
|
318
331
|
end
|
@@ -468,7 +481,7 @@ describe Mongoid::Document do
|
|
468
481
|
context "when attribute does not exist" do
|
469
482
|
|
470
483
|
before do
|
471
|
-
Person.field :weight, :default => 100
|
484
|
+
Person.field :weight, :default => 100, :type => Integer
|
472
485
|
@person = Person.new
|
473
486
|
end
|
474
487
|
|
@@ -3,8 +3,21 @@ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
|
3
3
|
describe Mongoid::Extensions::Integer::Conversions do
|
4
4
|
|
5
5
|
describe "#cast" do
|
6
|
-
|
7
|
-
|
6
|
+
|
7
|
+
context "when string is a number" do
|
8
|
+
|
9
|
+
it "converts the string to an Integer" do
|
10
|
+
Integer.cast("32").should == 32
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when string is not a number" do
|
16
|
+
|
17
|
+
it "returns the string" do
|
18
|
+
Integer.cast("foo").should == "foo"
|
19
|
+
end
|
20
|
+
|
8
21
|
end
|
9
22
|
end
|
10
23
|
|
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: 0.5.
|
4
|
+
version: 0.5.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/mongoid/associations/factory.rb
|
75
75
|
- lib/mongoid/associations/has_many_association.rb
|
76
76
|
- lib/mongoid/associations/has_one_association.rb
|
77
|
+
- lib/mongoid/attributes.rb
|
77
78
|
- lib/mongoid/commands.rb
|
78
79
|
- lib/mongoid/commands/create.rb
|
79
80
|
- lib/mongoid/commands/delete.rb
|
@@ -110,6 +111,7 @@ files:
|
|
110
111
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
111
112
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
112
113
|
- spec/unit/mongoid/associations_spec.rb
|
114
|
+
- spec/unit/mongoid/attributes_spec.rb
|
113
115
|
- spec/unit/mongoid/commands/create_spec.rb
|
114
116
|
- spec/unit/mongoid/commands/delete_all_spec.rb
|
115
117
|
- spec/unit/mongoid/commands/delete_spec.rb
|
@@ -171,6 +173,7 @@ test_files:
|
|
171
173
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
172
174
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
173
175
|
- spec/unit/mongoid/associations_spec.rb
|
176
|
+
- spec/unit/mongoid/attributes_spec.rb
|
174
177
|
- spec/unit/mongoid/commands/create_spec.rb
|
175
178
|
- spec/unit/mongoid/commands/delete_all_spec.rb
|
176
179
|
- spec/unit/mongoid/commands/delete_spec.rb
|