Class: RevTree
- Inherits:
-
Object
- Object
- RevTree
- Defined in:
- lib/revtree.rb
Overview
The `RevTree` class provides a tree structure representing file directories and files, allowing for version tracking based on MD5 hashes.
This class can traverse directories, compare versions of trees, and serialize/deserialize itself to/from JSON.
Instance Attribute Summary collapse
-
#children ⇒ Array<RevTree>
readonly
The list of children in the tree (empty for files).
-
#name ⇒ String
readonly
The name of the file or directory.
-
#rev ⇒ String
readonly
The revision (MD5 hash) of the file or directory.
-
#status ⇒ Symbol
readonly
The status of the file or directory (:unmodified, :modified, :added, :removed).
-
#type ⇒ Symbol
readonly
The type of the node (:folder or :file).
Class Method Summary collapse
-
.from_h(hash) ⇒ RevTree
Reconstructs a `RevTree` object from a hash.
-
.from_json(json_str) ⇒ RevTree
Reconstructs a `RevTree` object from a JSON string.
Instance Method Summary collapse
-
#for_each(status_whitelist = [:unmodified, :modified, :added, :removed]) {|node, full_path| ... } ⇒ void
Executes a block of code for each file matching the provided status whitelist.
-
#initialize(path, whitelist = ['*']) ⇒ RevTree
constructor
Initializes a new `RevTree` object representing a directory or file.
-
#print_tree(indent = 0) ⇒ void
Prints the tree structure, including file names and statuses, to the console.
-
#to_h ⇒ Hash
Serializes the `RevTree` object to a hash.
-
#to_json ⇒ String
Converts the `RevTree` object to JSON format.
-
#watch(status_whitelist = [:modified, :added, :removed]) {|node, full_path| ... } ⇒ void
Watches the tree for changes.
-
#with_interval(interval) ⇒ RevTree
Sets the interval for the watch method's sleep duration.
Constructor Details
#initialize(path, whitelist = ['*']) ⇒ RevTree
Initializes a new `RevTree` object representing a directory or file.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/revtree.rb', line 54 def initialize(path, whitelist = ['*']) @path = Pathname.new(path) @name = @path.basename.to_s @whitelist = whitelist @status = :unmodified @type = :folder @children = [] @interval = 5 @rev = '' if @path.directory? init_dir else init_file end end |
Instance Attribute Details
#children ⇒ Array<RevTree> (readonly)
Returns the list of children in the tree (empty for files).
36 37 38 |
# File 'lib/revtree.rb', line 36 def children @children end |
#name ⇒ String (readonly)
Returns the name of the file or directory.
42 43 44 |
# File 'lib/revtree.rb', line 42 def name @name end |
#rev ⇒ String (readonly)
Returns the revision (MD5 hash) of the file or directory.
45 46 47 |
# File 'lib/revtree.rb', line 45 def rev @rev end |
#status ⇒ Symbol (readonly)
Returns the status of the file or directory (:unmodified, :modified, :added, :removed).
48 49 50 |
# File 'lib/revtree.rb', line 48 def status @status end |
#type ⇒ Symbol (readonly)
Returns the type of the node (:folder or :file).
39 40 41 |
# File 'lib/revtree.rb', line 39 def type @type end |
Class Method Details
.from_h(hash) ⇒ RevTree
Reconstructs a `RevTree` object from a hash.
105 106 107 108 109 |
# File 'lib/revtree.rb', line 105 def self.from_h(hash) hash[:status] = hash[:status].to_sym hash[:type] = hash[:type].to_sym new_tree(hash) end |
.from_json(json_str) ⇒ RevTree
Reconstructs a `RevTree` object from a JSON string.
115 116 117 118 119 120 |
# File 'lib/revtree.rb', line 115 def self.from_json(json_str) data = JSON.parse(json_str, symbolize_names: true) file_tree = from_h(data) file_tree end |
Instance Method Details
#for_each(status_whitelist = [:unmodified, :modified, :added, :removed]) {|node, full_path| ... } ⇒ void
This method returns an undefined value.
Executes a block of code for each file matching the provided status whitelist.
129 130 131 132 133 |
# File 'lib/revtree.rb', line 129 def for_each(status_whitelist = [:unmodified, :modified, :added, :removed], &block) return unless block_given? RevTree.traverse_tree(self, status_whitelist, @path, nil, &block) end |
#print_tree(indent = 0) ⇒ void
This method returns an undefined value.
Prints the tree structure, including file names and statuses, to the console.
75 76 77 78 79 |
# File 'lib/revtree.rb', line 75 def print_tree(indent = 0) indent_prefix = ' ' * indent puts "#{indent_prefix}#{type_to_str()} #{@name} (rev: #{@rev}) #{status_to_str()}" @children.each { |child| child.print_tree(indent + 1) } end |
#to_h ⇒ Hash
Serializes the `RevTree` object to a hash.
84 85 86 87 88 89 90 91 92 |
# File 'lib/revtree.rb', line 84 def to_h { type: @type, name: @name, rev: @rev, status: @status, children: @children.map(&:to_h), } end |
#to_json ⇒ String
Converts the `RevTree` object to JSON format.
97 98 99 |
# File 'lib/revtree.rb', line 97 def to_json JSON.pretty_generate(self.to_h) end |
#watch(status_whitelist = [:modified, :added, :removed]) {|node, full_path| ... } ⇒ void
This method returns an undefined value.
Watches the tree for changes
Compares the refreshed tree to its last version and calls the provided block for each node that matches the statuses in the `status_whitelist`.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/revtree.rb', line 145 def watch(status_whitelist = [:modified, :added, :removed], &block) current_tree = self setup_traps loop do sleep @interval current_tree = refresh_tree(current_tree, status_whitelist, block) end end |
#with_interval(interval) ⇒ RevTree
Sets the interval for the watch method's sleep duration.
160 161 162 163 |
# File 'lib/revtree.rb', line 160 def with_interval(interval) @interval = interval self end |