revtree 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -1
  3. data/lib/revtree.rb +41 -0
  4. metadata +7 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3cfacc4513023ac62b90be3f9eed6fe2ab45af739aa6676413c947a214b7595f
4
- data.tar.gz: cd7763aed7ca500ef2aaa1103d82e33667b9ad6d732839768b41100f3af435bb
3
+ metadata.gz: e1e3a7315f79eab2d444a7b708fa4412dc14fa0d17aff060848a74c4275eefcc
4
+ data.tar.gz: 712bbd90a68a4cd071c1bd647cb9c36752aa01c0c0122cff32d585895695bd1f
5
5
  SHA512:
6
- metadata.gz: b723f8f00a74e68176bb3b9c60cfd09458cd0b67029ad903c76aee21a411394de498f04285fff7ae07436b35d2b55701e16a6f68fea1645c80acb1ebc6d1111d
7
- data.tar.gz: c6d506353323eb333c849460132257f93abe1426c8fb8b3e6724c16499942409d99db0311c2e7ccd36da8bd401b8396e1689928ac3951b1aa9cc3fcff70a06fa
6
+ metadata.gz: abeccda61e8bd8afbdc553550d43a22e786e20e57e0563f2d3e64e8accf2b67152cfc449b9417a8f10ecc3acf9bd58be2a6cde8402d1d0709586368b3f34054b
7
+ data.tar.gz: a90318c8711df1e9ce50d0fde521c7713e4923316be3e5f65e18a73489fbb76a3b9563651a2031f5ba69e19c11f511eeb713f2700e85c64a13cb22817ed5f2f1
data/README.md CHANGED
@@ -14,6 +14,8 @@ It also provides methods to print and serialize the tree structure, as well as t
14
14
  - **Serialization**: Supports serialization to and from JSON format for easy storage and transfer.
15
15
  - **Pretty Printing**: Provides a method to print the directory tree with statuses.
16
16
  - **Iterate with `for_each`**: Traverse the tree and apply a block to files matching specific statuses.
17
+ - **Watch for Changes**: Automatically monitors a directory for changes with `watch`, and triggers actions based on file changes.
18
+ - **Customizable Watch Interval**: Set a custom interval between checks with the `with_interval` method.
17
19
 
18
20
  ## Install
19
21
 
@@ -39,7 +41,7 @@ sudo make install
39
41
  git clone https://github.com/juliankahlert/revtree.git
40
42
  cd revtree
41
43
  gem build revtree.gemspec
42
- sudo gem install --local revtree-0.1.1.gem
44
+ sudo gem install --local revtree-0.1.2.gem
43
45
  ```
44
46
 
45
47
  ## API Documentation
@@ -70,6 +72,18 @@ sudo gem install --local revtree-0.1.1.gem
70
72
  - `&block` (Proc): A block to execute for each file matching the given statuses.
71
73
  - **Behavior**: Iterates over files in the tree, executing the block for each file whose status matches one of the statuses in the whitelist.
72
74
 
75
+ ### `#watch(status_whitelist = [:modified, :added, :removed], &block)`
76
+
77
+ - **Parameters**:
78
+ - `status_whitelist` (Array<Symbol>): List of statuses to watch (e.g., `[:added, :removed]`).
79
+ - `&block` (Proc): A block to execute when a file matching the given statuses is changed.
80
+
81
+ ### `#with_interval(interval)`
82
+
83
+ - **Parameters**:
84
+ - `interval` (Integer): Interval (in seconds) between checks for changes.
85
+ - **Returns**: The `RevTree` instance, enabling method chaining.
86
+
73
87
  ### `RevTree.from_h(h)`
74
88
 
75
89
  - **Parameters**:
@@ -84,6 +98,8 @@ sudo gem install --local revtree-0.1.1.gem
84
98
 
85
99
  ## Example Usage
86
100
 
101
+ ### Comparing two directory structures
102
+
87
103
  ```ruby
88
104
  #!/bin/env ruby
89
105
 
@@ -105,6 +121,24 @@ result_tree.for_each([:added, :removed]) do |file, full_path|
105
121
  end
106
122
  ```
