Module: QB::Util

Defined in:
lib/qb/util.rb,
lib/qb/util/bundler.rb,
lib/qb/util/interop.rb,
lib/qb/util/resource.rb,
lib/qb/util/decorators.rb,
lib/qb/util/docker_mixin.rb

Defined Under Namespace

Modules: Bundler, Decorators, DockerMixin, Interop 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.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/qb/util.rb', line 55

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).



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/qb/util.rb', line 94

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

.resolve(*segments) ⇒ Pathname

Returns absolute resolved path.

Returns:

  • (Pathname)

    absolute resolved path.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/qb/util.rb', line 34

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.



15
16
17
# File 'lib/qb/util.rb', line 15

def self.words string
  string.words
end

.words_slice?(full_string, input, &is_match) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/qb/util.rb', line 20

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)


26
27
28
29
30
# File 'lib/qb/util.rb', line 26

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