class Epi::Data
Constants
- ROOT_HOME
Attributes
Public Class Methods
# File lib/epi/data.rb, line 20 def configuration_paths self['configuration_paths'] ||= [] end
Get the default data storage instance @return [self]
# File lib/epi/data.rb, line 34 def default_instance @default_instance ||= new(detect_home_dir) end
# File lib/epi/data.rb, line 24 def jobs self['jobs'] ||= {} end
# File lib/epi/data.rb, line 28 def jobs=(value) self['jobs'] = value end
@param home [String] The directory in which all working files should be stored.
# File lib/epi/data.rb, line 75 def initialize(home) @home = Pathname home prepare_home! end
Remove the default instance. Useful if the home path changes.
# File lib/epi/data.rb, line 39 def reset! @default_instance = nil end
Private Class Methods
The home directory specified in the environment @return [String|NilClass]
# File lib/epi/data.rb, line 54 def custom_home_dir ENV['EPI_HOME'] end
Try to guess the Epi
home directory, by first looking at the ‘EPI_HOME` environment variable, then ’/etc/epi’, then ‘~/.epi’ @return [String]
# File lib/epi/data.rb, line 48 def detect_home_dir custom_home_dir || root_home_dir || user_home_dir end
The root home directory, if it exists or can be created @return [String|NilClass]
# File lib/epi/data.rb, line 60 def root_home_dir (Epi.root? || Dir.exist?(ROOT_HOME)) && ROOT_HOME end
The user’s home directory @return [String]
# File lib/epi/data.rb, line 66 def user_home_dir "#{ENV['HOME'] || '~'}/.epi" end
Public Instance Methods
# File lib/epi/data.rb, line 131 def [](key) hash[key.to_s] end
# File lib/epi/data.rb, line 135 def []=(key, value) hash[key.to_s] = value end
# File lib/epi/data.rb, line 105 def data_file @data_file ||= home + 'data' end
# File lib/epi/data.rb, line 120 def hash @hash ||= data_file.exist? ? Marshal.load(data_file.binread) : {} end
Read a file as UTF-8 @param file_name [String] Name of the file to read @return [String|NilClass] Contents of the file, or ‘nil` if the file doesn’t exist.
# File lib/epi/data.rb, line 83 def read(file_name) path = home + file_name path.exist? ? path.read : nil end
Force reload of data from disk
# File lib/epi/data.rb, line 110 def reload @hash = nil end
Returns true if using root data at /etc/epi, or false if using user data is at ~/.epi @return [TrueClass|FalseClass]
# File lib/epi/data.rb, line 127 def root? @is_root end
Save data to disk
# File lib/epi/data.rb, line 115 def save data_file.binwrite Marshal.dump hash data_file.chmod 0644 end
Write a file as UTF-8 @param file_name [String] Name of the file to write @param data [Object] Data
to be written to the file, or ‘nil` if the file should be deleted.
# File lib/epi/data.rb, line 91 def write(file_name, data) path = home + file_name if data.nil? path.delete if path.exist? nil else data = data.to_s path.parent.mkpath path.write data path.chmod 0644 data.length end end
Private Instance Methods
Ensures the home directory exists and is readable @raise [Fatal]
# File lib/epi/data.rb, line 143 def prepare_home! @is_root = @home.to_s == ROOT_HOME home.mkpath raise Fatal, 'We need write and execute permissions for ' << home.to_s unless home.exist? && home.readable? && home.executable? end