class Epi::Data

Constants

ROOT_HOME

Attributes

home[R]

Public Class Methods

configuration_paths() click to toggle source
# File lib/epi/data.rb, line 20
def configuration_paths
  self['configuration_paths'] ||= []
end
default_instance() click to toggle source

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
jobs() click to toggle source
# File lib/epi/data.rb, line 24
def jobs
  self['jobs'] ||= {}
end
jobs=(value) click to toggle source
# File lib/epi/data.rb, line 28
def jobs=(value)
  self['jobs'] = value
end
new(home) click to toggle source

@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
reset!() click to toggle source

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

custom_home_dir() click to toggle source

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
detect_home_dir() click to toggle source

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
root_home_dir() click to toggle source

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
user_home_dir() click to toggle source

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

[](key) click to toggle source
# File lib/epi/data.rb, line 131
def [](key)
  hash[key.to_s]
end
[]=(key, value) click to toggle source
# File lib/epi/data.rb, line 135
def []=(key, value)
  hash[key.to_s] = value
end
data_file() click to toggle source
# File lib/epi/data.rb, line 105
def data_file
  @data_file ||= home + 'data'
end
hash() click to toggle source
# File lib/epi/data.rb, line 120
def hash
  @hash ||= data_file.exist? ? Marshal.load(data_file.binread) : {}
end
read(file_name) click to toggle source

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
reload() click to toggle source

Force reload of data from disk

# File lib/epi/data.rb, line 110
def reload
  @hash = nil
end
root?() click to toggle source

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() click to toggle source

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(file_name, data) click to toggle source

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

prepare_home!() click to toggle source

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