Module: Torm::Tools

Included in:
Torm
Defined in:
lib/torm/tools.rb

Instance Method Summary collapse

Instance Method Details

#atomic_save(target_file, data) ⇒ Object

Save data to a temporary file, then rename it to the final file.



3
4
5
6
7
# File 'lib/torm/tools.rb', line 3

def atomic_save(target_file, data)
  tmp_file = target_file + ".#{Process.pid}.tmp"
  File.open(tmp_file, 'w') { |f| f.write data }
  File.rename(tmp_file, target_file)
end

#slice(hash, *white_listed_keys) ⇒ Object

Return a new Hash with only they white listed keys



17
18
19
20
21
22
23
# File 'lib/torm/tools.rb', line 17

def slice(hash, *white_listed_keys)
  sliced_hash = {}
  white_listed_keys.each do |key|
    sliced_hash[key] = hash[key] if hash.has_key?(key)
  end
  sliced_hash
end

#symbolize_keys(hash) ⇒ Object

Return a new Hash with all keys symbolized



10
11
12
13
14
# File 'lib/torm/tools.rb', line 10

def symbolize_keys(hash)
  symbolized_hash = {}
  hash.each { |k, v| symbolized_hash[k.to_sym] = v }
  symbolized_hash
end