Permalink
Browse files

Improve `wip index` patch matching. Clean-up a bit.

  • Loading branch information...
1 parent 5d3feb3 commit 4a827018c65b902b730212d5adda3fab25d35c75 @coreyti committed Oct 31, 2010
Showing with 34 additions and 23 deletions.
  1. +1 −5 lib/wip.rb
  2. +1 −0 lib/wip/cli.rb
  3. +26 −0 lib/wip/cli/base.rb
  4. +6 −18 lib/wip/cli/index.rb
View
@@ -22,15 +22,11 @@ def ui
end
def home
- @home ||= ENV['HOME'].strip
+ @home ||= Gem.user_home
end
def here
@here ||= Dir.pwd
end
-
- def index
- @index ||= Index.new
- end
end
end
View
@@ -7,6 +7,7 @@
module WIP
module CLI
+ autoload :Base, 'wip/cli/base'
autoload :Index, 'wip/cli/index'
class Shell < Thor
View
@@ -0,0 +1,26 @@
+module WIP
+ module CLI
+ class Base
+ attr_reader :path, :config
+
+ def initialize(path)
+ @path = path
+ @config = File.exist?(index_file) ? YAML.load_file(index_file) : {}
+ end
+
+ def get(options = {})
+ raise NotImplementedError
+ end
+
+ protected
+
+ def index_file
+ Pathname.new(File.join(WIP.home, ".wip/index"))
+ end
+
+ def context
+ File.expand_path(path.match(/^\//) ? path : File.join(WIP.here, path)).sub(/\/\.?$/, '')
+ end
+ end
+ end
+end
View
@@ -1,11 +1,8 @@
module WIP
module CLI
- class Index
- attr_reader :path
-
- def initialize(path)
- @path = path
- @initial = File.exist?(index_file) ? YAML.load_file(index_file) : {}
+ class Index < Base
+ def initialize(*)
+ super
end
def get(options = {})
@@ -20,13 +17,12 @@ def get(options = {})
private
def index
- home = ENV['HOME']
found = File.join(context, '**', '.wiprc')
found = Dir.glob(found).inject({}) do |memo, entry|
dirs = entry.split('/')[0...-1]
- location = dirs.join('/').sub(/\/\.?$/, '')
+ location = File.expand_path(dirs.join('/').sub(/\/\.?$/, ''))
- unless location == home
+ unless location == WIP.home
memo["#{dirs.last}"] = {
"path" => location
}
@@ -35,7 +31,7 @@ def index
memo
end
- works = (@initial || {}).merge(found)
+ works = (config || {}).merge(found)
cache = works.reject { |key, value| key == '.' }
File.open(index_file, 'w') do |out|
@@ -49,14 +45,6 @@ def index
}
end
end
-
- def index_file
- Pathname.new(File.join(Gem.user_home, ".wip/index"))
- end
-
- def context
- (path.match(/^\//) ? path : File.join(WIP.here, path)).sub(/\/\.?$/, '')
- end
end
end
end

0 comments on commit 4a82701

Please sign in to comment.