Sha256: e84f5b49c8c8fdb95b9a80f0aabca67e1c0dd980e74ca1d0011f0984730ca0d6

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Noticent
  module Definitions
    class Channel
      attr_reader :name
      attr_reader :group
      attr_reader :klass
      attr_reader :options

      def initialize(config, name, group: :default, klass: nil)
        raise BadConfiguration, 'name should be a symbol' unless name.is_a? Symbol
        raise BadConfiguration, '\'_any_\' is a reserved channel name' if name == :_any_
        raise BadConfiguration, '\'_none_\' is a reserved channel name' if name == :_none_

        @name = name
        @group = group
        @config = config

        sub_module = @config.use_sub_modules ? '::Channels::' : '::'
        suggested_class_name = @config.base_module_name + sub_module + name.to_s.camelize

        @klass = klass.nil? ? suggested_class_name.camelize.constantize : klass
      rescue NameError
        raise Noticent::BadConfiguration, "no class found for #{suggested_class_name}"
      end

      def using(options = {})
        @options = options
      end

      def instance(config, recipients, payload, context)
        inst = @klass.new(config, recipients, payload, context)
        return inst if @options.nil? || @options.empty?

        @options.each do |k, v|
          inst.send("#{k}=", v)
        rescue NoMethodError
          raise Noticent::BadConfiguration, "no method #{k}= found on #{@klass} as it is defined with the `using` clause"
        end

        inst
      rescue ArgumentError
        raise Noticent::BadConfiguration, "channel #{@klass} initializer arguments are mismatching."
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
noticent-0.0.6 lib/noticent/definitions/channel.rb
noticent-0.0.5 lib/noticent/definitions/channel.rb
noticent-0.0.4 lib/noticent/definitions/channel.rb
noticent-0.0.3 lib/noticent/definitions/channel.rb
noticent-0.0.2 lib/noticent/definitions/channel.rb
noticent-0.0.1 lib/noticent/definitions/channel.rb