mongomodel 0.2.5 → 0.2.6

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/Gemfile CHANGED
@@ -1,11 +1,12 @@
1
1
  source "http://rubygems.org"
2
- # git "git://github.com/rails/rails.git"
2
+ git "git://github.com/rails/rails.git"
3
3
 
4
- gem "activemodel", "= 3.0.0.beta3"
5
- gem "activesupport", "= 3.0.0.beta3"
4
+ gem "activemodel", ">= 3.0.0.beta4"
5
+ gem "activesupport", ">= 3.0.0.beta4"
6
+ gem "tzinfo"
6
7
 
7
- gem "mongo", '>= 0.20.1'
8
- gem "bson", '>= 0.20.1'
9
- gem "bson_ext", '>= 0.20.1'
8
+ gem "mongo", '>= 1.0'
9
+ gem "bson", '>= 1.0'
10
+ gem "bson_ext", '>= 1.0'
10
11
 
11
12
  gem "rspec"
data/Rakefile CHANGED
@@ -46,10 +46,10 @@ begin
46
46
  gem.authors = ["Sam Pohlenz"]
47
47
  gem.version = MongoModel::VERSION
48
48
 
49
- gem.add_dependency('activesupport', '= 3.0.0.beta3')
50
- gem.add_dependency('activemodel', '= 3.0.0.beta3')
51
- gem.add_dependency('mongo', '>= 0.20.1')
52
- gem.add_dependency('bson', '>= 0.20.1')
49
+ gem.add_dependency('activesupport', '>= 3.0.0.beta4')
50
+ gem.add_dependency('activemodel', '>= 3.0.0.beta4')
51
+ gem.add_dependency('mongo', '>= 1.0')
52
+ gem.add_dependency('bson', '>= 1.0')
53
53
  gem.add_development_dependency('rspec', '>= 1.3.0')
54
54
  end
55
55
 
@@ -2,7 +2,7 @@ module MongoModel
2
2
  module Attributes
3
3
  module Dirty
4
4
  def []=(key, value)
5
- attr = key.to_s
5
+ attr = key.to_sym
6
6
 
7
7
  # The attribute already has an unsaved change.
8
8
  if changed.include?(attr)
@@ -17,7 +17,7 @@ module MongoModel
17
17
  end
18
18
 
19
19
  def changed
20
- @changed ||= {}
20
+ @changed ||= {}.with_indifferent_access
21
21
  end
22
22
 
23
23
  private
@@ -11,7 +11,7 @@ module MongoModel
11
11
 
12
12
  # Returns the attributes as they were before any changes were made to the document.
13
13
  def original_attributes
14
- attributes.merge(changed_attributes)
14
+ {}.merge(attributes).merge(changed_attributes)
15
15
  end
16
16
 
17
17
  protected
@@ -192,8 +192,6 @@ module MongoModel
192
192
  # defined as methods on the model, which are called last.
193
193
  module Callbacks
194
194
  extend ActiveSupport::Concern
195
-
196
- include ActiveSupport::Callbacks
197
195
 
198
196
  CALLBACKS = [
199
197
  :after_initialize, :after_find, :before_validation, :after_validation,
@@ -203,50 +201,19 @@ module MongoModel
203
201
  ]
204
202
 
205
203
  included do
206
- [:initialize, :valid?].each do |method|
207
- alias_method_chain method, :callbacks
208
- end
204
+ extend ActiveModel::Callbacks
209
205
 
210
- define_callbacks :initialize, :find, :save, :create, :update, :destroy,
211
- :validation, :terminator => "result == false", :scope => [:kind, :name]
206
+ define_model_callbacks :initialize, :find, :only => :after
207
+ define_model_callbacks :save, :create, :update, :destroy
208
+
209
+ define_callbacks :validation, :terminator => "result == false", :scope => [:kind, :name]
212
210
  end
213
211
 
214
212
  module ClassMethods
