Class: Mongo::Grid::File

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo/grid/file.rb,
lib/mongo/grid/file/info.rb,
lib/mongo/grid/file/chunk.rb

Overview

A representation of a file in the database.

Since:

  • 2.0.0

Defined Under Namespace

Classes: Chunk, Info

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ File

Initialize the file.

Examples:

Create the file.

Grid::File.new(data, :filename => 'test.txt')

Parameters:

  • data (IO, String, Array<BSON::Document>)

    The file object, file contents or chunks.

  • options (BSON::Document, Hash) (defaults to: {})

    The info options.

  • opts (Hash)

    a customizable set of options

Options Hash (options):

  • :filename (String)

    Required name of the file.

  • :content_type (String)

    The content type of the file. Deprecated, please use the metadata document instead.

  • :metadata (String)

    Optional file metadata.

  • :chunk_size (Integer)

    Override the default chunk size.

Since:

  • 2.0.0



70
71
72
73
# File 'lib/mongo/grid/file.rb', line 70

def initialize(data, options = {})
  @info = Info.new(options.merge(:length => data.size))
  initialize_chunks!(data)
end

Instance Attribute Details

#chunksArray<Chunk> (readonly)

Returns chunks The file chunks.

Returns:

  • (Array<Chunk>)

    chunks The file chunks.

Since:

  • 2.0.0



31
32
33
# File 'lib/mongo/grid/file.rb', line 31

def chunks
  @chunks
end

#infoFile::Info (readonly)

Returns info The file information.

Returns:

Since:

  • 2.0.0



34
35
36
# File 'lib/mongo/grid/file.rb', line 34

def info
  @info
end

Instance Method Details

#==(other) ⇒ true, false

Check equality of files.

Examples:

Check the equality of files.

file == other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



46
47
48
49
# File 'lib/mongo/grid/file.rb', line 46

def ==(other)
  return false unless other.is_a?(File)
  chunks == other.chunks && info == other.info
end

#dataString

Joins chunks into a string.

Returns:

  • (String)

    The raw data for the file.

Since:

  • 2.0.0



80
81
82
# File 'lib/mongo/grid/file.rb', line 80

def data
  @data ||= Chunk.assemble(chunks)
end

#inspectString

Gets a pretty inspection of the file.

Examples:

Get the file inspection.

file.inspect

Returns:

  • (String)

    The file inspection.

Since:

  • 2.0.0



92
93
94
# File 'lib/mongo/grid/file.rb', line 92

def inspect
  "#<Mongo::Grid::File:0x#{object_id} filename=#{filename}>"
end