Module: Valise::Unpath

Extended by:
Unpath
Included in:
Item, PathMatcher, SearchRoot, Set, Set::Definer, Set::StemmedDefiner, Stack, Unpath, WorkspaceFinder
Defined in:
lib/valise/utils.rb

Defined Under Namespace

Classes: WorkspaceFinder

Instance Method Summary collapse

Instance Method Details

#clean_pathname(pathname) ⇒ Object



110
111
112
113
114
# File 'lib/valise/utils.rb', line 110

def clean_pathname(pathname)
  pathname.sub(/^~[^#{File::Separator}]*/) do |homedir|
    File::expand_path(homedir)
  end.cleanpath
end

#containing_workspace {|finder| ... } ⇒ Object

Yields:

  • (finder)


94
95
96
97
98
# File 'lib/valise/utils.rb', line 94

def containing_workspace
  finder = WorkspaceFinder.new
  yield finder if block_given?
  finder.find
end

#current_directoryObject



41
42
43
# File 'lib/valise/utils.rb', line 41

def current_directory
  make_pathname(Dir.pwd)
end

#file_from_backtrace(line) ⇒ Object



26
27
28
# File 'lib/valise/utils.rb', line 26

def file_from_backtrace(line)
  /(.*):\d+/.match(line)[1]
end

#from_here(rel_path, base_path = nil) ⇒ Object



30
31
32
33
# File 'lib/valise/utils.rb', line 30

def from_here(rel_path, base_path = nil)
  base_path ||= file_from_backtrace(caller[0])
  make_pathname(base_path) + make_pathname(rel_path)
end

#make_pathname(parts) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/valise/utils.rb', line 116

def make_pathname(parts)
  case parts
  when Pathname
    return clean_pathname(parts)
  when Array
    unless parts.any?{|part| not (String === part or Symbol === part)}
      parts = File::join(parts.map{|part| part.to_s})
    else
      raise ArgumentError, "path must be composed of strings or symbols"
    end
  when String
  when Symbol
    parts = parts.to_s
  when ::File
    parts = parts.path
  else
    raise ArgumentError, "path must be String, Array of Strings or File"
  end
  pathname = clean_pathname(Pathname.new(parts))
end

#starting_directoryObject Also known as: start_dir



35
36
37
38
# File 'lib/valise/utils.rb', line 35

def starting_directory
  make_pathname(ENV['PWD'] || Dir.pwd)
  #Otherwise symlinks won't behave as expected
end

#up_to(up_to = nil, base_path = nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/valise/utils.rb', line 45

def up_to(up_to=nil, base_path = nil)
  base_path ||= file_from_backtrace(caller[0])
  up_to ||= "lib"

  up_until(base_path, "Path with basename #{up_to.inspect}") do |path|
    path.basename.to_s == up_to
  end
end

#up_until(base_path = nil, description = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/valise/utils.rb', line 100

def up_until(base_path = nil, description=nil)
  base_path ||= file_from_backtrace(caller[0])
  make_pathname(base_path).ascend do |path|
    if yield(path)
      return path
    end
  end
  raise Errors::NoMatchingPath, "#{description || "Satisfactory path"} not found in #{base_path}"
end