Class: Epi::Data
- Inherits:
-
Object
- Object
- Epi::Data
- Extended by:
- Forwardable
- Includes:
- Exceptions
- Defined in:
- lib/epi/data.rb
Constant Summary collapse
- ROOT_HOME =
'/etc/epi'
Instance Attribute Summary collapse
-
#home ⇒ Object
readonly
Returns the value of attribute home.
Class Method Summary collapse
- .configuration_paths ⇒ Object
-
.default_instance ⇒ self
Get the default data storage instance.
- .jobs ⇒ Object
- .jobs=(value) ⇒ Object
-
.reset! ⇒ Object
Remove the default instance.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #data_file ⇒ Object
- #hash ⇒ Object
-
#initialize(home) ⇒ Data
constructor
A new instance of Data.
-
#read(file_name) ⇒ String|NilClass
Read a file as UTF-8.
-
#reload ⇒ Object
Force reload of data from disk.
-
#root? ⇒ TrueClass|FalseClass
Returns true if using root data at /etc/epi, or false if using user data is at ~/.epi.
-
#save ⇒ Object
Save data to disk.
-
#write(file_name, data) ⇒ Object
Write a file as UTF-8.
Constructor Details
#initialize(home) ⇒ Data
Returns a new instance of Data.
75 76 77 78 |
# File 'lib/epi/data.rb', line 75 def initialize(home) @home = Pathname home prepare_home! end |
Instance Attribute Details
#home ⇒ Object (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_paths ⇒ Object
20 21 22 |
# File 'lib/epi/data.rb', line 20 def configuration_paths self['configuration_paths'] ||= [] end |
.default_instance ⇒ self
Get the default data storage instance
34 35 36 |
# File 'lib/epi/data.rb', line 34 def default_instance @default_instance ||= new(detect_home_dir) end |
.jobs ⇒ Object
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_file ⇒ Object
105 106 107 |
# File 'lib/epi/data.rb', line 105 def data_file @data_file ||= home + 'data' end |
#hash ⇒ Object
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
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 |
#reload ⇒ Object
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
127 128 129 |
# File 'lib/epi/data.rb', line 127 def root? @is_root end |
#save ⇒ Object
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
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 |