Sha256: 1bb4b8cd454ffe55889b3d973d8db0b317e2dcb6f2e28d83ba4e9d8b51692c79

Contents?: true

Size: 1.53 KB

Versions: 16

Compression:

Stored size: 1.53 KB

Contents

module ROM
  # @api private
  module Configurable
    class Config
      WRITER_REGEXP = /=$/.freeze

      attr_reader :settings

      # @api private
      def initialize(settings = {})
        @settings = settings
      end

      # @api public
      def [](name)
        public_send(name)
      end

      # @api private
      def key?(name)
        settings.key?(name)
      end

      # @api private
      def freeze
        settings.each_value(&:freeze)
        super
      end

      # @api private
      def respond_to_missing?(_name, _include_private = false)
        true
      end
      
      def dup
        self.class.new(dup_settings(settings))
      end
        
      private
      
      def dup_settings(settings)
        settings.each_with_object({}) do |(key, value), new_settings|
          if value.is_a?(self.class)
            new_settings[key] = value.dup
          else
            new_settings[key] = value
          end
        end
      end

      # @api private
      def method_missing(meth, *args, &_block)
        return settings.fetch(meth, nil) if frozen?

        name = meth.to_s
        key = name.gsub(WRITER_REGEXP, '').to_sym

        if writer?(name)
          settings[key] = args.first
        else
          settings.fetch(key) { settings[key] = self.class.new }
        end
      end

      # @api private
      def writer?(name)
        !WRITER_REGEXP.match(name).nil?
      end
    end

    def config
      @config ||= Config.new
    end

    # @api public
    def configure
      yield(config)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rom-3.0.3 lib/rom/support/configurable.rb
rom-3.0.2 lib/rom/support/configurable.rb
rom-3.0.1 lib/rom/support/configurable.rb
rom-3.0.0 lib/rom/support/configurable.rb
rom-3.0.0.rc2 lib/rom/support/configurable.rb
rom-3.0.0.rc1 lib/rom/support/configurable.rb
rom-3.0.0.beta3 lib/rom/support/configurable.rb
rom-3.0.0.beta2 lib/rom/support/configurable.rb
rom-3.0.0.beta1 lib/rom/support/configurable.rb
rom-2.0.2 lib/rom/support/configurable.rb
rom-2.0.1 lib/rom/support/configurable.rb
rom-2.0.0 lib/rom/support/configurable.rb
rom-1.0.0 lib/rom/support/configurable.rb
rom-1.0.0.rc1 lib/rom/support/configurable.rb
rom-1.0.0.beta2 lib/rom/support/configurable.rb
rom-1.0.0.beta1 lib/rom/support/configurable.rb