215
- def after_initialize(*args, &block)
216
- options = args.extract_options!
217
- options[:prepend] = true
218
- set_callback(:initialize, :after, *(args << options), &block)
219
- end
220
-
221
- def after_find(*args, &block)
222
- options = args.extract_options!
223
- options[:prepend] = true
224
- set_callback(:find, :after, *(args << options), &block)
225
- end
226
-
227
- [:save, :create, :update, :destroy].each do |callback|
228
- module_eval <<-CALLBACKS, __FILE__, __LINE__
229
- def before_#{callback}(*args, &block)
230
- set_callback(:#{callback}, :before, *args, &block)
231
- end
232
-
233
- def around_#{callback}(*args, &block)
234
- set_callback(:#{callback}, :around, *args, &block)
235
- end
236
-
237
- def after_#{callback}(*args, &block)
238
- options = args.extract_options!
239
- options[:prepend] = true
240
- options[:if] = Array(options[:if]) << "!halted && value != false"
241
- set_callback(:#{callback}, :after, *(args << options), &block)
242
- end
243
- CALLBACKS
244
- end
245
-
246
213
  def before_validation(*args, &block)
247
214
  options = args.extract_options!
248
215
  if options[:on]
249
- options[:if] = Array(options[:if])
216
+ options[:if] = Array.wrap(options[:if])
250
217
  options[:if] << "@_on_validate == :#{options[:on]}"
251
218
  end
252
219
  set_callback(:validation, :before, *(args << options), &block)
@@ -254,24 +221,22 @@ module MongoModel
254
221
 
255
222
  def after_validation(*args, &block)
256
223
  options = args.extract_options!
257
- options[:if] = Array(options[:if])
258
- options[:if] << "!halted"
224
+ options[:if] = Array.wrap(options[:if])
225
+ options[:if] << "!halted && value != false"
259
226
  options[:if] << "@_on_validate == :#{options[:on]}" if options[:on]
260
227
  options[:prepend] = true
261
228
  set_callback(:validation, :after, *(args << options), &block)
262
229
  end
263
230
  end
264
231
 
265
- def initialize_with_callbacks(*args, &block) #:nodoc:
266
- initialize_without_callbacks(*args, &block)
232
+ def initialize(*args, &block) #:nodoc:
233
+ super
267
234
  run_callbacks_with_embedded(:initialize)
268
235
  end
269
236
 
270
- def valid_with_callbacks? #:nodoc:
237
+ def valid?(*) #:nodoc:
271
238
  @_on_validate = new_record? ? :create : :update
272
- run_callbacks(:validation) do
273
- valid_without_callbacks?
274
- end
239
+ run_callbacks(:validation) { super }
275
240
  end
276
241
 
277
242
  def run_callbacks_with_embedded(kind, *args, &block)
@@ -4,8 +4,8 @@ module MongoModel
4
4
 
5
5
  include ActiveModel::Serializers::JSON
6
6
 
7
- def serializable_hash(options = nil)
8
- options ||= {}
7
+ def serializable_hash(given_options = nil)
8
+ options = given_options ? given_options.dup : {}
9
9
 
10
10
  options[:only] = Array.wrap(options[:only]).map { |n| n.to_s }
11
11
  options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }
@@ -16,10 +16,10 @@ module MongoModel
16
16
  end
17
17
  end
18
18
 
19
- def valid?
19
+ def valid?(context=nil)
20
20
  errors.clear
21
21
 
22
- @_on_validate = new_record? ? :create : :update
22
+ self.validation_context = new_record? ? :create : :update
23
23
  run_callbacks(:validate)
24
24
 
25
25
  errors.empty?
@@ -4,12 +4,7 @@ require 'active_support/core_ext/hash/except'
4
4
  module MongoModel
5
5
  class Configuration
6
6
  def initialize(options)
7
- case options
8
- when Hash
9
- @options = DEFAULTS.merge(options).stringify_keys
10
- when String
11
- super(parse(options))
12
- end
7
+ set_options!(options)
13
8
  end
14
9
 
15
10
  def host
@@ -48,6 +43,15 @@ module MongoModel
48
43
  @options.except('host', 'port', 'database', 'username', 'password').symbolize_keys
49
44
  end
50
45
 
46
+ def set_options!(options)
47
+ case options
48
+ when Hash
49
+ @options = DEFAULTS.merge(options).stringify_keys
50
+ when String
51
+ set_options!(parse(options))
52
+ end
53
+ end
54
+
51
55
  DEFAULTS = {
52
56
  'host' => 'localhost',
53
57
  'port' => 27017,
@@ -1,8 +1,8 @@
1
1
  module MongoModel
2
2
  module Types
3
3
  class Boolean < Object
4
- TRUE_VALUES = [ true, 'true', 't', 'TRUE', 'T', 'YES', 'y', '1', 1 ]
5
- FALSE_VALUES = [ false, 'false', 'f', 'FALSE', 'F', 'NO', 'n', '0', 0 ]
4
+ TRUE_VALUES = [ true, 'true', 't', 'TRUE', 'T', 'yes', 'YES', 'Y', 'y', '1', 1 ]
5
+ FALSE_VALUES = [ false, 'false', 'f', 'FALSE', 'F', 'no', 'NO', 'N', 'n', '0', 0 ]
6
6
 
7
7
  def cast(value)
8
8
  if true?(value)
@@ -2,7 +2,11 @@ module MongoModel
2
2
  module Types
3
3
  class Float < Object
4
4
  def cast(value)
5
- value.to_f if value.respond_to?(:to_f)
5
+ if value.nil?
6
+ nil
7
+ else
8
+ Kernel::Float(value) rescue nil
9
+ end
6
10
  end
7
11
 
8
12
  def boolean(value)
@@ -2,7 +2,15 @@ module MongoModel
2
2
  module Types
3
3
  class Integer < Object
4
4
  def cast(value)
5
- value.to_i if value.respond_to?(:to_i)
5
+ if value.nil?
6
+ nil
7
+ else
8
+ begin
9
+ Kernel::Integer(value)
10
+ rescue ArgumentError
11
+ Kernel::Float(value).to_i rescue nil
12
+ end
13
+ end
6
14
  end
7
15
 
8
16
  def boolean(value)
@@ -2,7 +2,7 @@ module MongoModel
2
2
  module Types
3
3
  class String < Object
4
4
  def cast(value)
5
- value.to_s if value.respond_to?(:to_s)
5
+ value.to_s if value && value.respond_to?(:to_s)
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module MongoModel
2
2
  module Types
3
3
  class Symbol < Object
4
4
  def cast(value)
5
- value.to_sym if value.respond_to?(:to_sym)
5
+ value.to_sym if value && value.respond_to?(:to_sym)
6
6
  end
7
7
  end
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongomodel}
8
- s.version = "0.2.5"
8
+ s.version = "0.2.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sam Pohlenz"]
12
- s.date = %q{2010-05-17}
12
+ s.date = %q{2010-06-09}
13
13
  s.default_executable = %q{console}
14
14
  s.description = %q{MongoModel is a MongoDB ORM for Ruby/Rails similar to ActiveRecord and DataMapper.}
15
15
  s.email = %q{sam@sampohlenz.com}
@@ -217,23 +217,23 @@ Gem::Specification.new do |s|
217
217
  s.specification_version = 3
218
218
 
219
219
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
220
- s.add_runtime_dependency(%q<activesupport>, ["= 3.0.0.beta3"])
221
- s.add_runtime_dependency(%q<activemodel>, ["= 3.0.0.beta3"])
222
- s.add_runtime_dependency(%q<mongo>, [">= 0.20.1"])
223
- s.add_runtime_dependency(%q<bson>, [">= 0.20.1"])
220
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
221
+ s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0.beta4"])
222
+ s.add_runtime_dependency(%q<mongo>, [">= 1.0"])
223
+ s.add_runtime_dependency(%q<bson>, [">= 1.0"])
224
224
  s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
225
225
  else
226
- s.add_dependency(%q<activesupport>, ["= 3.0.0.beta3"])
227
- s.add_dependency(%q<activemodel>, ["= 3.0.0.beta3"])
228
- s.add_dependency(%q<mongo>, [">= 0.20.1"])
229
- s.add_dependency(%q<bson>, [">= 0.20.1"])
226
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
227
+ s.add_dependency(%q<activemodel>, [">= 3.0.0.beta4"])
228
+ s.add_dependency(%q<mongo>, [">= 1.0"])
229
+ s.add_dependency(%q<bson>, [">= 1.0"])
230
230
  s.add_dependency(%q<rspec>, [">= 1.3.0"])
231
231
  end
232
232
  else
233
- s.add_dependency(%q<activesupport>, ["= 3.0.0.beta3"])
234
- s.add_dependency(%q<activemodel>, ["= 3.0.0.beta3"])
235
- s.add_dependency(%q<mongo>, [">= 0.20.1"])
236
- s.add_dependency(%q<bson>, [">= 0.20.1"])
233
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
234
+ s.add_dependency(%q<activemodel>, [">= 3.0.0.beta4"])
235
+ s.add_dependency(%q<mongo>, [">= 1.0"])
236
+ s.add_dependency(%q<bson>, [">= 1.0"])
237
237
  s.add_dependency(%q<rspec>, [">= 1.3.0"])
238
238
  end
239
239
  end
@@ -47,20 +47,25 @@ module MongoModel
47
47
  :string =>
48
48
  {
49
49
  "abc" => "abc",
50
- 123 => "123"
50
+ 123 => "123",
51
+ nil => nil
51
52
  },
52
53
  :integer =>
53
54
  {
54
55
  123 => 123,
55
56
  55.123 => 55,
56
57
  "999" => 999,
57
- "12.123" => 12
58
+ "12.123" => 12,
59
+ "foo" => nil,
60
+ nil => nil
58
61
  },
59
62
  :float =>
60
63
  {
61
64
  55.123 => 55.123,
62
65
  123 => 123.0,
63
- "12.123" => 12.123
66
+ "12.123" => 12.123,
67
+ "foo" => nil,
68
+ nil => nil
64
69
  },
65
70
  :boolean =>
66
71
  {
@@ -68,16 +73,33 @@ module MongoModel
68
73
  false => false,
69
74
  "true" => true,
70
75
  "false" => false,
76
+ "TRUE" => true,
77
+ "FALSE" => false,
78
+ "yes" => true,
79
+ "no" => false,
80
+ "YES" => true,
81
+ "NO" => false,
82
+ "t" => true,
83
+ "f" => false,
84
+ "T" => true,
85
+ "F" => false,
86
+ "Y" => true,
87
+ "N" => false,
88
+ "y" => true,
89
+ "n" => false,
71
90
  1 => true,
72
91
  0 => false,
73
92
  "1" => true,
74
93
  "0" => false,
75
- '' => nil
94
+ "foo" => nil,
95
+ '' => nil,
96
+ nil => nil
76
97
  },
77
98
  :symbol =>
78
99
  {
79
100
  :some_symbol => :some_symbol,
80
- "some_string" => :some_string
101
+ "some_string" => :some_string,
102
+ nil => nil
81
103
  },
82
104
  :hash =>
83
105
  {
@@ -92,14 +114,16 @@ module MongoModel
92
114
  Date.civil(2009, 11, 15) => Date.civil(2009, 11, 15),
93
115
  Time.local(2008, 12, 3, 0, 0, 0, 0) => Date.civil(2008, 12, 3),
94
116
  "2009/3/4" => Date.civil(2009, 3, 4),
95
- "Sat Jan 01 20:15:01 UTC 2000" => Date.civil(2000, 1, 1)
117
+ "Sat Jan 01 20:15:01 UTC 2000" => Date.civil(2000, 1, 1),
118
+ nil => nil
96
119
  },
97
120
  :time =>
98
121
  {
99
- Time.local(2008, 5, 14, 1, 2, 3, 4) => Time.local(2008, 5, 14, 1, 2, 3, 4, 0),
100
- Date.civil(2009, 11, 15) => Time.local(2009, 11, 15, 0, 0, 0, 0, 0),
101
- "Sat Jan 01 20:15:01 UTC 2000" => Time.utc(2000, 1, 1, 20, 15, 1, 0, 0),
102
- "2009/3/4" => Time.utc(2009, 3, 4, 0, 0, 0, 0, 0)
122
+ Time.local(2008, 5, 14, 1, 2, 3, 123456) => Time.local(2008, 5, 14, 1, 2, 3, 123000),
123
+ Date.civil(2009, 11, 15) => Time.local(2009, 11, 15, 0, 0, 0, 0),
124
+ "Sat Jan 01 20:15:01.123456 UTC 2000" => Time.utc(2000, 1, 1, 20, 15, 1, 123000),
125
+ "2009/3/4" => Time.utc(2009, 3, 4, 0, 0, 0, 0),
126
+ nil => nil
103
127
  }
104
128
  }
