lib/em-fs/dir/glob.rb in em-fs-0.1.0 vs lib/em-fs/dir/glob.rb in em-fs-0.1.1
- old
+ new
@@ -4,13 +4,19 @@
FORMAT = "%m %D '%y' %G %n %i '%p' %s %U %A@ %T@ %C@\\n"
def initialize pattern
@weight = nil
+ @type = nil
+ @finished_callbacks = []
parse pattern
end
+ def finish &block
+ @finished_callbacks << block
+ end
+
def parse pattern
root = []
*path, name = pattern.split(::File::SEPARATOR)
@@ -32,17 +38,28 @@
@root = '.' if @root == ''
end
def each options = {}, &block
options = {
- depth: :inf
+ depth: :inf,
+ type: :all
}.merge options
+
EM::SystemCommand.execute find_command(options) do |on|
+ stats = []
on.stdout.line do |line|
- block.call File::Stat.parse line
+ stat = File::Stat.parse(line)
+ stats << stat
+ block.call(stat) if block
end
+ on.success do
+ @finished_callbacks.each do |callback|
+ callback.call(stats)
+ end
+ end
end
+ self
end
def each_entry options = {}, &block
options = {
depth: 1
@@ -62,17 +79,19 @@
end
private
def find_command options = {}
options = {
- depth: (@weight || :inf)
+ depth: (@weight || :inf),
+ type: (@type || :all)
}.merge options
builder = EM::SystemCommand::Builder.new 'find'
builder << @root
builder << [ :path, @path ] unless @path == "*" or @path == ''
builder << [ :name, @name ] unless @name == "*"
builder << [ :maxdepth, options[:depth] ] unless options[:depth] == :inf
+ builder << [ :type, options[:type] ] unless options[:type] == :all
builder << [ :printf, FORMAT ]
builder.to_s.gsub(/-+(\w)/, "-\\1")
end
end