lib/gitenv/context.rb in gitenv-0.0.5 vs lib/gitenv/context.rb in gitenv-0.1.0
- old
+ new
@@ -1,60 +1,49 @@
+require 'rbconfig'
module Gitenv
- module Context
- attr_accessor :from_paths, :to_paths, :absolute
+ class Context
+ attr_accessor :ignores
- def from path, &block
- (@from_paths ||= []) << path
- if block
- instance_eval &block
- @from_paths.pop
- end
- self
- end
+ def initialize config, options = {}
+ @config, @from, @ignores = config, options[:from], options[:ignores]
- def from_path
- @from_paths ? File.join(*([ @config.repository, @from_paths ].flatten)) : @config.repository
+ @to ||= File.expand_path('~')
+
+ @ignores = options[:ignores] || []
+ @ignores << '.DS_Store' if @ignores.empty? and RbConfig::CONFIG['host_os'] =~ /darwin/
end
- def to path, &block
- (@to_paths ||= []) << path
+ def from path = nil, &block
+ return @from if path.nil?
+
+ old = @from
+ @from = @from ? File.expand_path(path, @from) : File.expand_path(path)
+
if block
- instance_eval &block
- @to_paths.pop
+ @config.instance_eval &block
+ @from = old
end
+
self
end
- def to_abs path, &block
- previous = @to_paths
- @to_paths = [ path ]
- @absolute = true
+ def to path = nil, &block
+ return @to if path.nil?
+
+ old = @to
+ @to = @to ? File.expand_path(path, @to) : File.expand_path(path)
+
if block
- instance_eval &block
- @to_paths = previous
- @absolute = false
+ @config.instance_eval &block
+ @to = old
end
+
self
end
- def to_path
- @to_paths ? File.join(*(@absolute ? @to_paths : [ @config.home, @to_paths ]).flatten) : @config.home
- end
-
- def copy! config
- self.from_paths = config.from_paths ? config.from_paths.dup : []
- self.to_paths = config.to_paths ? config.to_paths.dup : []
- self.absolute = config.absolute
- @config = config
- end
-
- def home
- @config.home
- end
-
- def repository
- @config.repository
+ def dup
+ Context.new @config, from: @from, to: @to, ignores: @ignores.dup
end
end
end