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



46
47
48
49
50
51
52
# File 'lib/vagrant_snap.rb', line 46

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



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

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)


71
72
73
# File 'lib/vagrant_snap.rb', line 71

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

.initObject



13
14
15
16
# File 'lib/vagrant_snap.rb', line 13

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

.lastnameObject



63
64
65
66
67
68
69
# File 'lib/vagrant_snap.rb', line 63

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

.parse_tree(vmname) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/vagrant_snap.rb', line 18

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant_snap.rb', line 75

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



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

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



7
8
9
# File 'lib/vagrant_snap.rb', line 7

def tree
  @@tree
end