Class: Timely::Config
- Inherits:
-
Object
- Object
- Timely::Config
- Defined in:
- lib/timely/config.rb
Instance Attribute Summary collapse
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #create_default_config ⇒ Object
- #get(key_path, default = nil) ⇒ Object
-
#initialize(config_path = TIMELY_CONFIG) ⇒ Config
constructor
A new instance of Config.
- #load_config ⇒ Object
- #reload ⇒ Object
- #save ⇒ Object
- #set(key_path, value) ⇒ Object
Constructor Details
#initialize(config_path = TIMELY_CONFIG) ⇒ Config
Returns a new instance of Config.
8 9 10 11 |
# File 'lib/timely/config.rb', line 8 def initialize(config_path = TIMELY_CONFIG) @config_path = config_path @settings = load_config end |
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
6 7 8 |
# File 'lib/timely/config.rb', line 6 def settings @settings end |
Instance Method Details
#[](key) ⇒ Object
78 79 80 |
# File 'lib/timely/config.rb', line 78 def [](key) @settings[key] end |
#[]=(key, value) ⇒ Object
82 83 84 |
# File 'lib/timely/config.rb', line 82 def []=(key, value) @settings[key] = value end |
#create_default_config ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/timely/config.rb', line 21 def create_default_config default = { 'version' => Timely::VERSION, 'location' => { 'lat' => 59.9139, 'lon' => 10.7522 }, 'timezone' => 'Europe/Oslo', 'default_view' => 'month', 'work_hours' => { 'start' => 8, 'end' => 17 }, 'week_starts_on' => 'monday', 'google' => { 'safe_dir' => '~/.config/timely/credentials', 'sync_interval' => 300 }, 'outlook' => { 'client_id' => '', 'tenant_id' => 'common' }, 'notifications' => { 'enabled' => true, 'default_alarm' => 15 } } save_config(default) default end |
#get(key_path, default = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/timely/config.rb', line 52 def get(key_path, default = nil) keys = key_path.to_s.split('.') value = @settings keys.each do |key| break unless value.is_a?(Hash) value = value[key] end value.nil? ? default : value end |
#load_config ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/timely/config.rb', line 13 def load_config if File.exist?(@config_path) YAML.load_file(@config_path) || create_default_config else create_default_config end end |
#reload ⇒ Object
74 75 76 |
# File 'lib/timely/config.rb', line 74 def reload @settings = load_config end |
#save ⇒ Object
70 71 72 |
# File 'lib/timely/config.rb', line 70 def save save_config end |
#set(key_path, value) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/timely/config.rb', line 62 def set(key_path, value) keys = key_path.to_s.split('.') last_key = keys.pop parent = @settings keys.each { |k| parent[k] ||= {}; parent = parent[k] } parent[last_key] = value end |