mongoid 0.5.3 → 0.5.4

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.5.4
@@ -33,6 +33,7 @@ require "active_support/core_ext"
33
33
  require "will_paginate/collection"
34
34
  require "mongo"
35
35
  require "mongoid/associations"
36
+ require "mongoid/attributes"
36
37
  require "mongoid/commands"
37
38
  require "mongoid/criteria"
38
39
  require "mongoid/extensions"
@@ -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
@@ -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 attirbutes if given.
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 = HashWithIndifferentAccess.new(attributes) if 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
 
@@ -3,7 +3,7 @@ module Mongoid #:nodoc:
3
3
  module Integer #:nodoc:
4
4
  module Conversions #:nodoc:
5
5
  def cast(value)
6
- value.to_i
6
+ value =~ /\d/ ? value.to_i : value
7
7
  end
8
8
  end
9
9
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.5.3"
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",
@@ -18,8 +18,8 @@ end
18
18
  class Person < Mongoid::Document
19
19
  include Mongoid::Timestamps
20
20
  field :title
21
- field :terms
22
- field :age
21
+ field :terms, :type => Boolean
22
+ field :age, :type => Integer
23
23
  has_many :addresses
24
24
  has_one :name
25
25
  end
@@ -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 = HashWithIndifferentAccess.new({ :test => "test" })
311
- end
312
-
313
- it "sets the attributes hash on the object" do
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 == @attributes
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
- it "converts the string to an Integer" do
7
- Integer.cast("32").should == 32
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.3
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