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)


115
116
117
118
119
120
121
# File 'lib/aruba/aruba_path.rb', line 115

def [](index)
  if RUBY_VERSION < '1.9'
    to_s.chars.to_a[index].to_a.join('')
  else
    to_s[index]
  end
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

#absolute?Boolean

Returns:

  • (Boolean)


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

def absolute?
  (%r{\A/} === to_s)
end

#blocksInteger

Report count of blocks allocated on disk

This reports the amount of blocks which are allocated by the path.

Returns:

  • (Integer)

    The count of blocks on disk



129
130
131
# File 'lib/aruba/aruba_path.rb', line 129

def blocks
  File::Stat.new(to_s).blocks
end

#depthInteger

How many parts has the file name

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

Returns:

  • (Integer)

    The count of file name parts



85
86
87
88
89
90
91
92
93
94
# File 'lib/aruba/aruba_path.rb', line 85

def depth
  if RUBY_VERSION < '1.9'
    items = []
    __getobj__.each_filename { |f| items << f }

    items.size
  else
    __getobj__.each_filename.to_a.size
  end
end

#end_with?(string) ⇒ Boolean

Path ends with string

Parameters:

  • string (String)

    The string to check

Returns:

  • (Boolean)


100
101
102
# File 'lib/aruba/aruba_path.rb', line 100

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

#popObject

Remove last component of path

Examples:

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


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

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

#relative?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/aruba/aruba_path.rb', line 62

def relative?
  !(%r{\A/} === to_s)
end

#start_with?(string) ⇒ Boolean

Path starts with string

Parameters:

  • string (String)

    The string to check

Returns:

  • (Boolean)


108
109
110
# File 'lib/aruba/aruba_path.rb', line 108

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

#to_aryObject



70
71
72
# File 'lib/aruba/aruba_path.rb', line 70

def to_ary
  to_a
end

#to_sObject



58
59
60
# File 'lib/aruba/aruba_path.rb', line 58

def to_s
  __getobj__.to_s
end