Class: Mclone::Volume
- Inherits:
-
Object
- Object
- Mclone::Volume
- Defined in:
- lib/mclone.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
0- FILE =
'.mclone'
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
- #commit!(force = false) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(session, file) ⇒ Volume
constructor
A new instance of Volume.
- #modified? ⇒ Boolean
- #root ⇒ Object
- #tasks ⇒ Object
- #to_h ⇒ Object
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
#file ⇒ Object (readonly)
Returns the value of attribute file.
381 382 383 |
# File 'lib/mclone.rb', line 381 def file @file end |
#id ⇒ Object (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
422 423 424 |
# File 'lib/mclone.rb', line 422 def eql?(other) equal?(other) || id == other.id end |
#hash ⇒ Object
417 418 419 |
# File 'lib/mclone.rb', line 417 def hash id.hash end |
#modified? ⇒ 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 |
#root ⇒ Object
385 386 387 |
# File 'lib/mclone.rb', line 385 def root @root ||= File.realpath(File.dirname(file)) end |
#tasks ⇒ Object
452 453 454 |
# File 'lib/mclone.rb', line 452 def tasks TaskSet.new(self).merge!(@session.tasks) end |
#to_h ⇒ Object
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 |