Class: Dragonfly::MopedDataStore

Inherits:
Object
  • Object
show all
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

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')
  meta = grid_file..each_with_object({}){ |(k,v), h| h[k.to_sym] = v }
  [ grid_file.read, meta ]

rescue RuntimeError
  raise DataNotFound
end

#sessionObject



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'
  meta = temp_object.meta
  meta = meta.merge(opts[:meta]) if opts[:meta]

  grid_file = session.bucket.open(BSON::ObjectId.new, "w+")
  grid_file.content_type = content_type
  grid_file. = meta
  grid_file.write(temp_object.data)
  grid_file._id
end