Class: LibPath::Path::Unix::ParsedPath

Inherits:
Object
  • Object
show all
Defined in:
lib/libpath/path/unix.rb

Overview

Class representing a parsed path (for UNIX)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, search_directory = nil, **options) ⇒ ParsedPath

Initialises an instance from the given path, optional search_directory and options

Signature

  • Parameters:

    • path (String) The path. May not be nil

    • search_directory (String) The search_directory, from which the relative attributes are calculated for the path. May be nil

    • options (Hash) Options

  • Options:

    • ????

  • Exceptions:

    • ArgumentError Raised if path is nil

Raises:

  • (::ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/libpath/path/unix.rb', line 85

def initialize path, search_directory = nil, **options

  raise ::ArgumentError, "path may not be nil or empty" if path.nil? || path.empty?

  _Diagnostics = ::LibPath::Diagnostics
  _Internal_Form = ::LibPath::Internal_::Unix::Form
  _Util      =  ::LibPath::Util::Unix
  _C       =  ::LibPath::Path::Unix::ParsedPath::ParsedPath_Constants

  _Diagnostics.check_options(options, known: _C::INIT_VALID_OPTIONS)


  abs_path     = _Util.make_path_absolute(path, make_canonical: true, **options.select { |k| _C::INIT_MPA_COMMON_OPTIONS.include?(k) })

  _, _, f2_dir, f3_basename, f4_stem, f5_ext, f6_dir_parts, _ = _Internal_Form.split_path(abs_path)

  @given_path      =  path
  @absolute_path   =  abs_path
  @compare_path    = _Util.make_compare_path abs_path
  @directory     = f2_dir
  @directory_path    = f2_dir
  @directory_parts = f6_dir_parts

  @file_full_name    = f3_basename
  @file_name_only    = f4_stem
  @file_extension    = f5_ext

  if search_directory

    drp_options             = options.select { |k| _C::INIT_DRP_COMMON_OPTIONS.include?(k) }

    search_directory          =  _Util.make_path_absolute(search_directory, make_canonical: true, **options.select { |k| _C::INIT_MPA_COMMON_OPTIONS.include?(k) })
    search_directory          =  _Internal_Form.append_trailing_slash search_directory

    @search_directory         = search_directory
    @search_relative_path       =  _Util.derive_relative_path(search_directory, abs_path, **drp_options)
    @search_relative_path       =  _Internal_Form.append_trailing_slash(@search_relative_path) if _Internal_Form.char_is_path_name_separator?(abs_path[-1])
    @search_relative_directory_path   =  _Internal_Form.append_trailing_slash _Util.derive_relative_path(search_directory, f2_dir, **drp_options)
    @search_relative_directory_parts  =  @search_relative_directory_path.split('/').map { |v| v + '/' }
  end
end

Instance Attribute Details

#absolute_pathObject (readonly)

(String) The full-path of the instance



130
131
132
# File 'lib/libpath/path/unix.rb', line 130

def absolute_path
  @absolute_path
end

#compare_pathObject (readonly)

(String) A normalised form of #path that can be used in comparisons



132
133
134
# File 'lib/libpath/path/unix.rb', line 132

def compare_path
  @compare_path
end

#directoryObject (readonly)

(String) The entry’s directory (excluding the #drive if on Windows)



134
135
136
# File 'lib/libpath/path/unix.rb', line 134

def directory
  @directory
end

#directory_partsObject (readonly)

([String]) An array of directory parts, where each part ends in the path name separator



139
140
141
# File 'lib/libpath/path/unix.rb', line 139

def directory_parts
  @directory_parts
end

#directory_pathObject (readonly) Also known as: dirname

(String) The full path of the entry’s directory



136
137
138
# File 'lib/libpath/path/unix.rb', line 136

def directory_path
  @directory_path
end

#file_extensionObject (readonly) Also known as: extension

(String) The entry’s file extension



147
148
149
# File 'lib/libpath/path/unix.rb', line 147

def file_extension
  @file_extension
end

#file_full_nameObject (readonly) Also known as: basename

(String) The entry’s file name (combination of #stem + #extension)



141
142
143
# File 'lib/libpath/path/unix.rb', line 141

def file_full_name
  @file_full_name
end

#file_name_onlyObject (readonly) Also known as: stem

(String) The entry’s file stem



144
145
146
# File 'lib/libpath/path/unix.rb', line 144

def file_name_only
  @file_name_only
end

#given_pathObject (readonly)

(String) The path given to initialise the instance



128
129
130
# File 'lib/libpath/path/unix.rb', line 128

def given_path
  @given_path
end

#search_directoryObject (readonly)

(String) The search directory if specified; nil otherwise



150
151
152
# File 'lib/libpath/path/unix.rb', line 150

def search_directory
  @search_directory
end

#search_relative_directory_partsObject (readonly)

([String]) The #directory_parts relative to #search_directory; nil if no search directory specified



156
157
158
# File 'lib/libpath/path/unix.rb', line 156

def search_relative_directory_parts
  @search_relative_directory_parts
end

#search_relative_directory_pathObject (readonly)

(String) The #directory_path relative to #search_directory; nil if no search directory specified



154
155
156
# File 'lib/libpath/path/unix.rb', line 154

def search_relative_directory_path
  @search_relative_directory_path
end

#search_relative_pathObject (readonly)

(String) The #path relative to #search_directory; nil if no search directory specified



152
153
154
# File 'lib/libpath/path/unix.rb', line 152

def search_relative_path
  @search_relative_path
end

Instance Method Details

#to_sObject

(String) String form of path



159
160
161
162
# File 'lib/libpath/path/unix.rb', line 159

def to_s

  absolute_path
end