Class: Natsy::Config
- Inherits:
-
Object
- Object
- Natsy::Config
- Defined in:
- lib/natsy/config.rb
Overview
Represents the configuration options for Natsy. Configuration options are set using the Natsy::Config::set method, either as arguments or by using the appropriate setters on the object passed to the block.
Defined Under Namespace
Classes: Options
Constant Summary collapse
- VALID_OPTIONS =
Valid option keys that can be given to
Natsy::Config::set, either in aHashpassed to the method, keyword arguments passed to the method, or by using setters on theNatsy::Config::Optionsobject passed to the block. %i[ url urls logger default_queue ].freeze
- DEFAULT_URL =
The default NATS server URL (used if none is configured)
"nats://localhost:4222"
Class Method Summary collapse
-
.as_json(*_args) ⇒ Object
Alias for to_h.
-
.default_queue ⇒ Object
The default queue that
natsyshould use for subscriptions. -
.logger ⇒ Object
The logger that
natsyshould use to write out logs for messages received, responses sent, errors raised, lifecycle events, etc. -
.reset! ⇒ Object
Reset the configuration to default values.
-
.set(keyword_options = {}) {|new_block_options_object| ... } ⇒ Object
Specify configuration options, either by providing them as keyword arguments or by using a block.
-
.to_h ⇒ Object
Returns all config options as a
Hash. -
.to_json(*_args) ⇒ Object
Serialize the configuration into a JSON object string.
-
.urls ⇒ Object
The NATS server URLs that
natsyshould listen on.
Class Method Details
.as_json(*_args) ⇒ Object
Alias for to_h.
236 237 238 |
# File 'lib/natsy/config.rb', line 236 def as_json(*_args) to_h end |
.default_queue ⇒ Object
The default queue that natsy should use for subscriptions.
See also: Natsy::Config::Options#default_queue=
222 223 224 |
# File 'lib/natsy/config.rb', line 222 def default_queue Utils.presence([:default_queue]) end |
.logger ⇒ Object
The logger that natsy should use to write out logs for messages received, responses sent, errors raised, lifecycle events, etc.
See also: Natsy::Config::Options#logger=
214 215 216 |
# File 'lib/natsy/config.rb', line 214 def logger Utils.presence([:logger]) end |
.reset! ⇒ Object
Reset the configuration to default values.
246 247 248 |
# File 'lib/natsy/config.rb', line 246 def reset! @given_options = nil end |
.set(keyword_options = {}) {|new_block_options_object| ... } ⇒ Object
Specify configuration options, either by providing them as keyword arguments or by using a block. Should you choose to set options using a block, it will be passed a single argument (an instance of Natsy::Config::Options). You can set any options on the instance that you see fit.
NOTE: The following two examples do exactly the same thing.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/natsy/config.rb', line 172 def set( = {}) = ( || {}).transform_keys(&:to_sym) invalid_config = lambda do |detail, keys| raise InvalidConfigError, "Invalid options provided #{detail}: #{keys.join(', ')}" end invalid_keys = invalid_option_keys() invalid_config.call("as arguments", invalid_keys) if invalid_keys.any? # Want to take advantage of the setters on +Natsy::Config::Options+... = .each_with_object(Options.new) do |(key, value), | .send(:"#{key}=", value) end .merge!(.to_h) = Options.new yield() if block_given? invalid_keys = invalid_option_keys() invalid_config.call("in block", invalid_keys) if invalid_keys.any? .merge!(.to_h) end |
.to_h ⇒ Object
Returns all config options as a Hash.
227 228 229 230 231 232 233 |
# File 'lib/natsy/config.rb', line 227 def to_h { urls: urls, logger: logger, default_queue: default_queue, } end |
.to_json(*_args) ⇒ Object
Serialize the configuration into a JSON object string.
241 242 243 |
# File 'lib/natsy/config.rb', line 241 def to_json(*_args) to_h.to_json end |
.urls ⇒ Object
The NATS server URLs that natsy should listen on.
See also: Natsy::Config::Options#urls=
202 203 204 205 206 207 |
# File 'lib/natsy/config.rb', line 202 def urls given_url_list = [[:url]].flatten given_urls_list = [[:urls]].flatten all_given_urls = [*given_url_list, *given_urls_list].compact.uniq Utils.presence(all_given_urls) || [DEFAULT_URL] end |