Class: Mclone::Volume

Inherits:
Object
  • Object
show all
Defined in:
lib/mclone.rb

Defined Under Namespace

Classes: Error, TaskSet

Constant Summary collapse

VERSION =
0
FILE =
'.mclone'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, file) ⇒ Volume

Returns a new instance of Volume.



390
391
392
393
394
395
# File 'lib/mclone.rb', line 390

def initialize(session, file)
  @loaded_tasks = ObjectSet.new
  @id = SecureRandom.hex(4)
  @session = session
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



381
382
383
# File 'lib/mclone.rb', line 381

def file
  @file
end

#idObject (readonly)

Returns the value of attribute id.



378
379
380
# File 'lib/mclone.rb', line 378

def id
  @id
end

Class Method Details

.restore(session, file) ⇒ Object



398
399
400
401
402
# File 'lib/mclone.rb', line 398

def self.restore(session, file)
  obj = allocate
  obj.send(:from_file, session, file)
  obj
end

Instance Method Details

#commit!(force = false) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/mclone.rb', line 433

def commit!(force = false)
  if force || @session.force? || modified?
    # As a safeguard against malformed volume files generation, first write to a new file
    # and rename it to a real volume file only in case of normal turn of events
    _file = "#{file}~"
    begin
      open(_file, 'w') do |stream|
        stream << JSON.pretty_generate(to_h)
        tasks.commit!
      end
      FileUtils.mv(_file, file, force: true)
    ensure
      FileUtils.rm_f(_file)
    end
  end
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


422
423
424
# File 'lib/mclone.rb', line 422

def eql?(other)
  equal?(other) || id == other.id
end

#hashObject



417
418
419
# File 'lib/mclone.rb', line 417

def hash
  id.hash
end

#modified?Boolean

Returns:

  • (Boolean)


427
428
429
430
# File 'lib/mclone.rb', line 427

def modified?
  # Comparison against the original loaded tasks set allows to account for task removals
  (ts = tasks).modified? || ts != @loaded_tasks
end

#rootObject



385
386
387
# File 'lib/mclone.rb', line 385

def root
  @root ||= File.realpath(File.dirname(file))
end

#tasksObject



452
453
454
# File 'lib/mclone.rb', line 452

def tasks
  TaskSet.new(self).merge!(@session.tasks)
end

#to_hObject



457
458
459
# File 'lib/mclone.rb', line 457

def to_h
  { mclone: VERSION, volume: id, tasks: tasks.collect { |task| task.to_h(self) } }
end