Module: Bini::Extensions::Savable

Included in:
Sash
Defined in:
lib/bini/extensions/savable.rb

Instance Method Summary collapse

Instance Method Details

#[]=(key, value) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/bini/extensions/savable.rb', line 12

def []=(key,value)
  super key, value
  if options[:autosave] == true
    save
  else
    options[:dirty] = true
  end
  return value
end

#backupObject

Generate a backup file real quick.



52
53
54
# File 'lib/bini/extensions/savable.rb', line 52

def backup
  FileUtils.cp options[:file], backup_file if File.file? options[:file]
end

#backup_fileObject

The save file plus an extension.



91
92
93
# File 'lib/bini/extensions/savable.rb', line 91

def backup_file
  "#{options[:file]}.bak"
end

#basedirObject

The base directory of the save file.



86
87
88
# File 'lib/bini/extensions/savable.rb', line 86

def basedir
  File.dirname File.absolute_path options[:file]
end

#clean_filesObject

clean the system of residual files (obviously destructive).



73
74
75
76
77
78
79
# File 'lib/bini/extensions/savable.rb', line 73

def clean_files
  #FileUtils.rm options[:file], v
  return true if !options[:file]
  FileUtils.rm options[:file] if File.file? options[:file]
  FileUtils.rm backup_file if File.file? backup_file
  return true
end

#initializeObject

Lets setup some options.



6
7
8
9
10
# File 'lib/bini/extensions/savable.rb', line 6

def initialize
  options[:dirty] = false
  options[:file] = "#{Bini.config_dir}/#{Bini.long_name}/savable.yaml"
  super
end

#is_dirty?Boolean

has something updated that means we need to save

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/bini/extensions/savable.rb', line 67

def is_dirty?
  return true if options[:dirty] == true
  return false
end

#loadObject



42
43
44
45
46
47
48
49
# File 'lib/bini/extensions/savable.rb', line 42

def load
  self.clear
  if options[:file] && File.exist?(options[:file]) && File.stat(options[:file]).size > 0
    h = YAML::load open(options[:file], 'r').read
    h.each { |k,v| self[k] = v}
  end
  return self
end

#optionsObject



81
82
83
# File 'lib/bini/extensions/savable.rb', line 81

def options
  @options ||= Hash.new
end

#saveObject

save self into a file



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bini/extensions/savable.rb', line 23

def save
  return false if options[:dirty] && options[:dirty] != true

  FileUtils.mkdir_p basedir if !Dir.exist? basedir

  # I do this the long way because I want an immediate sync.
  f = open(options[:file], 'w')

  # make a plan hash, save that instead of the class.
  f.write(YAML::dump({}.merge(self)))
  f.sync
  f.close

  backup if options[:backup]
  set_mode if options[:mode]
  options[:dirty] = false
  return true
end

#set_modeObject

Set the mode of both the save file and backup file.



57
58
59
60
61
62
63
# File 'lib/bini/extensions/savable.rb', line 57

def set_mode
  FileUtils.chmod options[:mode], options[:file] if options[:mode] && File.exist?(options[:file])

  return true if !backup_file
  FileUtils.chmod options[:mode], backup_file if File.exist? backup_file
  return true
end