class Mongo::Grid::File
A representation of a file in the database.
@since 2.0.0
@deprecated Please use the 'stream' API on a FSBucket
instead.
Will be removed in driver version 3.0.
Attributes
@return [ Array<Chunk> ] chunks The file chunks.
@return [ File::Info
] info The file information.
Public Class Methods
Initialize the file.
@example Create the file.
Grid::File.new(data, :filename => 'test.txt')
@param [ IO, String, Array<BSON::Document> ] data The file object, file
contents or chunks.
@param [ BSON::Document, Hash ] options The info options.
@option options [ String ] :filename Required name of the file. @option options [ String ] :content_type The content type of the file.
Deprecated, please use the metadata document instead.
@option options [ String ] :metadata Optional file metadata. @option options [ Integer ] :chunk_size Override the default chunk
size.
@option opts [ Array<String> ] :aliases A list of aliases.
Deprecated, please use the metadata document instead.
@since 2.0.0
# File lib/mongo/grid/file.rb, line 73 def initialize(data, options = {}) options = options.merge(:length => data.size) unless options[:length] @info = Info.new(options) initialize_chunks!(data) end
Public Instance Methods
Check equality of files.
@example Check the equality of files.
file == other
@param [ Object ] other The object to check against.
@return [ true, false ] If the objects are equal.
@since 2.0.0
# File lib/mongo/grid/file.rb, line 49 def ==(other) return false unless other.is_a?(File) chunks == other.chunks && info == other.info end
Joins chunks into a string.
@return [ String ] The raw data for the file.
@since 2.0.0
# File lib/mongo/grid/file.rb, line 84 def data @data ||= Chunk.assemble(chunks) end
Gets a pretty inspection of the file.
@example Get the file inspection.
file.inspect
@return [ String ] The file inspection.
@since 2.0.0
# File lib/mongo/grid/file.rb, line 96 def inspect "#<Mongo::Grid::File:0x#{object_id} filename=#{filename}>" end
Private Instance Methods
@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.
# File lib/mongo/grid/file.rb, line 107 def initialize_chunks!(value) if value.is_a?(Array) @chunks = value.map{ |doc| Chunk.new(doc) } else @chunks = Chunk.split(value, info) end end