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(h) ⇒ 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 = nil) ⇒ 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.
Constructor Details
#initialize(path, whitelist = nil) ⇒ RevTree
Initializes a new ‘RevTree` object representing a directory or file.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/revtree.rb', line 54 def initialize(path, whitelist = nil) @path = Pathname.new(path) @name = @path.basename.to_s @whitelist = whitelist || [] @status = :unmodified 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(h) ⇒ RevTree
Reconstructs a ‘RevTree` object from a hash.
101 102 103 |
# File 'lib/revtree.rb', line 101 def self.from_h(h) new_tree(h[:name], h[:type].to_sym, h[:rev], h[:children], h[:status]) end |
.from_json(json_str) ⇒ RevTree
Reconstructs a ‘RevTree` object from a JSON string.
109 110 111 112 113 114 |
# File 'lib/revtree.rb', line 109 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.
123 124 125 126 127 |
# File 'lib/revtree.rb', line 123 def for_each(status_whitelist = [:unmodified, :modified, :added, :removed], &block) return unless block_given? RevTree.traverse_tree(self, status_whitelist, @path, &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.
71 72 73 74 75 |
# File 'lib/revtree.rb', line 71 def print_tree(indent = 0) status_str = @status ? " (status: #{@status})" : '' puts "#{' ' * indent}#{@type == :folder ? '[Folder]' : '[File]'} #{@name} (rev: #{@rev})#{status_str}" @children.each { |child| child.print_tree(indent + 1) } end |
#to_h ⇒ Hash
Serializes the ‘RevTree` object to a hash.
80 81 82 83 84 85 86 87 88 |
# File 'lib/revtree.rb', line 80 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.
93 94 95 |
# File 'lib/revtree.rb', line 93 def to_json JSON.pretty_generate(self.to_h) end |