Module: QB::Util
- Defined in:
- lib/qb/util.rb,
lib/qb/util/bundler.rb,
lib/qb/util/resource.rb,
lib/qb/util/decorators.rb,
lib/qb/util/docker_mixin.rb
Overview
Definitions
Defined Under Namespace
Modules: Bundler, Decorators, DockerMixin Classes: Resource
Class Method Summary collapse
-
.contract_path(path) ⇒ Pathname
do kind of the opposite of File.expand_path -- turn the home dir into ~ and the current dir into .
-
.find_up(filename, from = Pathname.pwd, raise_on_not_found: true) ⇒ Pathname?
Find
filename
infrom
or closest parent directory. -
.find_yaml_file!(dirs:, basename:, exts: QB::YAML_FILE_EXTS) ⇒ ::Pathname
Find a YAML file given it's basename and a list of directories to search, raising if it's not found.
-
.resolve(*segments) ⇒ Pathname
Absolute resolved path.
-
.words(string) ⇒ Array<String>
Split a string into 'words' for word-based matching.
- .words_slice?(full_string, input, &is_match) ⇒ Boolean
-
.words_start_with?(full_string, input) ⇒ Boolean
see if words from an input match words.
Class Method Details
.contract_path(path) ⇒ Pathname
do kind of the opposite of File.expand_path -- turn the home dir into ~ and the current dir into .
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/qb/util.rb', line 79 def self.contract_path path contracted = if path.start_with? Dir.pwd path.sub Dir.pwd, '.' elsif path.start_with? ENV['HOME'] path.sub ENV['HOME'], '~' else path end Pathname.new contracted end |
.find_up(filename, from = Pathname.pwd, raise_on_not_found: true) ⇒ Pathname?
Find filename
in from
or closest parent directory.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/qb/util.rb', line 118 def self.find_up filename, from = Pathname.pwd, raise_on_not_found: true path = from + filename return from if path.exist? parent = from.parent if from == parent if raise_on_not_found raise "not found in current or any parent directories: #{ filename }" else return nil end end return find_up filename, parent, raise_on_not_found: raise_on_not_found end |
.find_yaml_file!(dirs:, basename:, exts: QB::YAML_FILE_EXTS) ⇒ ::Pathname
Find a YAML file given it's basename and a list of directories to search, raising if it's not found.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/qb/util.rb', line 156 def self.find_yaml_file! dirs:, basename:, exts: QB::YAML_FILE_EXTS exts = exts.map { |ext| if ext.start_with?( '.' ) ext else '.' + ext end } checked = [] dirs.each do |dir| exts.each do |ext| path = ::Pathname.new File.join( dir, "#{ basename }#{ ext }" ) return path if path.file? checked << path end end file_pattern = \ "#{ basename }.{#{ exts.map { |ext| ext[ 1..-1 ] }.join ',' }" # If we haven't returned, we didn't find shit raise FSStateError.new "File `#{ file_pattern }` not found", checked: checked.map( &:to_s ) end |
.resolve(*segments) ⇒ Pathname
Returns absolute resolved path.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/qb/util.rb', line 57 def self.resolve *segments joined = Pathname.new '' ([Dir.pwd] + segments).reverse.each_with_index {|segment, index| joined = Pathname.new(segment).join joined return joined if joined.absolute? } # shouldn't ever happen raise "resolution failed: #{ segments.inspect }" end |
.words(string) ⇒ Array<String>
Split a string into 'words' for word-based matching
38 39 40 |
# File 'lib/qb/util.rb', line 38 def self.words string string.words end |
.words_slice?(full_string, input, &is_match) ⇒ Boolean
43 44 45 |
# File 'lib/qb/util.rb', line 43 def self.words_slice? full_string, input, &is_match full_string.words.slice? input.words, &is_match end |
.words_start_with?(full_string, input) ⇒ Boolean
see if words from an input match words
49 50 51 52 53 |
# File 'lib/qb/util.rb', line 49 def self.words_start_with? full_string, input words_slice? full_string, input do |full_string_word, input_word| full_string_word.start_with? input_word end end |