Class: Dragonfly::MopedDataStore
- Inherits:
-
Object
- Object
- Dragonfly::MopedDataStore
- Includes:
- Serializer
- Defined in:
- lib/dragonfly/moped_data_store.rb,
lib/dragonfly/moped_data_store/version.rb
Defined Under Namespace
Classes: DataNotFound
Constant Summary collapse
- OBJECT_ID =
BSON::ObjectId
- INVALID_OBJECT_ID =
Moped::Errors::InvalidObjectId
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#destroy(uid) ⇒ Object
———————————————————————.
-
#initialize(opts = {}) ⇒ MopedDataStore
constructor
.
-
#read(uid) ⇒ Object
———————————————————————.
- #session ⇒ Object
-
#write(temp_object, opts = {}) ⇒ Object
———————————————————————.
Constructor Details
#initialize(opts = {}) ⇒ MopedDataStore
22 23 24 25 26 |
# File 'lib/dragonfly/moped_data_store.rb', line 22 def initialize opts={} @host = opts[:host] || '127.0.0.1' @port = opts[:port] || '27017' @db = opts[:db] end |
Instance Method Details
#destroy(uid) ⇒ Object
61 62 63 |
# File 'lib/dragonfly/moped_data_store.rb', line 61 def destroy uid session.bucket.delete(uid) end |
#read(uid) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/dragonfly/moped_data_store.rb', line 50 def read uid grid_file = session.bucket.open(uid, 'r') = grid_file..each_with_object({}){ |(k,v), h| h[k.to_sym] = v } [ grid_file.read, ] rescue RuntimeError raise DataNotFound end |
#session ⇒ Object
28 29 30 31 32 |
# File 'lib/dragonfly/moped_data_store.rb', line 28 def session @session ||= Moped::Session.new(["#{@host}:#{@port}"]).tap do |session| session.use @db end end |
#write(temp_object, opts = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dragonfly/moped_data_store.rb', line 36 def write temp_object, opts={} content_type = opts[:content_type] || opts[:mime_type] || 'application/octet-stream' = temp_object. = .merge(opts[:meta]) if opts[:meta] grid_file = session.bucket.open(BSON::ObjectId.new, "w+") grid_file.content_type = content_type grid_file. = grid_file.write(temp_object.data) grid_file._id end |