Module: Bini::Extensions::Savable
- Included in:
- Sash
- Defined in:
- lib/bini/extensions/savable.rb
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
-
#backup ⇒ Object
Generate a backup file real quick.
-
#backup_file ⇒ Object
The save file plus an extension.
-
#basedir ⇒ Object
The base directory of the save file.
-
#clean_files ⇒ Object
clean the system of residual files (obviously destructive).
-
#initialize ⇒ Object
Lets setup some options.
-
#is_dirty? ⇒ Boolean
has something updated that means we need to save.
- #load ⇒ Object
- #options ⇒ Object
-
#save ⇒ Object
save self into a file.
-
#set_mode ⇒ Object
Set the mode of both the save file and backup file.
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 [:autosave] == true save else [:dirty] = true end return value end |
#backup ⇒ Object
Generate a backup file real quick.
52 53 54 |
# File 'lib/bini/extensions/savable.rb', line 52 def backup FileUtils.cp [:file], backup_file if File.file? [:file] end |
#backup_file ⇒ Object
The save file plus an extension.
91 92 93 |
# File 'lib/bini/extensions/savable.rb', line 91 def backup_file "#{[:file]}.bak" end |
#basedir ⇒ Object
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 [:file] end |
#clean_files ⇒ Object
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 ![:file] FileUtils.rm [:file] if File.file? [:file] FileUtils.rm backup_file if File.file? backup_file return true end |
#initialize ⇒ Object
Lets setup some options.
6 7 8 9 10 |
# File 'lib/bini/extensions/savable.rb', line 6 def initialize [:dirty] = false [:file] = "#{Bini.config_dir}/#{Bini.long_name}/savable.yaml" super end |
#is_dirty? ⇒ Boolean
has something updated that means we need to save
67 68 69 70 |
# File 'lib/bini/extensions/savable.rb', line 67 def is_dirty? return true if [:dirty] == true return false end |
#load ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/bini/extensions/savable.rb', line 42 def load self.clear if [:file] && File.exist?([:file]) && File.stat([:file]).size > 0 h = YAML::load open([:file], 'r').read h.each { |k,v| self[k] = v} end return self end |
#options ⇒ Object
81 82 83 |
# File 'lib/bini/extensions/savable.rb', line 81 def @options ||= Hash.new end |
#save ⇒ Object
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 [:dirty] && [:dirty] != true FileUtils.mkdir_p basedir if !Dir.exist? basedir # I do this the long way because I want an immediate sync. f = open([:file], 'w') # make a plan hash, save that instead of the class. f.write(YAML::dump({}.merge(self))) f.sync f.close backup if [:backup] set_mode if [:mode] [:dirty] = false return true end |
#set_mode ⇒ Object
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 [:mode], [:file] if [:mode] && File.exist?([:file]) return true if !backup_file FileUtils.chmod [:mode], backup_file if File.exist? backup_file return true end |