Class: Bini::Sash

Inherits:
Hash
  • Object
show all
Defined in:
lib/bini/sash.rb

Overview

This is a savable hash, it can be configured and used to store whatever the# contents of the hash are for loading later. Will serialize in yaml to keep all the dependencies in ruby stdlib.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Sash

initialization sets the values of the class Sash, not the contents of the Hash.



18
19
20
21
# File 'lib/bini/sash.rb', line 18

def initialize(params = {})
  params.each { |k,v| instance_variable_set "@" + k.to_s,v}
  load if @auto_load
end

Instance Attribute Details

#auto_loadObject

Returns the value of attribute auto_load.



12
13
14
# File 'lib/bini/sash.rb', line 12

def auto_load
  @auto_load
end

#auto_saveObject

Returns the value of attribute auto_save.



13
14
15
# File 'lib/bini/sash.rb', line 13

def auto_save
  @auto_save
end

#backupObject

Generate a backup file real quick.



73
74
75
# File 'lib/bini/sash.rb', line 73

def backup
  @backup
end

#fileObject

Returns the value of attribute file.



9
10
11
# File 'lib/bini/sash.rb', line 9

def file
  @file
end

#modeObject

Returns the value of attribute mode.



11
12
13
# File 'lib/bini/sash.rb', line 11

def mode
  @mode
end

Instance Method Details

#[]=(key, value) ⇒ Object



51
52
53
54
# File 'lib/bini/sash.rb', line 51

def []=(key,value)
  store key, value
  save! if @auto_save == true
end

#backup_fileObject

The save file plus an extension.



30
31
32
# File 'lib/bini/sash.rb', line 30

def backup_file
  "#{@file}.bak"
end

#basedirObject

The base directory of the save file.



24
25
26
27
# File 'lib/bini/sash.rb', line 24

def basedir
  return nil if !file
  File.dirname File.absolute_path @file
end

#loadObject

Load the save file into self.



62
63
64
65
66
67
68
69
70
# File 'lib/bini/sash.rb', line 62

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}
    return true
  end
  false
end

#saveObject

Save the hash to the file, check for backup and set_mode.



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

def save
  if any?
    FileUtils.mkdir_p basedir if !Dir.exist? basedir
    backup if @backup

    # I do this the long way because I want an immediate sync.
    f = open(@file, 'w')
    f.write YAML::dump self
    f.sync
    f.close

    set_mode if @mode
  end
  true
end

#save!Object

Save the hash to a file, overwriting if necessary.



56
57
58
59
# File 'lib/bini/sash.rb', line 56

def save!
  delete_file
  save
end

#set_modeObject

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



78
79
80
81
82
83
84
85
86
87
# File 'lib/bini/sash.rb', line 78

def set_mode
  # Why are we trying to set_mode when we don't even have a file?
  return false if !@file
  File.chmod @mode, @file if File.exist? @file

  # the backup file may not exist for whatever reason, lets not shit if it doesn't.
  return true if !backup_file
  File.chmod @mode, backup_file if File.exist? backup_file
  true
end