Module: Readlines::Info

Included in:
ReadDuc
Defined in:
lib/readlines/readlines/info.rb

Instance Method Summary collapse

Instance Method Details

#file_size_now(unit = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/readlines/readlines/info.rb', line 8

def file_size_now(unit = nil)
  size = ::File.size(@file_path)

  if unit.nil?
    size
  else
    case unit
    when :gigabytes, :g
      size / (1024.0 * 1024.0 * 1024.0)
    when :megabytes, :m
      size / (1024.0 * 1024.0)
    when :kilobytes, :k
      size / 1024.0
    when :bytes, :b
      size
    else
      raise Readlines::InvalidUnitError, "Invalid unit: #{unit}. Supported units are :gigabytes, :megabytes, :kilobytes, or :bytes."
    end
  end
end

#info_nowObject

Raises:

  • (Readlines::NotFoundError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/readlines/readlines/info.rb', line 29

def info_now
  raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)

  file_info = {
    created_at: ::File.birthtime(@file_path),
    modified_at: ::File.mtime(@file_path),
    path: ::File.absolute_path(@file_path),
    size: ::File.size(@file_path),
    name: ::File.basename(@file_path),
    type: ::File.extname(@file_path)
  }

  if block_given?
    yield file_info
  else
    file_info
  end
end