Class: Aruba::ArubaPath

Inherits:
Delegator
  • Object
show all
Defined in:
lib/aruba/aruba_path.rb

Overview

Pathname for aruba files and directories

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ArubaPath

Returns a new instance of ArubaPath.


12
13
14
15
16
17
18
# File 'lib/aruba/aruba_path.rb', line 12

def initialize(path)
  obj = [path.to_s].flatten

  super obj

  @delegate_sd_obj = obj
end

Instance Method Details

#[](index) ⇒ Object

Return string at index

Parameters:

  • index (Integer, Range)

88
89
90
# File 'lib/aruba/aruba_path.rb', line 88

def [](index)
  to_s[index]
end

#__getobj__Object

Get path


21
22
23
# File 'lib/aruba/aruba_path.rb', line 21

def __getobj__
  ::Pathname.new(::File.join(*@delegate_sd_obj))
end

#__setobj__(obj) ⇒ Object

Set path


26
27
28
# File 'lib/aruba/aruba_path.rb', line 26

def __setobj__(obj)
  @delegate_sd_obj = [obj.to_s].flatten
end

#depthInteger

How many parts has the file name

Examples:

path = ArubaPath.new('path/to/file.txt')
path.depth # => 3

Returns:

  • (Integer)

    The count of file name parts


65
66
67
# File 'lib/aruba/aruba_path.rb', line 65

def depth
  __getobj__.each_filename.to_a.size
end

#end_with?(string) ⇒ Boolean

Path ends with string

Parameters:

  • string (String)

    The string to check

Returns:

  • (Boolean)

73
74
75
# File 'lib/aruba/aruba_path.rb', line 73

def end_with?(string)
  to_s.end_with? string
end

#minimum_disk_space_usedInteger

Report minimum disk space used

This estimates the minimum bytes allocated by the path.

E.g. a 1-byte text file on a typical EXT-3 filesystem takes up 4096 bytes (could be more if it was truncated or less for sparse files).

Both `File::Stat` and the `stat()` system call will report 8 `blocks` (each “usually” represents 512 bytes). So 8 * 512 is exactly 4096 bytes.

(The “magic” 512 bye implied makes the value of “blocks” so confusing).

Currently Aruba allows you to set what's called the `physical_block_size`, which is a bit misleading - it's the “512” value. So if you somehow have a “filesystem unit size” of 8192 (instead of a typical 4KB), set the `physical_block_size` to 1024 (yes, divide by 8: 8192/8 = 1024).

Ideally, Aruba should provide e.g. `Aruba.config.fs_allocation_unit` (with 4096 as the default), so you wouldn't have to “divide by 8”.

(typical_fs_unit / typical_dev_bsize = 4096 / 512 = 8)

TODO: this is recommended over the above “blocks”

Returns:

  • (Integer)

    Total bytes allocate


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/aruba/aruba_path.rb', line 122

def minimum_disk_space_used
  # TODO: replace Aruba.config.physical_block_size
  # with something like Aruba.config.fs_allocation_unit
  dev_bsize = Aruba.config.physical_block_size

  stat = File::Stat.new(to_s)

  blocks = stat.blocks
  return (blocks * dev_bsize) if blocks

  typical_fs_unit = 4096
  typical_dev_bsize = 512 # google dev_bsize for more info

  block_multiplier = typical_fs_unit / typical_dev_bsize
  fs_unit_size = dev_bsize * block_multiplier
  fs_units = (stat.size + fs_unit_size - 1) / fs_unit_size
  fs_units = 1 if fs_units.zero?
  fs_units * fs_unit_size
end

#popObject

Remove last component of path

Examples:

path = ArubaPath.new 'path/to/dir.d'
path.pop
puts path # => path/to

52
53
54
# File 'lib/aruba/aruba_path.rb', line 52

def pop
  @delegate_sd_obj.pop
end

#push(p) ⇒ Object Also known as: <<

Add directory/file to path

Examples:

path = ArubaPath.new 'path/to/dir.d'
path << 'subdir.d
# or path.push 'subdir.d
puts path
# => path/to/dir.d/subdir.d

Parameters:

  • p (String)

    The path to be added


41
42
43
# File 'lib/aruba/aruba_path.rb', line 41

def push(p)
  @delegate_sd_obj << p
end

#start_with?(string) ⇒ Boolean

Path starts with string

Parameters:

  • string (String)

    The string to check

Returns:

  • (Boolean)

81
82
83
# File 'lib/aruba/aruba_path.rb', line 81

def start_with?(string)
  to_s.start_with? string
end