Class: Fuso::Config
- Inherits:
-
Object
- Object
- Fuso::Config
- Defined in:
- lib/fuso/config.rb
Constant Summary collapse
- CONFIG_DIR =
File.join(Dir.home, ".fuso")
- CONFIG_FILE =
File.join(CONFIG_DIR, "config.json")
- DEFAULT =
{ "projects" => ["Project 1"], "categories" => ["Development", "Planning", "Meeting", "Support"], "default_project" => "Project 1", "default_category" => "Development" }.freeze
Instance Attribute Summary collapse
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#default_category ⇒ Object
Returns the value of attribute default_category.
-
#default_project ⇒ Object
Returns the value of attribute default_project.
-
#projects ⇒ Object
Returns the value of attribute projects.
Class Method Summary collapse
Instance Method Summary collapse
- #add_category(name) ⇒ Object
- #add_project(name) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #remove_category(name) ⇒ Object
- #remove_project(name) ⇒ Object
- #save ⇒ Object
- #set_default_category(name) ⇒ Object
- #set_default_project(name) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
20 21 22 23 24 25 |
# File 'lib/fuso/config.rb', line 20 def initialize @projects = [] @categories = [] @default_project = nil @default_category = nil end |
Instance Attribute Details
#categories ⇒ Object
Returns the value of attribute categories.
18 19 20 |
# File 'lib/fuso/config.rb', line 18 def categories @categories end |
#default_category ⇒ Object
Returns the value of attribute default_category.
18 19 20 |
# File 'lib/fuso/config.rb', line 18 def default_category @default_category end |
#default_project ⇒ Object
Returns the value of attribute default_project.
18 19 20 |
# File 'lib/fuso/config.rb', line 18 def default_project @default_project end |
#projects ⇒ Object
Returns the value of attribute projects.
18 19 20 |
# File 'lib/fuso/config.rb', line 18 def projects @projects end |
Class Method Details
.load ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fuso/config.rb', line 27 def self.load config = new FileUtils.mkdir_p(CONFIG_DIR) if File.exist?(CONFIG_FILE) data = JSON.parse(File.read(CONFIG_FILE)) else data = { "projects" => DEFAULT["projects"].dup, "categories" => DEFAULT["categories"].dup, "default_project" => DEFAULT["default_project"], "default_category" => DEFAULT["default_category"] } File.write(CONFIG_FILE, JSON.pretty_generate(data)) end config.projects = (data["projects"] || []).dup config.categories = (data["categories"] || []).dup config.default_project = data["default_project"] config.default_category = data["default_category"] config end |
Instance Method Details
#add_category(name) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/fuso/config.rb', line 71 def add_category(name) name = name.strip return false if name.empty? || @categories.include?(name) @categories << name @default_category ||= name save true end |
#add_project(name) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/fuso/config.rb', line 61 def add_project(name) name = name.strip return false if name.empty? || @projects.include?(name) @projects << name @default_project ||= name save true end |
#remove_category(name) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/fuso/config.rb', line 91 def remove_category(name) return false unless @categories.include?(name) return false if @categories.size <= 1 @categories.delete(name) @default_category = @categories.first if @default_category == name save true end |
#remove_project(name) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/fuso/config.rb', line 81 def remove_project(name) return false unless @projects.include?(name) return false if @projects.size <= 1 @projects.delete(name) @default_project = @projects.first if @default_project == name save true end |
#save ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fuso/config.rb', line 50 def save FileUtils.mkdir_p(CONFIG_DIR) data = { "projects" => @projects, "categories" => @categories, "default_project" => @default_project, "default_category" => @default_category } File.write(CONFIG_FILE, JSON.pretty_generate(data)) end |
#set_default_category(name) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/fuso/config.rb', line 109 def set_default_category(name) return false unless @categories.include?(name) @default_category = name save true end |
#set_default_project(name) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/fuso/config.rb', line 101 def set_default_project(name) return false unless @projects.include?(name) @default_project = name save true end |