Sha256: b1fc6b398c439d4072ef11973534691f304f02a7d3a406abf1de0be82482c7c1

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# rubocop:ignore Style/Documentation
module ActiveSMS
  def self.config
    @@config ||= Configuration.new
  end

  def self.configure
    yield(config)
  end

  def self.reset!
    @@config = nil
  end

  # TODO: Documentation
  # rubocop:ignore Style/Documentation
  class Configuration
    attr_reader :default_backend
    attr_reader :backends

    def initialize
      register_backend :null_sender, ActiveSMS::Backend::NullSender
      self.default_backend = :null_sender
    end

    def default_backend=(value)
      raise ArgumentError, "default_backend must be a symbol!" unless value.is_a? Symbol

      unless @backends.keys.include? value
        raise ArgumentError, "Unregistered backend cannot be set as default!"
      end

      @default_backend = value
    end

    def register_backend(key, classname, params = {})
      raise ArgumentError, "backend key must be a symbol!" unless key.is_a? Symbol

      unless classname.class == Class
        raise ArgumentError, "backend class must be class (not instance or string)"
      end

      unless classname.method_defined? :send_sms
        raise ArgumentError, "backend must provide method send_sms"
      end

      define_backend(key, classname, params)
    end

    def remove_backend(key)
      if key == default_backend
        raise ArgumentError, "Removing default_backend is prohibited"
      end

      @backends.delete key
      true
    end

    private

    def define_backend(key, classname, params)
      @backends ||= {}
      @backends[key] = {
        class: classname,
        params: params
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_sms-0.0.1 lib/active_sms/configuration.rb