Sha256: e35141c97cd2ec269924d1541f39fadda719ce246111ae75d2448b23c2d0f6ab

Contents?: true

Size: 1.61 KB

Versions: 11

Compression:

Stored size: 1.61 KB

Contents

module QB
  module Util
    # split a string into 'words' for word-based matching
    def self.words string
      string.split(/[\W_-]+/).reject {|w| w.empty?}
    end # .words
    
    # see if words from an input match words 
    def self.words_start_with? full_string, input
      # QB.debug "does #{ input } match #{ full_string }?"
      
      input_words = words input
      full_string_words = words full_string
      
      full_string_words.each_with_index {|word, start_index|
        # compute the end index in full_string_words
        end_index = start_index + input_words.length - 1
        
        # short-circuit if can't match (more input words than full words left)
        if end_index >= full_string_words.length
          return false
        end
        
        # create the slice to test against
        slice = full_string_words[start_index..end_index]
        
        # see if every word in the slice starts with the corresponding word
        # in the input
        if slice.zip(input_words).all? {|full_word, input_word|
          full_word.start_with? input_word
        }
          # got a match!
          return true
        end
      }
      
      # no match
      false
    end # .match_words?
    
    # @return [Pathname] absolute resolved path.
    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
  end # Util
end # QB

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
qb-0.1.47 lib/qb/util.rb
qb-0.1.46 lib/qb/util.rb
qb-0.1.45 lib/qb/util.rb
qb-0.1.44 lib/qb/util.rb
qb-0.1.43 lib/qb/util.rb
qb-0.1.42 lib/qb/util.rb
qb-0.1.41 lib/qb/util.rb
qb-0.1.40 lib/qb/util.rb
qb-0.1.39 lib/qb/util.rb
qb-0.1.38 lib/qb/util.rb
qb-0.1.37 lib/qb/util.rb