Class: Sxn::Rules::BaseRule::RuleChange

Inherits:
Object
  • Object
show all
Defined in:
lib/sxn/rules/base_rule.rb

Overview

Represents a single change made by a rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, target, metadata = {}) ⇒ RuleChange

Returns a new instance of RuleChange.



330
331
332
333
334
335
# File 'lib/sxn/rules/base_rule.rb', line 330

def initialize(type, target,  = {})
  @type = type
  @target = target
   = .freeze
  @timestamp = Time.now
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



328
329
330
# File 'lib/sxn/rules/base_rule.rb', line 328

def 
  
end

#targetObject (readonly)

Returns the value of attribute target.



328
329
330
# File 'lib/sxn/rules/base_rule.rb', line 328

def target
  @target
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



328
329
330
# File 'lib/sxn/rules/base_rule.rb', line 328

def timestamp
  @timestamp
end

#typeObject (readonly)

Returns the value of attribute type.



328
329
330
# File 'lib/sxn/rules/base_rule.rb', line 328

def type
  @type
end

Instance Method Details

#rollbackObject

Rollback this specific change



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/sxn/rules/base_rule.rb', line 338

def rollback
  case @type
  when :file_created
    FileUtils.rm_f(@target)
  when :file_modified
    FileUtils.mv([:backup_path], @target) if [:backup_path] && File.exist?([:backup_path])
  when :directory_created
    Dir.rmdir(@target) if File.directory?(@target) && Dir.empty?(@target)
  when :symlink_created
    File.unlink(@target) if File.symlink?(@target)
  when :command_executed
    # Command execution cannot be rolled back
    # This is logged for audit purposes only
  else
    raise Sxn::Rules::RollbackError, "Unknown change type for rollback: #{@type}"
  end
end

#to_hObject



356
357
358
359
360
361
362
363
# File 'lib/sxn/rules/base_rule.rb', line 356

def to_h
  {
    type: @type,
    target: @target,
    metadata: ,
    timestamp: @timestamp.iso8601
  }
end