Class: Wmctile::Memory

Inherits:
Class
  • Object
show all
Defined in:
lib/wmctile/memory.rb

Instance Method Summary collapse

Methods inherited from Class

#cmd, #notify

Constructor Details

#initializeMemory

init ##########################



7
8
9
10
11
12
13
14
# File 'lib/wmctile/memory.rb', line 7

def initialize
  @path = File.expand_path '~/.config/wmctile/wmctile-memory.yml'
  if not File.exist? @path
    raw_memory = self.create_new_memory
  end
  raw_memory ||= File.read @path
  @memory = YAML.load(raw_memory)
end

Instance Method Details

#create_new_memoryObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wmctile/memory.rb', line 15

def create_new_memory
  dir_path = @path[/(.*)\/wmctile-memory.yml/, 1]
  if not Dir.exists? dir_path
    Dir.mkdir dir_path
  end
  out_file = File.new @path, 'w'
  # 20 workspaces should suffice
  20.times do |i|
    out_file.puts "#{i}:"
  end
  out_file.close
end

#get(workspace = 0, key = nil, key_sub = nil) ⇒ Object

getters/setters ###############



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wmctile/memory.rb', line 35

def get workspace = 0, key = nil, key_sub = nil
  begin
    a = @memory[workspace]
    if key.nil?
      return nil
    else
      a = a[key]
      if key_sub.nil?
        return a
      else
        return a[key_sub]
      end
    end
  rescue Exception => e
    return nil      
  end
end

#set(workspace = 0, key, hash) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wmctile/memory.rb', line 52

def set workspace = 0, key, hash
  hash.merge! 'time' => Time.now.to_i
  if @memory[workspace].nil?
    @memory[workspace] = {}
  end
  if @memory[workspace][key]
    @memory[workspace][key] = hash
  else
    @memory[workspace].merge! key => hash
  end
  self.write_memory
end

#write_memoryObject



27
28
29
30
31
# File 'lib/wmctile/memory.rb', line 27

def write_memory
  out_file = File.new @path, 'w'
  out_file.puts @memory.to_yaml
  out_file.close
end