Class: Watobo::Gui::SessionHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/watobo/gui/utils/session_history.rb

Overview

Class for managing history entries

entries are organised as a hash.
each entry consists of a hash.
the key of an entry is based on its session filename

@history_entry[session_file] = {
:last_used
:created
:project_name
:session_name
:description
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ SessionHistory

Returns a new instance of SessionHistory.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/watobo/gui/utils/session_history.rb', line 105

def initialize(filename)

  @max_entries = 8
  @history_entries = Hash.new
  @history_file = filename

  if File.exist? @history_file
  load(@history_file)
  else
    begin
      File.open(@history_file,"w") { |fh| fh.write YAML.dump(@history_entries) }
    rescue => bang
    puts bang
    puts bang.backtrace if $DEBUG
    end
  end

end

Instance Attribute Details

#max_entriesObject

Returns the value of attribute max_entries.



44
45
46
# File 'lib/watobo/gui/utils/session_history.rb', line 44

def max_entries
  @max_entries
end

Instance Method Details

#add_entry(prefs = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/watobo/gui/utils/session_history.rb', line 58

def add_entry(prefs = {})
  t_now = Time.now.to_i
  return false unless prefs.has_key? :session_name or prefs.has_key? :project_name
  puts "#"
  hid = history_id(prefs[:project_name], prefs[:session_name])
  @history_entries[hid] ||= {
    :created => t_now
  }
  @history_entries[hid][:last_used] = t_now

  [ :description, :project_name, :session_name ].each do |k|
    @history_entries[hid][k] = prefs[k] if prefs.has_key? k
  end

  while @history_entries.length > @max_entries do
    oid, ov = @history_entries.min_by{ |id,v| v[:last_used] }
    @history_entries.delete oid
  end

  save()
end

#delete_entry(project_name, session_name) ⇒ Object



80
81
82
# File 'lib/watobo/gui/utils/session_history.rb', line 80

def delete_entry(project_name, session_name)
  @history_entries.delete history_id(project_name, session_name)
end

#each(&b) ⇒ Object



93
94
95
96
97
# File 'lib/watobo/gui/utils/session_history.rb', line 93

def each(&b)
  @history_entries.each_key{ |k|
    yield @history_entries[k] if block_given?
  }
end

#entriesObject



54
55
56
# File 'lib/watobo/gui/utils/session_history.rb', line 54

def entries
  @history_entries
end

#load(history_file) ⇒ Object



99
100
101
102
103
# File 'lib/watobo/gui/utils/session_history.rb', line 99

def load(history_file)
  if File.exist? history_file
  @history_entries = YAML.load_file(history_file)
  end
end

#saveObject



45
46
47
48
49
50
51
52
# File 'lib/watobo/gui/utils/session_history.rb', line 45

def save()
  begin
    File.open(@history_file,"w") { |fh| fh.write YAML.dump(@history_entries) }
  rescue => bang
  puts bang
  puts bang.backtrace if $DEBUG
  end
end

#update_usage(prefs) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/watobo/gui/utils/session_history.rb', line 84

def update_usage(prefs)
  t_now = Time.now.to_i
  return false unless prefs.has_key? :session_name or prefs.has_key? :project_name
  hid = history_id(prefs[:project_name], prefs[:session_name])
  return false unless @history_entries.has_key? hid
  @history_entries[hid][:last_used] = t_now
  save()
end