Class: EventMachine::Dir::Glob

Inherits:
Object
  • Object
show all
Defined in:
lib/em-fs/dir/glob.rb

Constant Summary collapse

FORMAT =
"%m %D '%y' %G %n %i '%p' %s %U %A@ %T@ %C@\\n"

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Glob

Returns a new instance of Glob.



7
8
9
10
11
12
# File 'lib/em-fs/dir/glob.rb', line 7

def initialize pattern
  @weight = nil
  @type = nil
  @finished_callbacks = []
  parse pattern
end

Instance Method Details

#each(options = {}, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/em-fs/dir/glob.rb', line 41

def each options = {}, &block
  options = {
    depth: :inf,
    type: :all
  }.merge options

  EM::SystemCommand.execute find_command(options) do |on|
    stats = []
    on.stdout.line do |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

#each_entry(options = {}, &block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/em-fs/dir/glob.rb', line 63

def each_entry options = {}, &block
  options = {
    depth: 1
  }.merge options
  each options do |stat|
    block.call ::File.basename(stat.path)
  end
end

#each_path(options = {}, &block) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/em-fs/dir/glob.rb', line 72

def each_path options = {}, &block
  options = {
    depth: :inf
  }.merge options
  each options do |stat|
    block.call stat.path
  end
end

#finish(&block) ⇒ Object



14
15
16
# File 'lib/em-fs/dir/glob.rb', line 14

def finish &block
  @finished_callbacks << block
end

#parse(pattern) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/em-fs/dir/glob.rb', line 18

def parse pattern
  root = []

  *path, name = pattern.split(::File::SEPARATOR)

  if name.index("*").nil?
    path << name
    name = "*"
  end

  root << path.shift while path[0] and path[0].index("*").nil?

  @name = name
  @path = path.join(::File::SEPARATOR).gsub '**', '*'
  @root = root.join(::File::SEPARATOR)

  if path.length == 0
    @weight = 1
  end

  @root = '.' if @root == ''
end