105
129
 
@@ -4,23 +4,50 @@ module MongoModel
4
4
  specs_for(Document, EmbeddedDocument) do
5
5
  define_class(:TestDocument, described_class) do
6
6
  property :foo, String
7
+ property :boolean, Boolean
7
8
  end
8
9
 
9
10
  subject { TestDocument.new }
10
11
 
11
12
  describe "#query_attribute" do
12
- it "should return true if the attribute is not blank" do
13
- subject.foo = 'set foo'
14
- subject.query_attribute(:foo).should be_true
15
- end
13
+ context "string attribute" do
14
+ it "should return true if the attribute is not blank" do
15
+ subject.foo = 'set foo'
16
+ subject.query_attribute(:foo).should be_true
17
+ subject.foo?.should be_true
18
+ end
19
+
20
+ it "should return false if the attribute is nil" do
21
+ subject.foo = nil
22
+ subject.query_attribute(:foo).should be_false
23
+ subject.foo?.should be_false
24
+ end
16
25
 
17
- it "should return false if the attribute is blank" do
18
- subject.foo = ''
19
- subject.query_attribute(:foo).should be_false
26
+ it "should return false if the attribute is blank" do
27
+ subject.foo = ''
28
+ subject.query_attribute(:foo).should be_false
29
+ subject.foo?.should be_false
30
+ end
20
31
  end
