Class: Backblaze::B2::File

Inherits:
Base
  • Object
show all
Defined in:
lib/backblaze/b2/file.rb

Instance Method Summary collapse

Methods inherited from Base

#get, #head, #post, #put

Methods included from Utils

#camelize, included, #underscore

Constructor Details

#initialize(file_name:, bucket_id:, versions: nil, **file_version_args) ⇒ File

Returns a new instance of File.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/backblaze/b2/file.rb', line 3

def initialize(file_name:, bucket_id:, versions: nil, **file_version_args)
  @file_name = file_name
  @bucket_id = bucket_id
  if versions
    @fetched_all = true
    @versions = versions
  else
    @fetched_all = false
    @versions = [FileVersion.new(file_version_args.merge(file_name: file_name))]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/backblaze/b2/file.rb', line 67

def method_missing(m, *args, &block)
  if latest.respond_to?(m)
    latest.send(m, *args, &block)
  else
    super
  end
end

Instance Method Details

#[](version) ⇒ Object



15
16
17
# File 'lib/backblaze/b2/file.rb', line 15

def [](version)
  versions[version]
end

#destroy!(thread_count: 4) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/backblaze/b2/file.rb', line 36

def destroy!(thread_count: 4)
  versions
  thread_count = @versions.length if thread_count > @versions.length || thread_count < 1
  lock = Mutex.new
  errors = []
  threads = []
  thread_count.times do
    threads << Thread.new do
      version = nil
      loop do
        lock.synchronize { version = @versions.pop }
        break if version.nil?
        begin
          version.destroy!
        rescue Backblaze::FileError => e
          lock.synchronize { errors << e }
        end
      end
    end
  end
  threads.map(&:join)
  @destroyed = true
  if errors.any?
    raise Backblaze::DestroyErrors.new(errors)
  end
end

#exists?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/backblaze/b2/file.rb', line 63

def exists?
  !@destroyed
end

#file_nameObject Also known as: name



19
20
21
# File 'lib/backblaze/b2/file.rb', line 19

def file_name
  @file_name
end

#latestObject



32
33
34
# File 'lib/backblaze/b2/file.rb', line 32

def latest
  @versions.first
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/backblaze/b2/file.rb', line 75

def respond_to?(m)
  if latest.respond_to?(m)
    true
  else
    super
  end
end

#versionsObject



24
25
26
27
28
29
30
# File 'lib/backblaze/b2/file.rb', line 24

def versions
  unless @fetched_all
    @versions = file_versions(bucket_id: @bucket_id, convert: true, limit: -1, double_check_server: false, file_name: file_name)
    @fetched_all = true
  end
  @versions
end