Class: DirSync::Traverser

Inherits:
Object
  • Object
show all
Defined in:
lib/dir_sync/traverser.rb

Constant Summary collapse

TOLERANCE =
5
IGNORE_PATTERNS =
[
  /\.DS_Store$/,
  /\/\._/
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, file_system) ⇒ Traverser

Returns a new instance of Traverser.



15
16
17
18
19
20
21
22
23
24
# File 'lib/dir_sync/traverser.rb', line 15

def initialize path, file_system
  @file_system = file_system
  @base = Pathname.new path
  @base.mkpath
  @fiber = Fiber.new do
    @base.find { |child| Fiber.yield child if child.file? }
    Fiber.yield nil
  end
  @current = @fiber.resume
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/dir_sync/traverser.rb', line 7

def base
  @base
end

Instance Method Details

#advanceObject



30
31
32
# File 'lib/dir_sync/traverser.rb', line 30

def advance
  @current = @fiber.resume if @current
end

#cp(*traversers) ⇒ Object



50
51
52
53
54
# File 'lib/dir_sync/traverser.rb', line 50

def cp *traversers
  traversers.each do |t|
    @file_system.cp @current.to_s, "#{t.base}/#{name}" unless equivalent? t
  end
end

#descriptionObject



26
27
28
# File 'lib/dir_sync/traverser.rb', line 26

def description
  empty? ? 'empty' : "#{name}:#{ts}"
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dir_sync/traverser.rb', line 42

def empty?
  @current.nil?
end

#equivalent?(traverser) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/dir_sync/traverser.rb', line 60

def equivalent? traverser
  name == traverser.name and (ts - traverser.ts).abs <= TOLERANCE
end

#ignored?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dir_sync/traverser.rb', line 46

def ignored?
  IGNORE_PATTERNS.any? {|pattern| pattern.match name }
end

#nameObject



34
35
36
# File 'lib/dir_sync/traverser.rb', line 34

def name
  @current.relative_path_from(@base).to_s if @current
end

#rmObject



56
57
58
# File 'lib/dir_sync/traverser.rb', line 56

def rm
  @file_system.rm @current.to_s
end

#tsObject



38
39
40
# File 'lib/dir_sync/traverser.rb', line 38

def ts
  @current.mtime.to_i if @current
end