Class: RevTree

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, whitelist = ['*']) ⇒ RevTree

Initializes a new `RevTree` object representing a directory or file.

Parameters:

  • path (String, Pathname)

    the path to the file or directory

  • whitelist (Array<String>, nil) (defaults to: ['*'])

    a list of file patterns to include (optional)



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

#childrenArray<RevTree> (readonly)

Returns the list of children in the tree (empty for files).

Returns:

  • (Array<RevTree>)

    the list of children in the tree (empty for files)



36
37
38
# File 'lib/revtree.rb', line 36

def children
  @children
end

#nameString (readonly)

Returns the name of the file or directory.

Returns:

  • (String)

    the name of the file or directory



42
43
44
# File 'lib/revtree.rb', line 42

def name
  @name
end

#revString (readonly)

Returns the revision (MD5 hash) of the file or directory.

Returns:

  • (String)

    the revision (MD5 hash) of the file or directory



45
46
47
# File 'lib/revtree.rb', line 45

def rev
  @rev
end

#statusSymbol (readonly)

Returns the status of the file or directory (:unmodified, :modified, :added, :removed).

Returns:

  • (Symbol)

    the status of the file or directory (:unmodified, :modified, :added, :removed)



48
49
50
# File 'lib/revtree.rb', line 48

def status
  @status
end

#typeSymbol (readonly)

Returns the type of the node (:folder or :file).

Returns:

  • (Symbol)

    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.

Parameters:

  • hash (Hash)

    the hash to deserialize

Returns:

  • (RevTree)

    the reconstructed `RevTree` object



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.

Parameters:

  • json_str (String)

    the JSON string to deserialize

Returns:

  • (RevTree)

    the reconstructed `RevTree` object



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.

Parameters:

  • status_whitelist (Array<Symbol>) (defaults to: [:unmodified, :modified, :added, :removed])

    the list of statuses to match (:added, :modified, etc.)

Yields:

  • (node, full_path)

    the block to be executed for each matching file

Yield Parameters:

  • node (RevTree)

    the current node being traversed

  • full_path (String)

    the full path of the current node



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

This method returns an undefined value.

Prints the tree structure, including file names and statuses, to the console.

Parameters:

  • indent (Integer) (defaults to: 0)

    the indentation level (default: 0)



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_hHash

Serializes the `RevTree` object to a hash.

Returns:

  • (Hash)

    a hash representing the object



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_jsonString

Converts the `RevTree` object to JSON format.

Returns:

  • (String)

    a JSON string representing the object



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`.

Parameters:

  • status_whitelist (Array<Symbol>) (defaults to: [:modified, :added, :removed])

    the list of statuses to match (:added, :modified, etc.)

Yields:

  • (node, full_path)

    the block to be executed for each matching file

Yield Parameters:

  • node (RevTree)

    the node that was changed

  • full_path (String)

    the full path of the node



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.

Parameters:

  • interval (Integer)

    the number of seconds to sleep between checks

Returns:

  • (RevTree)

    the current instance for chaining



160
161
162
163
# File 'lib/revtree.rb', line 160

def with_interval(interval)
  @interval = interval
  self
end