123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Grid::File Deprecated

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongo/grid/file.rb,
lib/mongo/grid/file/chunk.rb,
lib/mongo/grid/file/info.rb

Overview

Deprecated.

Please use the ‘stream’ API on a FSBucket instead. Will be removed in driver version 3.0.

A representation of a file in the database.

Since:

  • 2.0.0

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(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

[ GitHub ]

  
# File 'lib/mongo/grid/file.rb', line 76

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

Instance Attribute Details

#chunksArray<Chunk> (readonly)

Returns:

  • (Array<Chunk>)

    chunks The file chunks.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file.rb', line 37

attr_reader :chunks

#infoFile::Info (readonly)

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file.rb', line 40

attr_reader :info

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

[ GitHub ]

  
# File 'lib/mongo/grid/file.rb', line 52

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

[ GitHub ]

  
# File 'lib/mongo/grid/file.rb', line 87

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

#initialize_chunks!(value) ⇒ Array<Grid::File::Chunk> (private)

Note:

If we have provided an array of BSON::Documents to initialize with, we have an array of chunk documents and need to create the chunk objects and assemble the data. If we have an IO object, then it’s the original file data and we must split it into chunks and set the original data itself.

Parameters:

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

    The file object, file contents or chunk documents.

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/grid/file.rb', line 115

def initialize_chunks!(value)
  if value.is_a?(Array)
    @chunks = value.map{ |doc| Chunk.new(doc) }
  else
    @chunks = Chunk.split(value, info)
  end
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

[ GitHub ]

  
# File 'lib/mongo/grid/file.rb', line 99

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