Class: DirSync::HistoryTraverser

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

Constant Summary collapse

REGEXP =
/:(\d+)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ HistoryTraverser

Returns a new instance of HistoryTraverser.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dir_sync/history_traverser.rb', line 8

def initialize name
  home = File.expand_path '~'
  Pathname.new("#{home}/.dir_sync").mkpath
  @path_old = "#{home}/.dir_sync/#{name}"
  @path_new = "#{@path_old}.new"
  @new = File.open @path_new, 'w'
  @base = '<history>'
  if File.exist? @path_old
    @fiber = Fiber.new do
      File.open(@path_old) do |file|
        file.each {|line| Fiber.yield line.chomp}
      end
      Fiber.yield nil
    end
    @description = :nothing
    advance
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/dir_sync/history_traverser.rb', line 5

def base
  @base
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/dir_sync/history_traverser.rb', line 5

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dir_sync/history_traverser.rb', line 5

def name
  @name
end

#tsObject (readonly)

Returns the value of attribute ts.



5
6
7
# File 'lib/dir_sync/history_traverser.rb', line 5

def ts
  @ts
end

Instance Method Details

#advanceObject



27
28
29
30
31
32
33
34
35
# File 'lib/dir_sync/history_traverser.rb', line 27

def advance
  @description = @fiber.resume if @description
  if @description
    match = REGEXP.match @description
    raise "unable to parse line \"#{@description}\"" unless match
    @name = match.pre_match
    @ts = match[1].to_i
  end
end

#closeObject



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

def close
  @new.close
  `mv #{@path_new} #{@path_old}`
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @description.nil?
end

#report(traverser) ⇒ Object



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

def report traverser
  @new.puts traverser.description
end