Class: Epi::Data

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Exceptions
Defined in:
lib/epi/data.rb

Constant Summary collapse

ROOT_HOME =
'/etc/epi'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(home) ⇒ Data

Returns a new instance of Data.

Parameters:

  • home (String)

    The directory in which all working files should be stored.



75
76
77
78
# File 'lib/epi/data.rb', line 75

def initialize(home)
  @home = Pathname home
  prepare_home!
end

Instance Attribute Details

#homeObject (readonly)

Returns the value of attribute home.



72
73
74
# File 'lib/epi/data.rb', line 72

def home
  @home
end

Class Method Details

.configuration_pathsObject



20
21
22
# File 'lib/epi/data.rb', line 20

def configuration_paths
  self['configuration_paths'] ||= []
end

.default_instanceself

Get the default data storage instance

Returns:

  • (self)


34
35
36
# File 'lib/epi/data.rb', line 34

def default_instance
  @default_instance ||= new(detect_home_dir)
end

.jobsObject



24
25
26
# File 'lib/epi/data.rb', line 24

def jobs
  self['jobs'] ||= {}
end

.jobs=(value) ⇒ Object



28
29
30
# File 'lib/epi/data.rb', line 28

def jobs=(value)
  self['jobs'] = value
end

.reset!Object

Remove the default instance. Useful if the home path changes.



39
40
41
# File 'lib/epi/data.rb', line 39

def reset!
  @default_instance = nil
end

Instance Method Details

#[](key) ⇒ Object



131
132
133
# File 'lib/epi/data.rb', line 131

def [](key)
  hash[key.to_s]
end

#[]=(key, value) ⇒ Object



135
136
137
# File 'lib/epi/data.rb', line 135

def []=(key, value)
  hash[key.to_s] = value
end

#data_fileObject



105
106
107
# File 'lib/epi/data.rb', line 105

def data_file
  @data_file ||= home + 'data'
end

#hashObject



120
121
122
# File 'lib/epi/data.rb', line 120

def hash
  @hash ||= data_file.exist? ? Marshal.load(data_file.binread) : {}
end

#read(file_name) ⇒ String|NilClass

Read a file as UTF-8

Parameters:

  • file_name (String)

    Name of the file to read

Returns:

  • (String|NilClass)

    Contents of the file, or nil if the file doesn't exist.



83
84
85
86
# File 'lib/epi/data.rb', line 83

def read(file_name)
  path = home + file_name
  path.exist? ? path.read : nil
end

#reloadObject

Force reload of data from disk



110
111
112
# File 'lib/epi/data.rb', line 110

def reload
  @hash = nil
end

#root?TrueClass|FalseClass

Returns true if using root data at /etc/epi, or false if using user data is at ~/.epi

Returns:

  • (TrueClass|FalseClass)


127
128
129
# File 'lib/epi/data.rb', line 127

def root?
  @is_root
end

#saveObject

Save data to disk



115
116
117
118
# File 'lib/epi/data.rb', line 115

def save
  data_file.binwrite Marshal.dump hash
  data_file.chmod 0644
end

#write(file_name, data) ⇒ Object

Write a file as UTF-8

Parameters:

  • file_name (String)

    Name of the file to write

  • data (Object)

    Data to be written to the file, or nil if the file should be deleted.



91
92
93
94
95
96
97
98
99
100
101
102
103
# 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