class FSSM::Path
Public Class Methods
new(path=nil, glob=nil, options={}, &block)
click to toggle source
# File lib/fssm/path.rb, line 2 def initialize(path=nil, glob=nil, options={}, &block) @options = options set_path(path || '.') set_glob(glob || '**/*') init_callbacks if block_given? if block.arity == 1 block.call(self) else self.instance_eval(&block) end end end
Public Instance Methods
create(*args, &block)
click to toggle source
# File lib/fssm/path.rb, line 30 def create(*args, &block) callback_action(:create, (block_given? ? block : args)) end
delete(*args, &block)
click to toggle source
# File lib/fssm/path.rb, line 38 def delete(*args, &block) callback_action(:delete, (block_given? ? block : args)) end
glob(value=nil)
click to toggle source
# File lib/fssm/path.rb, line 25 def glob(value=nil) return @glob if value.nil? set_glob(value) end
to_pathname()
click to toggle source
# File lib/fssm/path.rb, line 21 def to_pathname @path end
to_s()
click to toggle source
# File lib/fssm/path.rb, line 17 def to_s @path.to_s end
update(*args, &block)
click to toggle source
# File lib/fssm/path.rb, line 34 def update(*args, &block) callback_action(:update, (block_given? ? block : args)) end
Private Instance Methods
callback_action(type, args=[])
click to toggle source
# File lib/fssm/path.rb, line 49 def callback_action(type, args=[]) if args.is_a?(Proc) set_callback(type, args) elsif args.empty? get_callback(type) else run_callback(type, args) end end
get_callback(type)
click to toggle source
# File lib/fssm/path.rb, line 64 def get_callback(type) @callbacks[type] end
init_callbacks()
click to toggle source
# File lib/fssm/path.rb, line 44 def init_callbacks do_nothing = lambda { |base, relative|} @callbacks = Hash.new(do_nothing) end
run_callback(type, args)
click to toggle source
# File lib/fssm/path.rb, line 68 def run_callback(type, args) callback_args = split_path(args[0]) callback_args << args[1] if @options[:directories] begin @callbacks[type].call(*callback_args) rescue Exception => e raise FSSM::CallbackError, "#{type} - #{args[0]}: #{e.message}", e.backtrace end end
set_callback(type, arg)
click to toggle source
# File lib/fssm/path.rb, line 59 def set_callback(type, arg) raise ArgumentError, "Proc expected" unless arg.is_a?(Proc) @callbacks[type] = arg end
set_glob(glob)
click to toggle source
# File lib/fssm/path.rb, line 91 def set_glob(glob) @glob = glob.is_a?(Array) ? glob : [glob] end
set_path(path)
click to toggle source
# File lib/fssm/path.rb, line 84 def set_path(path) @path = FSSM::Pathname.for(path).expand_path raise FSSM::FileNotFoundError, "No such file or directory - #{@path}" unless @path.exist? raise FSSM::FileNotRealError, "Path is virtual - #{@path}" if @path.is_virtual? @path = @path.realpath end
split_path(path)
click to toggle source
# File lib/fssm/path.rb, line 79 def split_path(path) path = FSSM::Pathname.for(path) [@path.to_s, (path.relative? ? path : path.relative_path_from(@path)).to_s] end