Class: Snap::VBox::SnapShot

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

Overview

{{{

Defined Under Namespace

Classes: Snap

Class Method Summary collapse

Class Method Details

._parse(s) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/vagrant_snap.rb', line 48

def _parse(s)
  tree = [ Snap.new(s.name , s.time_stamp, s.description, s.uuid, s.uuid == @@current.uuid) ]
  s.children.each do |c|
    tree.concat [_parse(c)]
  end
  tree
end

.format(guide, s) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/vagrant_snap.rb', line 56

def format(guide, s)
  time     = time_elapse(Time.now - s.time_stamp)
  snapinfo = "#{s.name} [ #{time} ]"
  snapinfo  = snapinfo.yellow  if s.current
  result   = "#{guide} #{snapinfo}"
  result  << " #{s.description}" unless s.description.empty?
  result  << "\n"
end

.include?(name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/vagrant_snap.rb', line 83

def include?(name)
  return false unless tree
  tree.flatten.map(&:name).include? name
end

.initObject



15
16
17
18
# File 'lib/vagrant_snap.rb', line 15

def init
  @@current = nil
  @@tree = nil
end

.lastnameObject



65
66
67
68
69
70
71
# File 'lib/vagrant_snap.rb', line 65

def lastname
  if tree
    tree.flatten.sort_by(&:time_stamp).last.name
  else
    nil
  end
end

.next_available_snapnameObject



73
74
75
76
77
78
79
80
81
# File 'lib/vagrant_snap.rb', line 73

def next_available_snapname
  if lastname.nil?
    "0"
  else
    n = lastname.succ
    n = n.succ while VBox::SnapShot.include? n
    n
  end
end

.parse_tree(vmname) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/vagrant_snap.rb', line 20

def parse_tree(vmname)
  init
  vm = ::VirtualBox::VM.find( vmname )
  @@current = vm.current_snapshot
  return unless @@current
  @@tree = _parse(vm.root_snapshot)
end

.show(t = tree, guide = "") ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/vagrant_snap.rb', line 88

def show(t=tree, guide="")
  result = ""
  t.each_with_index do |v, idx|
    case v
    when Array
      tmp = guide.dup.chop.chop.sub("`", " ") << "    "
      tmp << "#{t.size == idx + 1 ? '`' : '|'}" << "--"
      result << show(v, tmp)
    else
      result << format(guide, v)
    end
  end
  result
end

.time_elapse(time) ⇒ Object

TODO

need refactoring



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vagrant_snap.rb', line 29

def time_elapse(time)
  _sec  = 1
  _min  = _sec * 60
  _hour = _min * 60
  _day  = _hour * 24

  sec = time.to_i
  min = sec / _min
  hour = sec / _hour
  day  = sec / _day

  case
  when day  > 0 then "#{day} day#{day == 1 ? '' : 's'}"
  when hour > 0 then "#{hour} hour#{hour == 1 ? '' : 's'}"
  when min  > 0 then "#{min} minute#{min == 1 ? '' : 's'}"
  when sec  > 0 then "#{sec} second#{sec == 1 ? '' : 's'}"
  end
end

.treeObject



9
10
11
# File 'lib/vagrant_snap.rb', line 9

def tree
  @@tree
end