mongo_odm 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/CONTRIBUTORS ADDED
@@ -0,0 +1,4 @@
1
+ Steve Sloan
2
+
3
+ * Config class, railtie to load config from YAML file
4
+ * Timestamps module
data/lib/mongo_odm.rb CHANGED
@@ -10,33 +10,34 @@ module MongoODM
10
10
  extend ActiveSupport::Autoload
11
11
 
12
12
  autoload :VERSION
13
+ autoload :Config
13
14
  autoload :Criteria
14
15
  autoload :Cursor
15
16
  autoload :Collection
16
17
  autoload :Document
17
18
  autoload :Errors
18
-
19
+
19
20
  def self.connection
20
- Thread.current[:mongo_odm_connection] ||= Mongo::Connection.new(config[:host], config[:port], config)
21
+ Thread.current[:mongo_odm_connection] ||= config.connection
21
22
  end
22
-
23
+
23
24
  def self.connection=(value)
24
25
  Thread.current[:mongo_odm_connection] = value
25
26
  end
26
-
27
+
27
28
  def self.database
28
- connection.db(config[:database] || 'test')
29
+ Thread.current[:mongo_odm_database] ||= self.connection.db( config.database )
29
30
  end
30
-
31
+
31
32
  def self.config
32
- @_config ||= {}
33
+ @_config ||= Config.new
33
34
  end
34
-
35
+
35
36
  def self.config=(value)
36
37
  self.connection = nil
37
- @_config = value
38
+ @_config = value.is_a?(Config) ? value : Config.new(value)
38
39
  end
39
-
40
+
40
41
  def self.instanciate(value)
41
42
  return value if value.is_a?(MongoODM::Document)
42
43
  if value.is_a?(Hash)
@@ -46,4 +47,4 @@ module MongoODM
46
47
  value.class.type_cast(value.to_mongo)
47
48
  end
48
49
 
49
- end
50
+ end
@@ -0,0 +1,47 @@
1
+ require 'uri'
2
+
3
+ module MongoODM
4
+ class Config
5
+ attr_accessor :database, :host, :port, :username, :password
6
+ attr_accessor :logger, :pool_size
7
+
8
+ def initialize(opts = {})
9
+ from_hash opts
10
+ @database ||= 'test'
11
+ @host ||= 'localhost'
12
+ @port || 27017
13
+ @logger ||= Rails.logger if defined?(Rails)
14
+ @pool_size ||= 1
15
+ end
16
+
17
+ def uri=(uri)
18
+ uri = URI.parse(uri) unless URI === uri
19
+ @database = uri.path.to_s.sub('/', '')
20
+ @host, @port, @username, @password = uri.host, uri.port, uri.user, uri.password
21
+ end
22
+
23
+ def from_hash(opts)
24
+ opts = opts.dup.symbolize_keys!
25
+
26
+ if opts[:uri].present?
27
+ self.uri = opts[:uri]
28
+ else
29
+ @database, @host, @port, @username, @password = opts.values_at(:database, :host, :port, :username, :password)
30
+ @port &&= Integer(@port)
31
+ end
32
+
33
+ @logger ||= opts[:logger]
34
+ @pool_size ||= opts[:pool_size]
35
+ end
36
+
37
+ def connection
38
+ opts = { :logger => self.logger, :pool_size => self.pool_size }
39
+ Mongo::Connection.new(self.host, self.port, opts).tap do |conn|
40
+ if self.username || self.password
41
+ conn.add_auth(self.database, self.username, self.password)
42
+ conn.apply_saved_authentication
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -12,6 +12,7 @@ module MongoODM
12
12
  autoload :Fields
13
13
  autoload :Inspect
14
14
  autoload :Persistence
15
+ autoload :Timestamps
15
16
  autoload :Validations
16
17
  autoload :Equality
17
18
 
@@ -19,7 +19,7 @@ module MongoODM
19
19
  module InstanceMethods
20
20
  def initialize_with_callbacks(*args)
21
21
  initialize_without_callbacks(*args)
