Class: FlycalCli::Config
- Inherits:
-
Object
- Object
- FlycalCli::Config
- Defined in:
- lib/flycal_cli/config.rb
Constant Summary collapse
- CONFIG_DIR =
File.("~/.flycal")
- CONFIG_FILE =
File.join(CONFIG_DIR, "config.yml")
- CREDENTIALS_FILE =
File.join(CONFIG_DIR, "credentials.json")
- TOKENS_FILE =
File.join(CONFIG_DIR, "tokens.yml")
- DEFAULTS_FILE =
File.("../../config/defaults.yml", __dir__)
Class Method Summary collapse
- .calendar_default ⇒ Object
- .calendar_default=(calendar_id) ⇒ Object
- .clear_all ⇒ Object
- .config_dir ⇒ Object
- .config_file ⇒ Object
- .credentials_exist? ⇒ Boolean
- .credentials_path ⇒ Object
- .deep_dup(value) ⇒ Object
- .default_values ⇒ Object
- .exclude_calendars ⇒ Object
- .exclude_calendars=(calendar_ids) ⇒ Object
- .load ⇒ Object
- .locale ⇒ Object
- .locale=(value) ⇒ Object
- .merge_missing_defaults(target, defaults) ⇒ Object
- .normalize_slots_keys(data) ⇒ Object
- .save(data) ⇒ Object
- .slots_config ⇒ Object
- .tokens_path ⇒ Object
Class Method Details
.calendar_default ⇒ Object
34 35 36 37 38 39 |
# File 'lib/flycal_cli/config.rb', line 34 def calendar_default value = load["calendar_default"] return nil if value.nil? || value.to_s.strip.empty? || value.to_s == "~" value.to_s end |
.calendar_default=(calendar_id) ⇒ Object
41 42 43 44 45 |
# File 'lib/flycal_cli/config.rb', line 41 def calendar_default=(calendar_id) data = load data["calendar_default"] = calendar_id save(data) end |
.clear_all ⇒ Object
63 64 65 |
# File 'lib/flycal_cli/config.rb', line 63 def clear_all FileUtils.rm_rf(CONFIG_DIR) end |
.config_dir ⇒ Object
59 60 61 |
# File 'lib/flycal_cli/config.rb', line 59 def config_dir CONFIG_DIR end |
.config_file ⇒ Object
143 144 145 |
# File 'lib/flycal_cli/config.rb', line 143 def config_file CONFIG_FILE end |
.credentials_exist? ⇒ Boolean
51 52 53 |
# File 'lib/flycal_cli/config.rb', line 51 def credentials_exist? File.exist?(CREDENTIALS_FILE) end |
.credentials_path ⇒ Object
47 48 49 |
# File 'lib/flycal_cli/config.rb', line 47 def credentials_path CREDENTIALS_FILE end |
.deep_dup(value) ⇒ Object
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/flycal_cli/config.rb', line 183 def deep_dup(value) case value when Hash value.transform_values { |v| deep_dup(v) } when Array value.map { |v| deep_dup(v) } else value end end |
.default_values ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/flycal_cli/config.rb', line 157 def default_values @default_values ||= begin raise "Defaults file not found: #{DEFAULTS_FILE}" unless File.exist?(DEFAULTS_FILE) YAML.load_file(DEFAULTS_FILE) || {} end end |
.exclude_calendars ⇒ Object
71 72 73 |
# File 'lib/flycal_cli/config.rb', line 71 def exclude_calendars Array(slots_config["exclude_calendars"]).map(&:to_s).reject(&:empty?) end |
.exclude_calendars=(calendar_ids) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/flycal_cli/config.rb', line 75 def exclude_calendars=(calendar_ids) data = load data["slots"] ||= {} data["slots"]["exclude_calendars"] = Array(calendar_ids).map(&:to_s) save(data) end |
.load ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/flycal_cli/config.rb', line 15 def load user_data = if File.exist?(CONFIG_FILE) YAML.load_file(CONFIG_FILE) || {} else {} end merged, changed = merge_missing_defaults(user_data, default_values) merged, migrated = normalize_slots_keys(merged) save(merged) if changed || migrated || !File.exist?(CONFIG_FILE) merged end |
.locale ⇒ Object
147 148 149 |
# File 'lib/flycal_cli/config.rb', line 147 def locale load["locale"].to_s end |
.locale=(value) ⇒ Object
151 152 153 154 155 |
# File 'lib/flycal_cli/config.rb', line 151 def locale=(value) data = load data["locale"] = value.to_s save(data) end |
.merge_missing_defaults(target, defaults) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/flycal_cli/config.rb', line 165 def merge_missing_defaults(target, defaults) merged = target.dup changed = false defaults.each do |key, default_value| if !merged.key?(key) merged[key] = deep_dup(default_value) changed = true elsif default_value.is_a?(Hash) && merged[key].is_a?(Hash) nested, nested_changed = merge_missing_defaults(merged[key], default_value) merged[key] = nested changed ||= nested_changed end end [merged, changed] end |
.normalize_slots_keys(data) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/flycal_cli/config.rb', line 82 def normalize_slots_keys(data) slots = data["slots"] return [data, false] unless slots.is_a?(Hash) changed = false normalized = slots.dup if normalized.key?("exclude-calendars") && !normalized.key?("exclude_calendars") normalized["exclude_calendars"] = normalized.delete("exclude-calendars") changed = true end if normalized.key?("weekdays-only") && !normalized.key?("weekdays_only") normalized["weekdays_only"] = normalized.delete("weekdays-only") changed = true end if normalized.key?("workhours") && !normalized.key?("hours") normalized["hours"] = normalized.delete("workhours") changed = true end if normalized.key?("tempaltes") && !normalized.key?("templates") normalized["templates"] = normalized.delete("tempaltes") changed = true end templates = normalized["templates"] templates_empty = !templates.is_a?(Hash) || templates.empty? if templates_empty && normalized["hours"] days = if normalized.key?("weekdays_only") && normalized["weekdays_only"] == false [1, 2, 3, 4, 5, 6, 7] else [1, 2, 3, 4, 5] end normalized["templates"] = { "work" => { "days" => days, "hours" => deep_dup(normalized["hours"]) } } changed = true end %w[hours weekdays_only weekdays-only workhours tempaltes].each do |legacy_key| next unless normalized.key?(legacy_key) normalized.delete(legacy_key) changed = true end return [data, false] unless changed data = data.dup data["slots"] = normalized [data, true] end |
.save(data) ⇒ Object
29 30 31 32 |
# File 'lib/flycal_cli/config.rb', line 29 def save(data) FileUtils.mkdir_p(CONFIG_DIR) File.write(CONFIG_FILE, data.to_yaml) end |
.slots_config ⇒ Object
67 68 69 |
# File 'lib/flycal_cli/config.rb', line 67 def slots_config load.fetch("slots", {}) end |
.tokens_path ⇒ Object
55 56 57 |
# File 'lib/flycal_cli/config.rb', line 55 def tokens_path TOKENS_FILE end |