Sha256: 90017e4b8b1a1473e8b4a6136b022bb5c4f60865ddc1f371005455894952cfe4

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module SwitchPoint
  class Config
    attr_accessor :auto_writable
    alias_method :auto_writable?, :auto_writable

    def initialize
      self.auto_writable = false
    end

    def define_switch_point(name, config)
      assert_valid_config!(config)
      switch_points[name] = config
    end

    def switch_points
      @switch_points ||= {}
    end

    def database_name(name, mode)
      fetch(name)[mode]
    end

    def model_name(name, mode)
      if fetch(name)[mode]
        "#{name}_#{mode}".camelize
      end
    end

    def fetch(name)
      switch_points.fetch(name)
    end

    def keys
      switch_points.keys
    end

    def each_key(&block)
      switch_points.each_key(&block)
    end

    private

    def assert_valid_config!(config)
      unless config.key?(:readonly) || config.key?(:writable)
        raise ArgumentError.new(':readonly or :writable must be specified')
      end

      if config.key?(:readonly)
        unless config[:readonly].is_a?(Symbol)
          raise TypeError.new(":readonly's value must be Symbol")
        end
      end
      if config.key?(:writable)
        unless config[:writable].is_a?(Symbol)
          raise TypeError.new(":writable's value must be Symbol")
        end
      end
      nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
switch_point-0.9.0 lib/switch_point/config.rb