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.



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

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.



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

def max_entries
  @max_entries
end

Instance Method Details

#add_entry(prefs = {}) ⇒ Object



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

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



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

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

#each(&b) ⇒ Object



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

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

#entriesObject



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

def entries
  @history_entries
end

#load(history_file) ⇒ Object



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

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

#saveObject



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

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



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

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