Skip to content

Commit

Permalink
Merge branch 'master' of github.com:faustinoaq/watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
faustinoaq committed Apr 2, 2017
2 parents 1a66623 + d91faf1 commit c168159
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
10 changes: 9 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
require "spec"
require "../src/watcher"

TEST_FILE = "src/watcher.cr"
TEST_FILE = "src/watcher.cr"

module Watcher

# Allow to read interval value
def self.interval
@@interval
end
end
18 changes: 16 additions & 2 deletions spec/watcher_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,32 @@ describe Watcher do
end

it "verify Watcher::WatchEvent.event.change" do
Watcher.watch TEST_FILE do |event|
Watcher.watch(TEST_FILE) do |event|
event.status = true
event.status.should eq(true)
break
end
end

it "verify Watcher::WatchEvent.event.files" do
Watcher.watch TEST_FILE do |event|
Watcher.watch(TEST_FILE) do |event|
event.files[TEST_FILE] = timestamp = Watcher.timestamp_for(TEST_FILE)
event.files.should eq({TEST_FILE => timestamp})
break
end
end

it "verify default WatcherEvent interval" do
Watcher.watch(TEST_FILE) do |event|
event.interval.should eq(1)
break
end
end

it "change WatcherEvent interval" do
Watcher.watch(TEST_FILE, 2) do |event|
event.interval.should eq(2)
break
end
end
end
13 changes: 10 additions & 3 deletions src/watcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ require "./watcher/*"
module Watcher
TIMESTAMPS = {} of String => String

# Class to save file changes
private class WatchEvent
property status = false, files = {} of String => String
getter interval

def initialize(@interval : Int32)
end

# Allow to yield a block when a file changes
def on_change
yield files if status
end
Expand All @@ -29,13 +36,13 @@ module Watcher
end

# Allow to watch file changes using Watcher.watch
def self.watch(files)
event = WatchEvent.new
def self.watch(files, interval = 1)
event = WatchEvent.new(interval)
loop do
event = scanner(files, event)
yield event
event.files.clear
sleep 1
sleep event.interval
end
end
end
Expand Down

0 comments on commit c168159

Please sign in to comment.