Class: AutoCommit::Gatherer

Inherits:
Object
  • Object
show all
Defined in:
lib/autocommit/gatherer.rb

Constant Summary collapse

Delay =

seconds

0.25

Instance Method Summary collapse

Constructor Details

#initialize(dir, committer) ⇒ Gatherer

Returns a new instance of Gatherer.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/autocommit/gatherer.rb', line 10

def initialize dir, committer
  @dir = dir
  @committer = committer
  @mutex = Mutex.new
  @list = []
  Detector.new dir do |event|
    name = event.absolute_name
    puts "#{name}  (#{event.flags})"  unless /\.git/ =~ name
    trigger_commit event.absolute_name
  end
end

Instance Method Details

#commitObject



42
43
44
45
46
47
48
49
50
# File 'lib/autocommit/gatherer.rb', line 42

def commit
  @mutex.synchronize {
    @list.clear
    # Prevent the next commit (>=0.25 seconds later, but still)
    # from interfering with this one:
    # commit from within the synchronized block
    @committer.commit
  }
end

#delayed_commitObject



37
38
39
40
# File 'lib/autocommit/gatherer.rb', line 37

def delayed_commit
  sleep Delay
  commit
end

#start_threadObject



31
32
33
34
35
# File 'lib/autocommit/gatherer.rb', line 31

def start_thread
  Thread.new {
    delayed_commit
  }
end

#trigger_commit(full_name) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/autocommit/gatherer.rb', line 22

def trigger_commit full_name
  in_git_repo = full_name.start_with? File.join(@dir, ".git")
  return  if in_git_repo
  @mutex.synchronize {
    start_thread  if @list.empty?
    @list << Time.now
  }
end