Class: Taskcmd::Storage
- Inherits:
-
Object
- Object
- Taskcmd::Storage
- Defined in:
- lib/taskcmd/storage.rb
Overview
Interact with local filesystem to store and load projects.
Constant Summary collapse
- DEFAULT_DIRECTORY_NAME =
'.taskcmd'- CONFIG_FILE_NAME =
'config'- CONFIG_FILE_EXT =
'msgpack'- PROJECT_FILE_EXT =
'project.msgpack'
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
Instance Method Summary collapse
- #delete_project(project_name) ⇒ Object
-
#initialize(dir = nil) ⇒ Storage
constructor
A new instance of Storage.
- #load_config ⇒ Object
- #load_project(name) ⇒ Object
- #project_exists?(name) ⇒ Boolean
- #project_file(name) ⇒ Object
- #project_name_list ⇒ Object
- #save_config(config) ⇒ Object
- #save_project(project) ⇒ Object
Constructor Details
#initialize(dir = nil) ⇒ Storage
Returns a new instance of Storage.
13 14 15 |
# File 'lib/taskcmd/storage.rb', line 13 def initialize(dir = nil) @dir = dir || default_directory end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
11 12 13 |
# File 'lib/taskcmd/storage.rb', line 11 def dir @dir end |
Instance Method Details
#delete_project(project_name) ⇒ Object
35 36 37 |
# File 'lib/taskcmd/storage.rb', line 35 def delete_project(project_name) File.delete(project_file(project_name)) end |
#load_config ⇒ Object
52 53 54 55 56 |
# File 'lib/taskcmd/storage.rb', line 52 def load_config return {} unless File.file?(config_file) data = File.read(config_file) MessagePack.unpack(data) end |
#load_project(name) ⇒ Object
23 24 25 26 |
# File 'lib/taskcmd/storage.rb', line 23 def load_project(name) data = File.read(project_file(name)) MessagePack.unpack(data) end |
#project_exists?(name) ⇒ Boolean
39 40 41 |
# File 'lib/taskcmd/storage.rb', line 39 def project_exists?(name) File.file?(project_file(name)) end |
#project_file(name) ⇒ Object
43 44 45 |
# File 'lib/taskcmd/storage.rb', line 43 def project_file(name) File.join(dir, "#{name}.#{PROJECT_FILE_EXT}") end |
#project_name_list ⇒ Object
17 18 19 20 21 |
# File 'lib/taskcmd/storage.rb', line 17 def project_name_list Dir.entries(dir) .select { |x| x.end_with?(".#{PROJECT_FILE_EXT}") } .map { |x| x.delete_suffix(".#{PROJECT_FILE_EXT}") } end |
#save_config(config) ⇒ Object
47 48 49 50 |
# File 'lib/taskcmd/storage.rb', line 47 def save_config(config) data = MessagePack.pack(config) File.write(config_file, data) end |
#save_project(project) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/taskcmd/storage.rb', line 28 def save_project(project) data = MessagePack.pack(project) path = project_file(project.name) File.write(path, data) path end |