Class: Pathname
- Defined in:
- lib/nrser/core_ext/pathname.rb,
lib/nrser/core_ext/pathname/subpath.rb
Overview
Definitions
Instance Method Summary collapse
- #_core_sub(*args, &block) ⇒ Object
-
#closest_dir ⇒ Pathname
The “closest” directory - which is ‘self` if the instance is a #directory?, otherwise it’s #dirname.
-
#find_up(rel_path, **kwds) ⇒ Object
See NRSER.find_up.
-
#find_up!(rel_path, **kwds) ⇒ Object
See NRSER.find_up!.
-
#start_with?(*prefixes) ⇒ Boolean
override to accept Pathname instances.
-
#sub(pattern, *args, &block) ⇒ Pathname
Our override of ‘#sub` to support Pathname instances as patterns.
-
#subpath?(other, root: nil, strict: false) ⇒ Boolean
Is ‘other` a subpath of `self`?.
-
#to_dot_rel(**kwds) ⇒ Pathname
Shortcut to call #to_rel with ‘dot_slash=true`.
-
#to_dot_rel_s(**kwds) ⇒ String
Shortcut to call #to_rel_s with ‘dot_slash=true`.
-
#to_pn ⇒ Pathname
Just returns ‘self`.
-
#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.
-
#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.
Instance Method Details
#_core_sub(*args, &block) ⇒ Object
58 |
# File 'lib/nrser/core_ext/pathname.rb', line 58 alias_method :_core_sub, :sub |
#closest_dir ⇒ Pathname
The “closest” directory - which is ‘self` if the instance is a #directory?, otherwise it’s #dirname.
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
See NRSER.find_up.
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
See NRSER.find_up!.
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.
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.
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:
-
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.
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. 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`.
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`.
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_pn ⇒ Pathname
Just returns ‘self`. Implemented to match the String#to_pn API so it can be called on an argument that may be either one.
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.
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.
157 158 159 |
# File 'lib/nrser/core_ext/pathname.rb', line 157 def to_rel_s **kwds to_rel( **kwds ).to_s end |