Class: CalendarAssistant::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/calendar_assistant/config.rb,
lib/calendar_assistant/config/token_store.rb

Direct Known Subclasses

CalendarAssistant::CLI::Config

Defined Under Namespace

Modules: Keys Classes: AccessingHashAsScalar, NoTokensAuthorized, TokenStore

Constant Summary collapse

DEFAULT_CALENDAR_ID =
"primary"
DEFAULT_SETTINGS =
{
  Keys::Settings::LOCATION_ICON => "🌎",              # string emoji
  Keys::Settings::MEETING_LENGTH => "30m",            # ChronicDuration
  Keys::Settings::START_OF_DAY => "9am",              # BusinessTime
  Keys::Settings::END_OF_DAY => "6pm",                # BusinessTime
  Keys::Options::CALENDARS => [DEFAULT_CALENDAR_ID],  # array of calendar ids
  Keys::Options::FORMATTING => true,                  # Rainbow
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: {}, user_config: {}, defaults: DEFAULT_SETTINGS) ⇒ Config

Returns a new instance of Config.



61
62
63
64
65
66
67
# File 'lib/calendar_assistant/config.rb', line 61

def initialize(options: {},
               user_config: {},
               defaults: DEFAULT_SETTINGS)
  @defaults = defaults
  @options = options
  @user_config = user_config
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



59
60
61
# File 'lib/calendar_assistant/config.rb', line 59

def defaults
  @defaults
end

#optionsObject (readonly)

Returns the value of attribute options.



59
60
61
# File 'lib/calendar_assistant/config.rb', line 59

def options
  @options
end

#user_configObject (readonly)

Returns the value of attribute user_config.



59
60
61
# File 'lib/calendar_assistant/config.rb', line 59

def user_config
  @user_config
end

Instance Method Details

#calendar_idsObject

helper method for Keys::Options::CALENDARS



151
152
153
# File 'lib/calendar_assistant/config.rb', line 151

def calendar_ids
  split_if_array(Keys::Options::CALENDARS)
end

#debug?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/calendar_assistant/config.rb', line 163

def debug?
  setting(Keys::Options::DEBUG)
end

#event_visibilityObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/calendar_assistant/config.rb', line 171

def event_visibility
  value = setting(Keys::Settings::VISIBILITY)
  case value
  when CalendarAssistant::Event::Visibility::PUBLIC
    CalendarAssistant::Event::Visibility::PUBLIC
  when CalendarAssistant::Event::Visibility::PRIVATE
    CalendarAssistant::Event::Visibility::PRIVATE
  else
    CalendarAssistant::Event::Visibility::DEFAULT
  end
end

#get(keypath) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/calendar_assistant/config.rb', line 101

def get(keypath)
  rval = Config.find_in_hash(user_config, keypath)

  if rval.is_a?(Hash)
    raise CalendarAssistant::Config::AccessingHashAsScalar, "keypath #{keypath} is not a scalar"
  end

  rval
end

#in_env(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/calendar_assistant/config.rb', line 69

def in_env(&block)
  # this is totally not thread-safe
  orig_b_o_d = BusinessTime::Config.beginning_of_workday
  orig_e_o_d = BusinessTime::Config.end_of_workday
  begin
    BusinessTime::Config.beginning_of_workday = setting(Config::Keys::Settings::START_OF_DAY)
    BusinessTime::Config.end_of_workday = setting(Config::Keys::Settings::END_OF_DAY)
    yield
  ensure
    BusinessTime::Config.beginning_of_workday = orig_b_o_d
    BusinessTime::Config.end_of_workday = orig_e_o_d
  end
end

#must_beObject



155
156
157
# File 'lib/calendar_assistant/config.rb', line 155

def must_be
  split_if_array(Keys::Options::MUST_BE)
end

#must_not_beObject



159
160
161
# File 'lib/calendar_assistant/config.rb', line 159

def must_not_be
  split_if_array(Keys::Options::MUST_NOT_BE)
end

#persist!Object



167
168
169
# File 'lib/calendar_assistant/config.rb', line 167

def persist!
  #noop
end

#profile_nameObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/calendar_assistant/config.rb', line 83

def profile_name
  # CLI option takes precedence
  return options[Keys::Settings::PROFILE] if options[Keys::Settings::PROFILE]

  # then a configured preference takes precedence
  default = get([Keys::SETTINGS, Keys::Settings::PROFILE])
  return default if default

  # finally we'll grab the first configured token and set that as the default
  token_names = tokens.keys
  if token_names.empty?
    raise CalendarAssistant::Config::NoTokensAuthorized, "Please run `calendar-assistant help authorize` for help."
  end
  token_names.first.tap do |new_default|
    Config.set_in_hash user_config, [Keys::SETTINGS, Keys::Settings::PROFILE], new_default
  end
end

#set(keypath, value) ⇒ Object



111
112
113
# File 'lib/calendar_assistant/config.rb', line 111

def set(keypath, value)
  Config.set_in_hash user_config, keypath, value
end

#setting(setting_name) ⇒ Object Also known as: []

note that, despite the name, this method returns both options

and settings


119
120
121
122
123
124
125
# File 'lib/calendar_assistant/config.rb', line 119

def setting(setting_name)
  context = Config.find_in_hash(options, Keys::Options::CONTEXT)
  Config.find_in_hash(options, setting_name) ||
    Config.find_in_hash(user_config, [Keys::SETTINGS, context, setting_name]) ||
    Config.find_in_hash(user_config, [Keys::SETTINGS, setting_name]) ||
    Config.find_in_hash(defaults, setting_name)
end

#settingsObject



129
130
131
132
133
134
135
136
137
# File 'lib/calendar_assistant/config.rb', line 129

def settings
  setting_names = CalendarAssistant::Config::Keys::Settings.constants.map do |k|
    CalendarAssistant::Config::Keys::Settings.const_get k
  end
  setting_names.inject({}) do |settings, key|
    settings[key] = setting key
    settings
  end
end

#token_storeObject



144
145
146
# File 'lib/calendar_assistant/config.rb', line 144

def token_store
  CalendarAssistant::Config::TokenStore.new self
end

#tokensObject



139
140
141
142
# File 'lib/calendar_assistant/config.rb', line 139

def tokens
  Config.find_in_hash(user_config, Keys::TOKENS) ||
    Config.set_in_hash(user_config, Keys::TOKENS, {})
end