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

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 .

Parameters:

  • path (Pathname | String)

    to contract.

Returns:

  • (Pathname)

    contracted path.



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.

Parameters:

  • filename (String)

    name of file to search for.

  • from (Pathname) (defaults to: Pathname.pwd)

    (Pathname.pwd) directory to start from.

  • raise_on_not_found: (Boolean) (defaults to: true)

    When true, a FSStateError will be raised if no file is found (default behavior).

    This is something of a legacy behavior - I think it would be better to have find_up return nil in that case and add a find_up! method that raises on not found. But I'm not going to do it right now.

Returns:

  • (Pathname)

    Pathname of found file.

  • (nil)

    If no file is found and the raise_on_not_found option is false.

Raises:

  • (QB::FSStateError)

    If file is not found in from or any of it's parent directories and the raise_on_not_found option is true (default behavior).



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.

Parameters:

  • dirs (::Array<::Pathname | ::String>)

    Directories to look in; searched in order.

  • basename (::String)

    Extension-less name of file.

  • exts (::Array<::String>) (defaults to: QB::YAML_FILE_EXTS)

    YAML extensions. Will be normalized to have a '.' prefix if they don't already.

Returns:

  • (::Pathname)

    Path to the first found file.

Raises:

  • (FSStateError<checked: ::Array<::String>>)

    If a file was not found across any of the dirs and exts.



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.

Returns:

  • (Pathname)

    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

Returns:

  • (Array<String>)

    Array of non-empty words in string.



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

Returns:

  • (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

Returns:

  • (Boolean)


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