Class: Pathname

Inherits:
Object show all
Defined in:
lib/nrser/core_ext/pathname.rb,
lib/nrser/core_ext/pathname/subpath.rb

Overview

Definitions

Instance Method Summary collapse

Instance Method Details

#_core_sub(*args, &block) ⇒ Object



58
# File 'lib/nrser/core_ext/pathname.rb', line 58

alias_method :_core_sub, :sub

#closest_dirPathname

The “closest” directory - which is ‘self` if the instance is a #directory?, otherwise it’s #dirname.

Returns:



177
178
179
# File 'lib/nrser/core_ext/pathname.rb', line 177

def closest_dir
  directory? ? self : dirname
end

#find_up(rel_path, **kwds) ⇒ Object



105
106
107
# File 'lib/nrser/core_ext/pathname.rb', line 105

def find_up rel_path, **kwds
  NRSER.find_up rel_path, **kwds, from: self
end

#find_up!(rel_path, **kwds) ⇒ Object



112
113
114
# File 'lib/nrser/core_ext/pathname.rb', line 112

def find_up! rel_path, **kwds
  NRSER.find_up! rel_path, **kwds, from: self
end

#start_with?(*prefixes) ⇒ Boolean

override to accept Pathname instances.

Parameters:

  • prefixes (String)

    the prefixes to see if the Pathname starts with.

Returns:

  • (Boolean)

    true if the Pathname starts with any of the prefixes.



34
35
36
37
38
39
40
41
42
# File 'lib/nrser/core_ext/pathname.rb', line 34

def start_with? *prefixes
  to_s.start_with? *prefixes.map { |prefix|
    if Pathname === prefix
      prefix.to_s
    else
      prefix
    end
  }
end

#sub(pattern, *args, &block) ⇒ Pathname

Our override of ‘#sub` to support Pathname instances as patterns.

Just calls ‘#to_s` on `pattern` if it’s a Pathname before passing down to #_core_sub.

Parameters:

Returns:

  • (Pathname)

    A brand new Pathname boys and girls!



83
84
85
86
87
88
89
90
# File 'lib/nrser/core_ext/pathname.rb', line 83

def sub pattern, *args, &block
  case pattern
  when Pathname
    _core_sub pattern.to_s, *args, &block
  else
    _core_sub pattern, *args, &block
  end
end

#subpath?(other, root: nil, strict: false) ⇒ Boolean

Is ‘other` a subpath of `self`?

Which - it turns out - is a bit of a ricky question! Who knew?!

Maybe that’s why it’s not in the stand’ lib.

Here’s how we gonna go:

  1. Raise a NRSER::ValueError unless ‘self` is a #directory? and is #absolute?.

    I don’t think it make any sense to ask about subpaths of something that is not a directory, and things just get too messy unless it’s absolute since we need to expand ‘other` to make sure it doesn’t dot-dot-dig it’s way outta there.

2.

Parameters:

  • strict (Boolean) (defaults to: false)

    Decides behavior when ‘other` expands to the same directory as `self`:

    1. When ‘strict: false` (the default) a directory is considered a subpath of itself.

    2. When ‘strict: true`, a directory **is not** considered a subpath of itself.

Returns:

  • (Boolean)

    ‘true` if `other` is a path inside `self`.

    See the ‘strict` parameter for behavior when `other` expands to `self`.

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nrser/core_ext/pathname/subpath.rb', line 62

def subpath? other, root: nil, strict: false
  unless directory?
    raise NRSER::ValueError.new \
      "Receiver {Pathname}", self, "must be a {#directory} in order to test",
      "for subpaths",
      value: self
  end

  unless absolute?
    raise NRSER::ValueError.new \
      "Receiver {Pathname}", self, "must be {#absolute?} in order to test",
      "for subpaths",
      value: self
  end

  abs_other = other.to_pn.expand_path root

  # Deal with easy case first, when they're the same dir
  return !strict if self == abs_other

  # Ok, now see if they prefix match
  abs_other.start_with? self
end

#to_dot_rel(**kwds) ⇒ Pathname

Shortcut to call #to_rel with ‘dot_slash=true`.

Returns:



145
146
147
# File 'lib/nrser/core_ext/pathname.rb', line 145

def to_dot_rel **kwds
  to_rel **kwds, dot_slash: true
end

#to_dot_rel_s(**kwds) ⇒ String

Shortcut to call #to_rel_s with ‘dot_slash=true`.

Returns:



167
168
169
# File 'lib/nrser/core_ext/pathname.rb', line 167

def to_dot_rel_s **kwds
  to_rel_s( **kwds, dot_slash: true ).to_s
end

#to_pnPathname

Just returns ‘self`. Implemented to match the String#to_pn API so it can be called on an argument that may be either one.

Returns:



98
99
100
# File 'lib/nrser/core_ext/pathname.rb', line 98

def to_pn
  self
end

#to_rel(base_dir: Pathname.getwd, dot_slash: false) ⇒ Pathname

Shortcut to convert into a relative pathname, by default from the working directory, with option to ‘./` prefix.

Parameters:

  • base_dir (Pathname) (defaults to: Pathname.getwd)

    Directory you want the result to be relative to.

  • dot_slash (Boolean) (defaults to: false)

    When ‘true` will prepend `./` to the resulting path, unless it already starts with `../`.

Returns:



129
130
131
132
133
134
135
136
137
# File 'lib/nrser/core_ext/pathname.rb', line 129

def to_rel base_dir: Pathname.getwd, dot_slash: false
  rel = relative_path_from base_dir
  
  if dot_slash && !rel.start_with?( /\.\.?\// )
    File.join( '.', rel ).to_pn
  else
    rel
  end
end

#to_rel_s(**kwds) ⇒ String

Just a quick cut for ‘.to_rel.to_s`, since I seem to use that sort of form a lot.

Parameters:

  • base_dir (Pathname)

    Directory you want the result to be relative to.

  • dot_slash (Boolean)

    When ‘true` will prepend `./` to the resulting path, unless it already starts with `../`.

Returns:



157
158
159
# File 'lib/nrser/core_ext/pathname.rb', line 157

def to_rel_s **kwds
  to_rel( **kwds ).to_s
end