Class: Pathname

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

Instance Method Summary collapse

Instance Method Details

#_original_subObject



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

alias_method :_original_sub, :sub

#find_up(rel_path, **kwds) ⇒ Object



60
61
62
# File 'lib/nrser/core_ext/pathname.rb', line 60

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

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



67
68
69
# File 'lib/nrser/core_ext/pathname.rb', line 67

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.



13
14
15
16
17
18
19
20
21
# File 'lib/nrser/core_ext/pathname.rb', line 13

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

#sub(pattern, replacement) ⇒ Pathname

override sub to support Pathname instances as patterns.

Parameters:

Returns:



38
39
40
41
42
43
44
45
# File 'lib/nrser/core_ext/pathname.rb', line 38

def sub pattern, replacement
  case pattern
  when Pathname
    _original_sub pattern.to_s, replacement
  else
    _original_sub pattern, replacement
  end
end

#to_dot_rel(**kwds) ⇒ Pathname

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

Parameters:

  • base_dir: (Pathname)

    Directory you want the result to be relative to.

Returns:



100
101
102
# File 'lib/nrser/core_ext/pathname.rb', line 100

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`.

Parameters:

  • base_dir: (Pathname)

    Directory you want the result to be relative to.

Returns:



122
123
124
# File 'lib/nrser/core_ext/pathname.rb', line 122

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:



53
54
55
# File 'lib/nrser/core_ext/pathname.rb', line 53

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:



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

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:



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

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