Class: Iceberg::FileObject

Inherits:
Object
  • Object
show all
Defined in:
lib/ibg/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ FileObject

Returns a new instance of FileObject.



48
49
50
51
52
53
# File 'lib/ibg/storage.rb', line 48

def initialize(key)
  download = SETTING['local']['download']
  @key = key
  @path = File.join(download, key)
  @content_length = File.exist?(@path) ? File.size(@path) : nil
end

Instance Attribute Details

#content_lengthObject (readonly)

Returns the value of attribute content_length.



55
56
57
# File 'lib/ibg/storage.rb', line 55

def content_length
  @content_length
end

#keyObject (readonly)

Returns the value of attribute key.



55
56
57
# File 'lib/ibg/storage.rb', line 55

def key
  @key
end

Instance Method Details

#closeObject



84
85
86
87
88
# File 'lib/ibg/storage.rb', line 84

def close
  return unless @fd
  @fd.close
  @fd = nil
end

#deleteObject



90
91
92
# File 'lib/ibg/storage.rb', line 90

def delete
  FileUtils.rm_f(@path)
end

#exists?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ibg/storage.rb', line 57

def exists?
  @content_length != nil
end

#read(size = nil, outbuf = '') ⇒ Object

TODO S3 bin/icebergsync.rb



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ibg/storage.rb', line 68

def read(size = nil, outbuf = '')
  if block_given?
    close
    File.open(@path, 'rb') do |fd|
      loop do
        data = fd.read(32 * 1024) # TODO
        break unless data
        yield(data)
      end
    end
  else
    @fd = File.open(@path, 'rb') unless @fd
    @fd.read(size, outbuf)
  end
end

#write(data) ⇒ Object

TODO S3 bin/icebergsync.rb



62
63
64
65
# File 'lib/ibg/storage.rb', line 62

def write(data)
  @fd = File.open(@path, 'wb') unless @fd
  @fd.write(data)
end