Method: Natsy::Config.set

Defined in:
lib/natsy/config.rb

.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.

Examples:

Natsy::Config.set(
  urls: ["nats://foo.bar:4567", "nats://foo.bar:5678"],
  default_queue: "foobar",
  logger: Rails.logger,
)
Natsy::Config.set do |options|
  options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
  options.default_queue = "foobar"
  options.logger = Rails.logger
end

Yields:

  • (new_block_options_object)


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(keyword_options = {})
  new_hash_options = (keyword_options || {}).transform_keys(&:to_sym)

  invalid_config = lambda do |detail, keys|
    raise InvalidConfigError, "Invalid options provided #{detail}: #{keys.join(', ')}"
  end

  invalid_keys = invalid_option_keys(new_hash_options)
  invalid_config.call("as arguments", invalid_keys) if invalid_keys.any?

  # Want to take advantage of the setters on +Natsy::Config::Options+...
  new_hash_options_object = new_hash_options.each_with_object(Options.new) do |(key, value), options|
    options.send(:"#{key}=", value)
  end

  given_options.merge!(new_hash_options_object.to_h)

  new_block_options_object = Options.new
  yield(new_block_options_object) if block_given?

  invalid_keys = invalid_option_keys(new_block_options_object)
  invalid_config.call("in block", invalid_keys) if invalid_keys.any?

  given_options.merge!(new_block_options_object.to_h)
end