Module: Tap::Env::Minimap
- Included in:
- Tap::Env
- Defined in:
- lib/tap/env/minimap.rb
Overview
Minimap adds minimization and search methods to an array of paths. The minimized paths are refered to as ‘minipaths’ and represent the shortest basename that uniquely identifies a path within a collection of paths. File extensions and versions are removed when possible.
paths = %w{
path/to/file-0.1.0.txt
path/to/file-0.2.0.txt
path/to/another_file.txt
}
paths.extend Env::Minimap
paths.minimap
# => [
# ['file-0.1.0', 'path/to/file-0.1.0.txt'],
# ['file-0.2.0', 'path/to/file-0.2.0.txt'],
# ['another_file','path/to/another_file.txt']]
Minipaths can be used as keys to uniquely match a path. Longer, more complete paths may also be used (they add specificity, even though it is not needed). Likewise shorter paths may be used; the minimatch method returns the first matching path.
paths.minimatch('file') # => 'path/to/file-0.1.0.txt'
paths.minimatch('file-0.2.0') # => 'path/to/file-0.2.0.txt'
paths.minimatch('to/another_file') # => 'path/to/another_file.txt'
Usage
Minimap may extend any object responding to each, or be included in classes that implement each. Non-string entries are converted to paths by calling entry.path, if entry responds to path, or entry.to_s if it does not. Override the entry_to_path method to change this default behavior.
class ConstantMap < Array
include Env::Minimap
def entry_to_path(const)
const.underscore
end
end
constants = ConstantMap[Tap::Env::Minimap, Tap::Env]
constants.minimatch('env') # => Tap::Env
constants.minimatch('minimap') # => Tap::Env::Minimap
Instance Method Summary collapse
-
#minihash(reverse = false) ⇒ Object
Returns minimap as a hash of (minipath, value) pairs.
-
#minimap ⇒ Object
Determines the minipaths for each entry in self and returns a mapping of the minipaths to their corresponding entry.
-
#minimatch(key) ⇒ Object
Returns the first entry that mini-matches the input, or nil if no such entry exists.
Instance Method Details
#minihash(reverse = false) ⇒ Object
Returns minimap as a hash of (minipath, value) pairs.
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/tap/env/minimap.rb', line 100 def minihash(reverse=false) hash = {} minimap.each do |key, value| if reverse hash[value] = key else hash[key] = value end end hash end |
#minimap ⇒ Object
Determines the minipaths for each entry in self and returns a mapping of the minipaths to their corresponding entry.
paths = %w{
path/to/file-0.1.0.txt
path/to/file-0.2.0.txt
path/to/another_file.txt
}.extend Minimap
paths.minimap
# => [
# ['file-0.1.0', 'path/to/file-0.1.0.txt'],
# ['file-0.2.0', 'path/to/file-0.2.0.txt'],
# ['another_file','path/to/another_file.txt']]
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tap/env/minimap.rb', line 68 def minimap hash = {} map = [] each {|entry| map << (hash[entry_to_path(entry)] = [entry]) } minimize(hash.keys) do |key, mini_key| hash[key].unshift mini_key end map end |
#minimatch(key) ⇒ Object
Returns the first entry that mini-matches the input, or nil if no such entry exists.
paths = %w{
path/to/file-0.1.0.txt
path/to/file-0.2.0.txt
path/to/another_file.txt
}.extend Minimap
paths.minimatch('file-0.2.0') # => 'path/to/file-0.2.0.txt'
paths.minimatch('file-0.3.0') # => nil
91 92 93 94 95 96 97 |
# File 'lib/tap/env/minimap.rb', line 91 def minimatch(key) key = key.to_s each do |entry| return entry if minimal_match?(entry_to_path(entry), key) end nil end |