Class: Stashify::Directory::Google::Cloud::Storage

Inherits:
Stashify::Directory show all
Defined in:
lib/stashify/directory/google/cloud/storage.rb

Overview

An implementation for interacting with Google Cloud Storage buckets as if they had directories with “/” as a path separator. In addition to a path, it also needs a Google::Cloud::Storage::Bucket object representing the bucket the file resides within.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket:, path:) ⇒ Storage

Returns a new instance of Storage.



19
20
21
22
# File 'lib/stashify/directory/google/cloud/storage.rb', line 19

def initialize(bucket:, path:)
  @bucket = bucket
  super(path: path)
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



17
18
19
# File 'lib/stashify/directory/google/cloud/storage.rb', line 17

def bucket
  @bucket
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
# File 'lib/stashify/directory/google/cloud/storage.rb', line 54

def ==(other)
  self.class == other.class && @bucket == other.bucket && path == other.path
end

#directory(name) ⇒ Object



42
43
44
# File 'lib/stashify/directory/google/cloud/storage.rb', line 42

def directory(name)
  Stashify::Directory::Google::Cloud::Storage.new(bucket: @bucket, path: path_of(name))
end

#directory?(name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/stashify/directory/google/cloud/storage.rb', line 37

def directory?(name)
  path = path_of(name)
  !@bucket.file(path) && !@bucket.files(prefix: path).empty?
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/stashify/directory/google/cloud/storage.rb', line 46

def exists?(name)
  @bucket.file(path_of(name))
end

#file(name) ⇒ Object



50
51
52
# File 'lib/stashify/directory/google/cloud/storage.rb', line 50

def file(name)
  Stashify::File::Google::Cloud::Storage.new(bucket: @bucket, path: path_of(name))
end

#filesObject



31
32
33
34
35
# File 'lib/stashify/directory/google/cloud/storage.rb', line 31

def files
  @bucket.files.map do |gcloud_file|
    find(::Regexp.last_match(1)) if gcloud_file.name =~ %r{^#{Regexp.escape(path)}/([^/]*)(/.*)?$}
  end.compact
end

#parentObject



24
25
26
27
28
29
# File 'lib/stashify/directory/google/cloud/storage.rb', line 24

def parent
  Stashify::Directory::Google::Cloud::Storage.new(
    bucket: @bucket,
    path: ::File.dirname(path),
  )
end