22
- _run_initialize_callbacks
22
+ run_callbacks(:initialize)
23
23
  self
24
24
  end
25
25
  end
@@ -0,0 +1,25 @@
1
+ module MongoODM
2
+ module Document
3
+ module Timestamps
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ field :created_at, Time
9
+ field :updated_at, Time
10
+
11
+ before_save :set_timestamps
12
+ end
13
+
14
+ module InstanceMethods
15
+ def set_timestamps
16
+ now = Time.now.utc
17
+ self.created_at ||= now if self.new_record?
18
+ self.updated_at = now
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+
@@ -34,9 +34,7 @@ module MongoODM
34
34
  # Runs all the specified validations and returns true if no errors were added otherwise false.
35
35
  def valid?
36
36
  errors.clear
37
-
38
- _run_validate_callbacks
39
-
37
+ run_callbacks(:validate)
40
38
  errors.empty?
41
39
  end
42
40
  end
@@ -0,0 +1,13 @@
1
+ module Rails #:nodoc:
2
+ module MongoODM #:nodoc:
3
+ class Railtie < Rails::Railtie #:nodoc:
4
+ initializer 'configure database' do
5
+ config_file = Rails.root.join 'config', 'mongo.yml'
6
+ if config_file.file?
7
+ config = YAML.load( ERB.new( config_file.read ).result )[Rails.env]
8
+ ::MongoODM.config = config if config.present?
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,6 +2,6 @@
2
2
  module MongoODM
3
3
  VERSION_MAJOR = "0"
4
4
  VERSION_MINOR = "2"
5
- VERSION_BUILD = "4"
5
+ VERSION_BUILD = "5"
6
6
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_BUILD}"
7
7
  end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Config do
5
+ before do
6
+ @config = { :host => 'localhost', :port => 9000 }
7
+ MongoODM.config = @config
8
+ end
9
+
10
+ after do
11
+ MongoODM.config = {}
12
+ end
13
+
14
+ it 'should set the configuration' do
15
+ MongoODM.config.host.should == @config[:host]
16
+ MongoODM.config.port.should == @config[:port]
17
+ end
18
+ end
@@ -2,5 +2,5 @@
2
2
  require "spec_helper"
3
3
 
4
4
  describe MongoODM::Document::Persistence do
5
-
5
+
6
6
  end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::Timestamps do
5
+
6
+ end
@@ -2,39 +2,13 @@
2
2
  require "spec_helper"
3
3
 
4
4
  describe MongoODM do
5
-
6
- describe "#config" do
7
-
8
- context "when no configuration was passed" do
9
- it "returns an empty hash" do
10
- MongoODM.config.should == {}
11
- end
12
-
13
- end
14
-
15
- context "when a custom configuration was passed" do
16
-
17
- before do
18
- MongoODM.config = {:host => "localhost", :port => 9000}
19
- end
20
-
21
- after do
22
- MongoODM.config = {}
23
- end
24
-
25
- it "should return the configuration as a hash" do
26
- MongoODM.config.should == {:host => "localhost", :port => 9000}
27
- end
28
-
29
- end
30
-
31
- end
32
-
33
5
  describe "#connection" do
34
6
 
35
7
  context "when connection does not exist" do
36
8
 
37
9
  before do
10
+ conn = Mongo::Connection.new( nil, nil, :connect => false )
11
+ Mongo::Connection.stub(:new).and_return(conn)
38
12
  MongoODM.connection = nil
39
13
  end
40
14
 
@@ -43,10 +17,11 @@ describe MongoODM do
43
17
  end
44
18
 
45
19
  end
46
-
47
- context "when connection already exists" do
48
20
 
21
+ context "when connection already exists" do
49
22
  before do
23
+ conn = Mongo::Connection.new( nil, nil, :connect => false )
24
+ Mongo::Connection.stub(:new).and_return(conn)
50
25
  @connection = MongoODM.connection
51
26
  end
52
27
 
@@ -61,7 +36,7 @@ describe MongoODM do
61
36
 
62
37
  it "returns a different Mongo::Connection when configuration changes" do
