Class: Noticent::Definitions::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/noticent/definitions/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name, group: :default, klass: nil) ⇒ Channel

Returns a new instance of Channel.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/noticent/definitions/channel.rb', line 11

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

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



7
8
9
# File 'lib/noticent/definitions/channel.rb', line 7

def group
  @group
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/noticent/definitions/channel.rb', line 8

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/noticent/definitions/channel.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/noticent/definitions/channel.rb', line 9

def options
  @options
end

Instance Method Details

#instance(config, recipients, payload, context) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/noticent/definitions/channel.rb', line 32

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

#using(options = {}) ⇒ Object



28
29
30
# File 'lib/noticent/definitions/channel.rb', line 28

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