Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Using autoload to require the different session stores #8

Merged
merged 1 commit into from

2 participants

@arvida

I added autoload for requiring the session stores so you don't have to rescue on LoadError and not require files that is not used by the app using the gem.
I guess it comes down to personal coding style/preferences about using rescue LoadError vs autoload for handling stuff like this =) What you think?

@brianhempel
Owner

Thanks! Autoload is definitely cleaner, you're right. I'll take a look to make sure isn't some behavior that rescue gives us that autoload doesn't (e.g. robustness when the programmer loads stuff in a strange order, or sets the collection name before declaring the sessions store).

@arvida

Cool, let me know if you need any help.

@brianhempel brianhempel merged commit 66baa11 into brianhempel:master
@brianhempel
Owner

Merged. Thanks for the cleanup!

@arvida

Nice :+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 20, 2011
  1. Using autoload to require the different session stores

    Arvid Andersson authored
This page is out of date. Refresh to see the latest.
View
6 lib/mongo_session_store-rails3.rb
@@ -27,6 +27,6 @@ def self.collection_name
self.collection_name = "sessions"
end
-require 'mongo_session_store/mongo_mapper_store'
-require 'mongo_session_store/mongoid_store'
-require 'mongo_session_store/mongo_store'
+autoload :MongoMapperStore,'mongo_session_store/mongo_mapper_store'
+autoload :MongoidStore, 'mongo_session_store/mongoid_store'
+autoload :MongoStore, 'mongo_session_store/mongo_store'
View
38 lib/mongo_session_store/mongo_mapper_store.rb
@@ -1,26 +1,22 @@
-begin
- require 'mongo_mapper'
- require 'mongo_session_store/mongo_store_base'
+require 'mongo_mapper'
+require 'mongo_session_store/mongo_store_base'
- module ActionDispatch
- module Session
- class MongoMapperStore < MongoStoreBase
-
- class Session
- include MongoMapper::Document
- set_collection_name MongoSessionStore.collection_name
-
- key :_id, String
- key :data, Binary, :default => Marshal.dump({})
-
- timestamps!
- end
-
+module ActionDispatch
+ module Session
+ class MongoMapperStore < MongoStoreBase
+
+ class Session
+ include MongoMapper::Document
+ set_collection_name MongoSessionStore.collection_name
+
+ key :_id, String
+ key :data, Binary, :default => Marshal.dump({})
+
+ timestamps!
end
+
end
end
+end
- MongoMapperStore = ActionDispatch::Session::MongoMapperStore
-
-rescue LoadError
-end
+MongoMapperStore = ActionDispatch::Session::MongoMapperStore
View
44 lib/mongo_session_store/mongoid_store.rb
@@ -1,31 +1,27 @@
-begin
- require 'mongoid'
- require 'mongo_session_store/mongo_store_base'
+require 'mongoid'
+require 'mongo_session_store/mongo_store_base'
- module ActionDispatch
- module Session
- class MongoidStore < MongoStoreBase
-
- class Session
- include Mongoid::Document
- include Mongoid::Timestamps
- self.collection_name = MongoSessionStore.collection_name
-
- identity :type => String
+module ActionDispatch
+ module Session
+ class MongoidStore < MongoStoreBase
- field :data, :type => BSON::Binary, :default => BSON::Binary.new(Marshal.dump({}))
- end
+ class Session
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ self.collection_name = MongoSessionStore.collection_name
- private
- def pack(data)
- BSON::Binary.new(Marshal.dump(data))
- end
-
+ identity :type => String
+
+ field :data, :type => BSON::Binary, :default => BSON::Binary.new(Marshal.dump({}))
+ end
+
+ private
+ def pack(data)
+ BSON::Binary.new(Marshal.dump(data))
end
+
end
end
-
- MongoidStore = ActionDispatch::Session::MongoidStore
+end
-rescue LoadError
-end
+MongoidStore = ActionDispatch::Session::MongoidStore
Something went wrong with that request. Please try again.