21
32
 
22
- it "should create a query method" do
23
- subject.foo?.should == subject.query_attribute(:foo)
33
+ context "boolean attribute" do
34
+ it "should return true if the attribute is true" do
35
+ subject.boolean = true
36
+ subject.query_attribute(:boolean).should be_true
37
+ subject.boolean?.should be_true
38
+ end
39
+
40
+ it "should return false if the attribute is nil" do
41
+ subject.boolean = nil
42
+ subject.query_attribute(:boolean).should be_false
43
+ subject.boolean?.should be_false
44
+ end
45
+
46
+ it "should return false if the attribute is false" do
47
+ subject.boolean = false
48
+ subject.query_attribute(:boolean).should be_false
49
+ subject.boolean?.should be_false
50
+ end
24
51
  end
25
52
  end
26
53
  end
@@ -1,6 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MongoModel do
4
+ after(:all) do
5
+ MongoModel.configuration = {}
6
+ end
7
+
4
8
  describe "setting a custom database configuration" do
5
9
  before(:each) do
6
10
  MongoModel.configuration = {
@@ -25,6 +29,18 @@ describe MongoModel do
25
29
  end
26
30
  end
27
31
 
32
+ describe "setting a custom database configuration as a URI string" do
33
+ before(:each) do
34
+ MongoModel.configuration = "mongodb://127.0.0.2:27019/mydb"
35
+ end
36
+
37
+ it "should should merge configuration with defaults" do
38
+ MongoModel.configuration.host.should == '127.0.0.2'
39
+ MongoModel.configuration.port.should == 27019
40
+ MongoModel.configuration.database.should == 'mydb'
41
+ end
42
+ end
43
+
28
44
  it "should have a logger accessor" do
29
45
  logger = mock('logger')
30
46
  MongoModel.logger = logger
@@ -25,6 +25,6 @@ Spec::Runner.configure do |config|
25
25
  end
26
26
 
27
27
  config.before(:each) do
28
- MongoModel.database.collections.each { |c| c.drop }
28
+ MongoModel.database.collections.each { |c| c.drop rescue c.remove }
29
29
  end
30
30
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Pohlenz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-05-17 00:00:00 +09:30
18
+ date: 2010-06-09 00:00:00 +09:30
19
19
  default_executable: console
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -24,15 +24,15 @@ dependencies:
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - "="
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: -1848230021
29
+ hash: -1848230024
30
30
  segments:
31
31
  - 3
32
32
  - 0
33
33
  - 0
34
- - beta3
35
- version: 3.0.0.beta3
34
+ - beta4
35
+ version: 3.0.0.beta4
36
36
  type: :runtime
37
37
  version_requirements: *id001
38
38
  - !ruby/object:Gem::Dependency
@@ -41,15 +41,15 @@ dependencies:
41
41
  requirement: &id002 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
- - - "="
44
+ - - ">="
45
45
  - !ruby/object:Gem::Version
46
- hash: -1848230021
46
+ hash: -1848230024
47
47
  segments:
48
48
  - 3
49
49
  - 0
50
50
  - 0
51
- - beta3
52
- version: 3.0.0.beta3
51
+ - beta4
52
+ version: 3.0.0.beta4
53
53
  type: :runtime
54
54
  version_requirements: *id002
55
55
  - !ruby/object:Gem::Dependency
@@ -60,12 +60,11 @@ dependencies:
60
60
  requirements:
61
61
  - - ">="
62
62
  - !ruby/object:Gem::Version
63
- hash: 77
63
+ hash: 15
64
64
  segments:
65
- - 0
66
- - 20
67
65
  - 1
68
- version: 0.20.1
66
+ - 0
67
+ version: "1.0"
69
68
  type: :runtime
70
69
  version_requirements: *id003
71
70
  - !ruby/object:Gem::Dependency
@@ -76,12 +75,11 @@ dependencies:
76
75
  requirements:
77
76
  - - ">="
78
77
  - !ruby/object:Gem::Version
79
- hash: 77
78
+ hash: 15
80
79
  segments:
81
- - 0
82
- - 20
83
80
  - 1
84
- version: 0.20.1
81
+ - 0
82
+ version: "1.0"
85
83
  type: :runtime
86
84
  version_requirements: *id004
87
85
  - !ruby/object:Gem::Dependency