Class: Moped::GridFS::Bucket

Inherits:
Object
  • Object
show all
Includes:
Bucketable, Inspectable
Defined in:
lib/moped/gridfs/bucket.rb

Constant Summary collapse

DEFAULT_NAME =
'fs'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bucketable

#chunks_collection, #files_collection

Constructor Details

#initialize(session, name = DEFAULT_NAME) ⇒ Bucket

Returns a new instance of Bucket.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/moped/gridfs/bucket.rb', line 16

def initialize(session, name = DEFAULT_NAME)
  @name = name.to_s.strip
  @session = session

  raise ArgumentError.new("Bucket name cannot be empty") if @name.empty?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/moped/gridfs/bucket.rb', line 12

def name
  @name
end

#sessionObject (readonly)

Returns the value of attribute session.



12
13
14
# File 'lib/moped/gridfs/bucket.rb', line 12

def session
  @session
end

Instance Method Details

#delete(selector) ⇒ Object Also known as: remove



46
47
48
49
50
51
52
# File 'lib/moped/gridfs/bucket.rb', line 46

def delete(selector)
  document = files_collection.find(parse_selector(selector)).first
  return unless document
  chunks_collection.find(files_id: document['_id']).remove_all
  files_collection.find(_id: document['_id']).remove_all
  true
end

#dropObject



56
57
58
59
# File 'lib/moped/gridfs/bucket.rb', line 56

def drop
  [files_collection, chunks_collection].map(&:drop)
  @indexes_ensured = false
end

#ensure_indexesObject



29
30
31
32
33
34
35
36
# File 'lib/moped/gridfs/bucket.rb', line 29

def ensure_indexes
  @indexes_ensured ||= begin
    chunks_collection.indexes.create(files_id: 1, n: 1)
    # Optional index on filename
    files_collection.indexes.create({filename: 1}, {background: true})
    true
  end
end

#filesObject



38
39
40
# File 'lib/moped/gridfs/bucket.rb', line 38

def files
  Files.new(self)
end

#inspectObject



61
62
63
# File 'lib/moped/gridfs/bucket.rb', line 61

def inspect
  build_inspect_string(name: name)
end

#md5(file_id) ⇒ Object



42
43
44
# File 'lib/moped/gridfs/bucket.rb', line 42

def md5(file_id)
  session.command(filemd5: file_id, root: name)['md5']
end

#open(selector, mode) ⇒ Object



23
24
25
26
27
# File 'lib/moped/gridfs/bucket.rb', line 23

def open(selector, mode)
  ensure_indexes
  file = File.new(self, mode, selector)
  block_given? ? yield(file) : file
end