watchly 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b8966b74b90391a7b30d7b74a272de6166ab7fc87aa007ac645329ead905588
4
- data.tar.gz: a8bf52bb46dafdc51d02d9670788725e8dc573fe4a25f7394f6f29774f5948b6
3
+ metadata.gz: 6c133cfc132b5dcfb5c57e0835bd80e2dd7239af7c1631f9d72ccfc7acbb8f60
4
+ data.tar.gz: 2516f37a4cd11b70705b57a77b26d803989322d07cc265751e246dd5394db9ab
5
5
  SHA512:
6
- metadata.gz: 9869ac2f0caa4bb11167d32359ec6e2979d98df79b6b31292d49e252137c90bb66d89de7aff5f4383473032b012d46389f8e5d3e871d4ed67eb8337dd6946292
7
- data.tar.gz: 6b4eade1ab3ece07ad059395c65d31bbe5ffc4074b5a61a8f543e57e5d0e38969a50959306f20521bd624017aca92dc24bcb65ac1480ad621c0da0a57ec968a3
6
+ metadata.gz: 51d950ab49d3e18e064ee3e24263789aefe7b2f4614d78f5a98e858fdc00b75a2992f4c24d02f841f76e2a38f058e9b4960de048b019a61e11f3916c9c6733b1
7
+ data.tar.gz: b0a543c5fd955d3d4fb206cbfbb793f2a18e2c67d9dca68feb09ccb98647cfc73d42a0a9b91128b8561633701c74b6a3d3af89ebd8472a61a1403753788ed2ae
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Watchly
2
2
 
3
+ ![repocard](https://repocard.dannyben.com/svg/watchly.svg)
4
+
3
5
  Watchly is a lightweight, dependency-free, polling-based file watcher for Ruby.
4
- It watches one or more glob patterns and reports file changes in a simple,
5
- predictable way.
6
+ It watches one or more files, directories, or glob patterns and reports file
7
+ changes in a simple, predictable way.
6
8
 
7
9
  Watchly is designed around *batch semantics*: when files change, a single
8
10
  callback is triggered with a complete description of what changed.
@@ -39,10 +41,17 @@ watcher = Watchly::Watcher.new '**/*'
39
41
  # with a different interval
40
42
  watcher = Watchly::Watcher.new '**/*', interval: 2.0
41
43
 
42
- # with multiple glob patterns
44
+ # watch a directory recursively
45
+ watcher = Watchly::Watcher.new 'src'
46
+
47
+ # with multiple targets
43
48
  watcher = Watchly::Watcher.new 'spec/**/*.rb', 'lib/*.*'
44
49
  ```
45
50
 
51
+ Directory targets are watched recursively. Like Ruby's default glob behavior,
52
+ implicit directory expansion excludes dotfiles. Dotfiles can still be watched
53
+ when passed as literal files or matched by an explicit glob pattern.
54
+
46
55
  ---
47
56
 
48
57
  ### Watch for changes
@@ -1,10 +1,11 @@
1
1
  module Watchly
2
2
  class Snapshot
3
- attr_reader :globs, :files
3
+ attr_reader :targets, :files
4
+ alias globs targets
4
5
 
5
- def initialize(globs)
6
- @globs = globs
7
- @files = capture(globs)
6
+ def initialize(targets)
7
+ @targets = targets
8
+ @files = capture(targets)
8
9
  end
9
10
 
10
11
  def diff(other)
@@ -32,16 +33,22 @@ module Watchly
32
33
  end
33
34
  end
34
35
 
35
- def capture(globs)
36
- Array(globs)
37
- .flat_map { |glob| Dir.glob glob }
38
- .uniq
36
+ def capture(targets)
37
+ Array(targets)
38
+ .flat_map { |target| expand_target target }
39
+ .uniq { |path| File.expand_path path }
39
40
  .each_with_object({}) do |path, acc|
40
41
  next unless File.file? path
41
42
 
42
43
  stat = File.stat path
43
- acc[path] = [stat.mtime.to_i, stat.size]
44
+ acc[path] = [stat.mtime, stat.size]
44
45
  end
45
46
  end
47
+
48
+ def expand_target(target)
49
+ return Dir.glob File.join(target, '**', '*') if File.directory? target
50
+
51
+ Dir.glob target
52
+ end
46
53
  end
47
54
  end
@@ -1,3 +1,3 @@
1
1
  module Watchly
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -2,10 +2,11 @@ module Watchly
2
2
  class Watcher
3
3
  DEFAULT_INTERVAL = 1.0
4
4
 
5
- attr_reader :globs, :interval, :stopped
5
+ attr_reader :targets, :interval, :stopped
6
+ alias globs targets
6
7
 
7
- def initialize(*globs, interval: nil)
8
- @globs = globs
8
+ def initialize(*targets, interval: nil)
9
+ @targets = targets
9
10
  @interval = interval || DEFAULT_INTERVAL
10
11
  @stopped = false
11
12
  end
@@ -15,12 +16,12 @@ module Watchly
15
16
  def on_change
16
17
  raise ArgumentError, 'Block required' unless block_given?
17
18
 
18
- previous = Snapshot.new globs
19
+ previous = Snapshot.new targets
19
20
 
20
21
  until stopped
21
22
  sleep interval
22
23
 
23
- current = Snapshot.new globs
24
+ current = Snapshot.new targets
24
25
  next if previous == current
25
26
 
26
27
  changes = previous.diff current
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watchly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
@@ -10,7 +10,7 @@ cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: A small, dependency-free, polling-based library that watches one or more
13
- glob patterns and reports on change
13
+ files, directories, or glob patterns and reports on change
14
14
  email: db@dannyben.com
15
15
  executables: []
16
16
  extensions: []
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  requirements: []
47
- rubygems_version: 4.0.3
47
+ rubygems_version: 4.0.17
48
48
  specification_version: 4
49
49
  summary: Lightweight, polling-based file system watcher
50
50
  test_files: []