Class: RCS::Common::GridFS::ReadOnlyFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rcs-common/gridfs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, attributes) ⇒ ReadOnlyFile

Returns a new instance of ReadOnlyFile.



12
13
14
15
16
17
# File 'lib/rcs-common/gridfs.rb', line 12

def initialize(bucket, attributes)
  @attributes = attributes
  @bucket = bucket
  @last_chunk_num = (@attributes[:length].to_f / @attributes[:chunk_size]).ceil - 1
  rewind
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Raises:

  • (NoMethodError)


19
20
21
22
# File 'lib/rcs-common/gridfs.rb', line 19

def method_missing(name)
  raise NoMethodError.new(name.inspect) unless @attributes.has_key?(name)
  @attributes[name]
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/rcs-common/gridfs.rb', line 10

def attributes
  @attributes
end

#bucketObject (readonly)

Returns the value of attribute bucket.



10
11
12
# File 'lib/rcs-common/gridfs.rb', line 10

def bucket
  @bucket
end

#file_positionObject (readonly) Also known as: tell, position, pos

Returns the value of attribute file_position.



10
11
12
# File 'lib/rcs-common/gridfs.rb', line 10

def file_position
  @file_position
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rcs-common/gridfs.rb', line 57

def eof?
  @file_position >= @attributes[:length]
end

#file_lengthObject



65
66
67
# File 'lib/rcs-common/gridfs.rb', line 65

def file_length
  @attributes[:length]
end

#idObject



61
62
63
# File 'lib/rcs-common/gridfs.rb', line 61

def id
  @attributes[:_id]
end

#read(bytes_to_read = nil) ⇒ Object Also known as: content



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rcs-common/gridfs.rb', line 24

def read(bytes_to_read = nil)
  data = ''

  return data if @file_position >= @attributes[:length]
  return data if bytes_to_read and bytes_to_read <= 0

  if @current_chunk[:n]
    chunk_size = @attributes[:chunk_size]
    offset = @file_position % chunk_size
    offset = chunk_size if offset == 0
    data = @current_chunk[:data][offset..-1] || ''
  end

  if bytes_to_read.nil? or bytes_to_read > data.bytesize
    loop do
      break unless read_next_chunk
      data << @current_chunk[:data]

      break if bytes_to_read and bytes_to_read <= data.bytesize
    end
  end

  bytes_to_read = bytes_to_read ? bytes_to_read - 1 : -1
  data = data[0..bytes_to_read]
  @file_position += data.bytesize
  data
end

#rewindObject



52
53
54
55
# File 'lib/rcs-common/gridfs.rb', line 52

def rewind
  @current_chunk = {n: nil, data: nil}
  @file_position = 0
end