Class: S3Tree::Directory

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/s3_tree/directory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, path) ⇒ Directory

Returns a new instance of Directory.



6
7
8
9
# File 'lib/s3_tree/directory.rb', line 6

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/s3_tree/directory.rb', line 4

def path
  @path
end

Instance Method Details

#childrenObject



20
21
22
# File 'lib/s3_tree/directory.rb', line 20

def children
  subdirectories + files
end

#filesObject



30
31
32
33
34
35
# File 'lib/s3_tree/directory.rb', line 30

def files
  @files ||= list_objects['contents'].collect do |object|
    S3Tree::Files.new(@bucket, s3_object: object) unless object.key.ends_with?('/')
  end.compact
  (@files.sort_by &:last_modified).reverse!
end

#last_modifiedObject



41
42
43
# File 'lib/s3_tree/directory.rb', line 41

def last_modified
  (@bucket.objects(prefix: @path.gsub('+', ' ')).inject([]) { |date, s3_object| date.push(s3_object.last_modified) }).max
end

#nameObject



11
12
13
# File 'lib/s3_tree/directory.rb', line 11

def name
  URI.decode(path_pieces.last).gsub('+', ' ')
end

#parentObject



15
16
17
18
# File 'lib/s3_tree/directory.rb', line 15

def parent
  parent_path = path_pieces[0..-2].join('/')
  S3Tree::Directory.new(@bucket, parent_path) unless parent_path.blank?
end

#sizeObject



37
38
39
# File 'lib/s3_tree/directory.rb', line 37

def size
  number_to_human_size(@bucket.objects(prefix: @path.gsub('+', ' ')).inject(0) { |sum, s3_object| sum + s3_object.content_length })
end

#subdirectoriesObject



24
25
26
27
28
# File 'lib/s3_tree/directory.rb', line 24

def subdirectories
  @subdirectories ||= list_objects['common_prefixes'].collect do |prefix|
    S3Tree::Directory.new(@bucket, prefix.prefix)
  end
end