63
38
  MongoODM.config = {:port => 9000}
64
- @connection.should_not == MongoODM.connection
39
+ MongoODM.connection.should_not == @connection
65
40
  end
66
41
 
67
42
  end
@@ -70,7 +45,7 @@ describe MongoODM do
70
45
 
71
46
  describe "#connection=" do
72
47
  it "accepts a Mongo::Connection instance to set the connection" do
73
- connection = Mongo::Connection.new('localhost', 27017)
48
+ connection = Mongo::Connection.new('localhost', 27017, :connect => false)
74
49
  MongoODM.connection = connection
75
50
  connection.should == MongoODM.connection
76
51
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 5
9
+ version: 0.2.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Carlos Paramio
@@ -325,6 +325,7 @@ extra_rdoc_files:
325
325
  - LICENSE
326
326
  - README.rdoc
327
327
  files:
328
+ - CONTRIBUTORS
328
329
  - Gemfile
329
330
  - Gemfile.lock
330
331
  - LICENSE
@@ -332,6 +333,7 @@ files:
332
333
  - Rakefile
333
334
  - lib/mongo_odm.rb
334
335
  - lib/mongo_odm/collection.rb
336
+ - lib/mongo_odm/config.rb
335
337
  - lib/mongo_odm/core_ext/conversions.rb
336
338
  - lib/mongo_odm/criteria.rb
337
339
  - lib/mongo_odm/cursor.rb
@@ -348,14 +350,17 @@ files:
348
350
  - lib/mongo_odm/document/fields.rb
349
351
  - lib/mongo_odm/document/inspect.rb
350
352
  - lib/mongo_odm/document/persistence.rb
353
+ - lib/mongo_odm/document/timestamps.rb
351
354
  - lib/mongo_odm/document/validations.rb
352
355
  - lib/mongo_odm/errors.rb
356
+ - lib/mongo_odm/railtie.rb
353
357
  - lib/mongo_odm/version.rb
354
358
  - spec/models/00-blank_slate.rb
355
359
  - spec/models/01-shape.rb
356
360
  - spec/models/02-circle.rb
357
361
  - spec/models/03-big_red_circle.rb
358
362
  - spec/mongo_odm/collection_spec.rb
363
+ - spec/mongo_odm/config_spec.rb
359
364
  - spec/mongo_odm/core_ext/conversions_spec.rb
360
365
  - spec/mongo_odm/cursor_spec.rb
361
366
  - spec/mongo_odm/document/attribute_methods/dirty_spec.rb
@@ -368,6 +373,7 @@ files:
368
373
  - spec/mongo_odm/document/fields_spec.rb
369
374
  - spec/mongo_odm/document/inspect_spec.rb
370
375
  - spec/mongo_odm/document/persistence_spec.rb
376
+ - spec/mongo_odm/document/timestamps_spec.rb
371
377
  - spec/mongo_odm/document/validations_spec.rb
372
378
  - spec/mongo_odm/document_spec.rb
373
379
  - spec/mongo_odm/mongo_odm_spec.rb
@@ -410,6 +416,7 @@ test_files:
410
416
  - spec/models/02-circle.rb
411
417
  - spec/models/03-big_red_circle.rb
412
418
  - spec/mongo_odm/collection_spec.rb
419
+ - spec/mongo_odm/config_spec.rb
413
420
  - spec/mongo_odm/core_ext/conversions_spec.rb
414
421
  - spec/mongo_odm/cursor_spec.rb
415
422
  - spec/mongo_odm/document/attribute_methods/dirty_spec.rb
@@ -422,6 +429,7 @@ test_files:
422
429
  - spec/mongo_odm/document/fields_spec.rb
423
430
  - spec/mongo_odm/document/inspect_spec.rb
424
431
  - spec/mongo_odm/document/persistence_spec.rb
432
+ - spec/mongo_odm/document/timestamps_spec.rb
425
433
  - spec/mongo_odm/document/validations_spec.rb
426
434
  - spec/mongo_odm/document_spec.rb
427
435
  - spec/mongo_odm/mongo_odm_spec.rb