Class: QB::Path

Inherits:
Pathname
  • Object
show all
Includes:
NRSER::Props::Immutable::InstanceVariables
Defined in:
lib/qb/path.rb

Overview

An extension of Pathname that implements the facts we want about paths as NRSER::Meta::Props.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Path #initialize(**values) ⇒ Path

Returns a new instance of Path.

Overloads:

  • #initialize(path) ⇒ Path

    Initialize in the same way as you would a Pathname. #cwd is set to the current directory (via Pathname#getwd) and the path argument is assigned to #raw.

    Parameters:

    • path (String | Pathname)

      Target path.

  • #initialize(**values) ⇒ Path

    Initialize by invoking NRSER::Meta::Props#initialize_props.

    The #raw value is passed up to Pathname#initialize.

    #cwd is accepted in values, allowing a re-instantiated object to "make sense" when the process' current directory may no longer be the one that data was constructed against.

    #cwd defaults to the current directory (via Pathname.getwd) if not provided.

Parameters:

  • **values

    see NRSER::Meta::Props#initialize_props



177
178
179
180
181
182
183
184
185
186
# File 'lib/qb/path.rb', line 177

def initialize arg
  case arg
  when Hash
    super arg[:raw]
    initialize_props cwd: Pathname.getwd, **arg
  else
    super arg
    initialize_props raw: arg, cwd: Pathname.getwd
  end
end

Instance Method Details

#cwd?Boolean

Returns true if self is equal to #cwd.

Returns:

  • (Boolean)

    true if self is equal to #cwd.



203
204
205
# File 'lib/qb/path.rb', line 203

def cwd?
  self == cwd
end

#expanded?Boolean

Returns true if self is equal to #expand_path.

Returns:

  • (Boolean)

    true if self is equal to #expand_path.



195
196
197
# File 'lib/qb/path.rb', line 195

def expanded?
  self == expand_path
end

#gemObject



284
285
286
287
288
289
290
# File 'lib/qb/path.rb', line 284

def gem
  unless instance_variable_defined? :@gem
    @gem = QB::Package::Gem.from_root_path path, repo: git
  end
  
  @gem
end

#gitQB::Repo::Git?

Repo::Git resource for the Git repo #path is in one, or nil if it isn't.

Returns:



275
276
277
278
279
280
281
# File 'lib/qb/path.rb', line 275

def git
  unless instance_variable_defined? :@git
    @git = QB::Repo::Git.from_path path
  end
  
  @git
end

#pathPathname Also known as: pathname

Returns A regular (non-QB::Path) Pathname version of self.

Returns:

  • (Pathname)

    A regular (non-QB::Path) Pathname version of self.



256
257
258
# File 'lib/qb/path.rb', line 256

def path
  Pathname.new self
end

#realpath?Boolean

Is self already it's real path?

Returns:



249
250
251
# File 'lib/qb/path.rb', line 249

def realpath?
  self == try_realpath
end

#relativeQB::Path?

Relative path from #cwd to self, if one exists.

Returns:

  • (QB::Path)

    If a relative path from #cwd to self exists.

  • (nil)

    If no relative path from #cwd to self exists. Can't recall exactly how this happens, but I guess it can...



217
218
219
220
221
222
223
# File 'lib/qb/path.rb', line 217

def relative
  begin
    relative_path_from cwd
  rescue ArgumentError => error
    nil
  end
end

#try_realpathnil, Pathname

Like Pathname#realpath but returns nil instead of raising if there isn't one.

Returns:

  • (nil)

    If there is no real path.

  • (Pathname)

    If there is a real path.



235
236
237
238
239
240
241
# File 'lib/qb/path.rb', line 235

def try_realpath
  begin
    realpath
  rescue SystemCallError => error
    nil
  end
end