Class: Brandish::PathSet
- Inherits:
-
Object
- Object
- Brandish::PathSet
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/brandish/path_set.rb
Overview
A set of paths that can be searched for a certain file. This is used for looking for certain files, like sources or templates. This can allow Brandish to provide "default" files.
Despite its name, the order in which the paths are added to the pathset are important.
Constant Summary collapse
- DEFAULT_FIND_OPTIONS =
{ file: true, allow_absolute: false }.freeze
Instance Method Summary collapse
-
#<<(path) ⇒ self
Adds a path to this pathset.
-
#clear ⇒ void
Removes all of the paths from this pathset.
- #each(&block) ⇒ Object
-
#find(short, options = {}) ⇒ ::Pathname
Finds a file in the pathset.
-
#find_all(short, options = {}) {|path| ... } ⇒ void
Finds all versions of the short path name in the paths in the path sets.
-
#initialize ⇒ PathSet
constructor
Initialize the pathset.
- #replace(path) ⇒ self
-
#resolve(path, options = {}) ⇒ ::Pathname
"Resolves" a path.
Constructor Details
#initialize ⇒ PathSet
Initialize the pathset.
37 38 39 |
# File 'lib/brandish/path_set.rb', line 37 def initialize @paths = [] end |
Instance Method Details
#<<(path) ⇒ self
Adds a path to this pathset.
45 46 47 |
# File 'lib/brandish/path_set.rb', line 45 def <<(path) @paths << ::Pathname.new(path) end |
#clear ⇒ void
This method returns an undefined value.
Removes all of the paths from this pathset.
21 |
# File 'lib/brandish/path_set.rb', line 21 def_delegator :@paths, :clear |
#each ⇒ ::Enumerable<::Pathname> #each {|path| ... } ⇒ void
34 |
# File 'lib/brandish/path_set.rb', line 34 def_delegator :@paths, :each |
#find(short, options = {}) ⇒ ::Pathname
Finds a file in the pathset. If the file is returned, it is guarenteed
to exist. Relative paths, that do not expand out of the relative path,
are handled like you would expect - the path is appended to one of the
paths in the set, and checked for existance. However, for a file that
expands out of the relative path (e.g. ../a
or a/../../b
), the
behavior for expansion depends on the allow_absolute
option. If
allow_absolute
is false (default), the path is expanded against /
before it is joind with the paths in the set (e.g. ../a
, against
/path/to
, with allow_absolute=false
, expands to /path/to/a
).
If allow_absolute
is true, it directly expanding against the path
(e.g. ../a
, against /path/to
, with allow_absolute=true
, expands
to /path/a
). allow_absolute
should only be used if the given path
is trusted. Absolute paths are handled in a similar manner; if
allow_absolute=false
, for /a
against /path/to
, it expands to
/path/to/a
; with allow_absolute=true
, for /a
against /path/to
,
it expands to /a
.
119 120 121 122 123 |
# File 'lib/brandish/path_set.rb', line 119 def find(short, = {}) find_all(short, ).next rescue ::StopIteration fail NoFileError, "Could not find `#{short}' in any of the given paths (paths: #{@paths})" end |
#find_all(short, options = {}) {|path| ... } ⇒ void
This method returns an undefined value.
Finds all versions of the short path name in the paths in the path sets. If no block is given, it returns an enumerable; otherwise, if a block is given, it yields the joined path if it exists.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/brandish/path_set.rb', line 140 def find_all(short, = {}) return to_enum(:find_all, short, ) unless block_given? short = ::Pathname.new(short) = DEFAULT_FIND_OPTIONS.merge() @paths.reverse.each do |path| joined = path_join(path, short, ) yield joined if ([:file] && joined.file?) || joined.exist? end nil end |
#replace(path) ⇒ self
55 56 57 58 |
# File 'lib/brandish/path_set.rb', line 55 def replace(path) clear self << path end |
#resolve(path, options = {}) ⇒ ::Pathname
72 73 74 75 76 77 78 79 80 |
# File 'lib/brandish/path_set.rb', line 72 def resolve(path, = {}) = DEFAULT_FIND_OPTIONS.merge() path = ::Pathname.new(path) if [:allow_absolute] path.cleanpath else ::Pathname.new(path.("/").to_s.gsub(%r{\A(/|\\)}, "")) end end |