Class: Phlexite::Cli::Glob
- Inherits:
-
Object
- Object
- Phlexite::Cli::Glob
- Defined in:
- lib/phlexite/cli/glob.rb
Constant Summary collapse
- NO_LEADING_DOT =
todo
'(?=[^\.])'- START_OF_FILENAME =
beginning of string or a slash
'(\A|\/)'- END_OF_STRING =
'\z'
Instance Method Summary collapse
-
#initialize(glob_string) ⇒ Glob
constructor
A new instance of Glob.
- #smoosh(chars) ⇒ Object
- #to_regexp ⇒ Object
- #to_regexp_string ⇒ Object
Constructor Details
#initialize(glob_string) ⇒ Glob
Returns a new instance of Glob.
10 11 12 |
# File 'lib/phlexite/cli/glob.rb', line 10 def initialize glob_string @glob_string = glob_string end |
Instance Method Details
#smoosh(chars) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/phlexite/cli/glob.rb', line 69 def smoosh chars out = [] until chars.empty? char = chars.shift if char == "*" && chars.first == "*" chars.shift chars.shift if chars.first == "/" out.push("**") else out.push(char) end end out end |
#to_regexp ⇒ Object
65 66 67 |
# File 'lib/phlexite/cli/glob.rb', line 65 def to_regexp Regexp.new(to_regexp_string) end |
#to_regexp_string ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/phlexite/cli/glob.rb', line 14 def to_regexp_string chars = @glob_string.chars chars = smoosh(chars) curlies = 0 escaping = false string = chars.map do |char| if escaping escaping = false char else case char when "**" "([^/]+/)*" when "*" ".*" when "?" "." when "." "\\." when "{" curlies += 1 "(" when "}" if curlies > 0 curlies -= 1 ")" else char end when "," if curlies > 0 "|" else char end when "\\" escaping = true "\\" else char end end end.join START_OF_FILENAME + string + END_OF_STRING end |