Sha256: 3b3195e60bfa5814a70df6f1f59358305efb57745c825bc2c6530250cd81f714

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

class NestedConfig
  # Test helper for modifying config inside a block.
  #
  # == Example
  #
  #   require 'nested_config/with_config'
  #
  #   class MyCase < MiniTest::TestCase
  #     include NestedConfig::WithConfig
  #
  #     def app_config
  #       MyApp.config # global
  #     end
  #   end
  #
  #   class SomeCase < MyCase
  #     def setup
  #       app_config.tap do |config|
  #         config.coins = 1000
  #         config.queue do |queue|
  #           queue.workers do |workers|
  #             workers.max = 5
  #           end
  #         end
  #       end
  #     end
  #
  #     def test_with_basic_value
  #       with_config(app_config) do |config|
  #         config.coins = 500
  #       end
  #       # global config reset to previous config
  #     end
  #
  #     def test_queue_with_changed_workers
  #       with_config(app_config, :queue, :workers) do |workers|
  #         workers.max = 1
  #         # do stuff with modified config max value
  #       end
  #       # global config reset to previous config
  #     end
  #   end
  module WithConfig
    def with_config(config, *keys, &block)
      current = config
      while key = keys.shift
        current = current[key]
        unless current
          raise ArgumentError, "nested key #{key.inspect} not found"
        end
      end
      unless current.respond_to?(:__with_cloned__)
        raise ArgumentError, "config #{current.inspect} can't be cloned"
      end
      current.__with_cloned__(&block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nested_config-0.4.0 lib/nested_config/with_config.rb