107
123
 
124
+ ### Watching a directory for changes
125
+
126
+ ```ruby
127
+ #!/bin/env ruby
128
+
129
+ require 'revtree'
130
+
131
+ # Create a RevTree for the current directory, including only .rb and .md files
132
+ file_tree = RevTree.new('./', ['*.rb', '*.md'])
133
+
134
+ # Watch for changes in the directory
135
+ file_tree.with_interval(10).watch([:modified, :added, :removed]) do |file, full_path|
136
+ puts "File #{file.name} #{file.status} in #{full_path}"
137
+ end
138
+ ```
139
+
140
+ This second example demonstrates the `watch` method, which continuously monitors the directory tree for changes and triggers a block of code when changes (additions, modifications, or deletions) are detected. The `with_interval` method customizes the time between checks.
141
+
108
142
  ## Encouragement for Contribution
109
143
 
110
144
  Contributions from the community are welcome!
data/lib/revtree.rb CHANGED
@@ -126,6 +126,47 @@ class RevTree
126
126
  RevTree.traverse_tree(self, status_whitelist, @path, &block)
127
127
  end
128
128
 
129
+ # Watches the tree for changes
130
+ #
131
+ # Compares the refreshed tree to its last version and calls the provided block
132
+ # for each node that matches the statuses in the `status_whitelist`.
133
+ #
134
+ # @param status_whitelist [Array<Symbol>] the list of statuses to match (:added, :modified, etc.)
135
+ # @yield [node, full_path] the block to be executed for each matching file
136
+ # @yieldparam node [RevTree] the node that was changed
137
+ # @yieldparam full_path [String] the full path of the node
138
+ # @return [void]
139
+ def watch(status_whitelist = [:modified, :added, :removed], &block)
140
+ current_tree = self
141
+ @interval ||= 5
142
+
143
+ Signal.trap('INT') { exit }
144
+ Signal.trap('TERM') { exit }
145
+ loop do
146
+ sleep @interval
147
+
148
+ new_tree = RevTree.new(@path, @whitelist)
149
+ diff_tree = RevTree.compare(current_tree, new_tree)
150
+
151
+ next if diff_tree.nil?
152
+
153
+ diff_tree.for_each(status_whitelist) do |node, full_path|
154
+ block.call(node, full_path)
155
+ end
156
+
157
+ current_tree = new_tree
158
+ end
159
+ end
160
+
161
+ # Sets the interval for the watch method's sleep duration.
162
+ #
163
+ # @param interval [Integer] the number of seconds to sleep between checks
164
+ # @return [RevTree] the current instance for chaining
165
+ def with_interval(interval)
166
+ @interval = interval
167
+ self
168
+ end
169
+
129
170
  private
130
171
 
131
172
  # Calculates the MD5 hash for the file.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: revtree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Kahlert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-06 00:00:00.000000000 Z
11
+ date: 2024-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -51,7 +51,8 @@ dependencies:
51
51
  - !ruby/object:Gem::Version
52
52
  version: '3.4'
53
53
  description: RevTree builds a recursive directory tree and compares file revisions.
54
- It can mark files and folders as added, removed, modified, or unmodified.
54
+ It can mark files and folders as added, removed, modified, or unmodified. For convenience,
55
+ RevTree also allows you to watch for changes in a directory tree.
55
56
  email:
56
57
  - 90937526+juliankahlert@users.noreply.github.com
57
58
  executables: []
@@ -63,7 +64,9 @@ files:
63
64
  homepage: https://github.com/juliankahlert/revtree
64
65
  licenses:
65
66
  - MIT
66
- metadata: {}
67
+ metadata:
68
+ homepage_uri: https://github.com/juliankahlert/revtree
69
+ source_code_uri: https://github.com/juliankahlert/revtree
67
70
  post_install_message:
68
71
  rdoc_options: []
69
72
  require_paths: