Class: Noticent::Definitions::Alert

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

Defined Under Namespace

Classes: DefaultValue, Notifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name:, scope:, constructor_name:) ⇒ Alert

Returns a new instance of Alert.



13
14
15
16
17
18
19
20
# File 'lib/noticent/definitions/alert.rb', line 13

def initialize(config, name:, scope:, constructor_name:)
  @config = config
  @name = name
  @scope = scope
  @constructor_name = constructor_name
  @products = Noticent::Definitions::ProductGroup.new(@config)
  @defaults = { _any_: Noticent::Definitions::Alert::DefaultValue.new(self, :_any_, config.default_value) }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#constructor_nameObject (readonly)

Returns the value of attribute constructor_name.



11
12
13
# File 'lib/noticent/definitions/alert.rb', line 11

def constructor_name
  @constructor_name
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#notifiersObject (readonly)

Returns the value of attribute notifiers.



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

def notifiers
  @notifiers
end

#productsObject (readonly)

Returns the value of attribute products.



10
11
12
# File 'lib/noticent/definitions/alert.rb', line 10

def products
  @products
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#appliesObject



60
61
62
# File 'lib/noticent/definitions/alert.rb', line 60

def applies
  @products
end

#default(value, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/noticent/definitions/alert.rb', line 43

def default(value, &block)
  defaults = @defaults

  if block_given?
    default = Noticent::Definitions::Alert::DefaultValue.new(self, :_any_, value)
    default.instance_eval(&block)

    defaults[default.channel] = default
  else
    defaults[:_any_].value = value
  end

  @defaults = defaults

  default
end

#default_for(channel) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/noticent/definitions/alert.rb', line 33

def default_for(channel)
  raise ArgumentError, "no channel named '#{channel}' found" if @config.channels[channel].nil?

  @defaults[channel].nil? ? @defaults[:_any_].value : @defaults[channel].value
end

#default_valueObject



39
40
41
# File 'lib/noticent/definitions/alert.rb', line 39

def default_value
  @defaults[:_any_].value
end

#notify(recipient, template: '') ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
# File 'lib/noticent/definitions/alert.rb', line 22

def notify(recipient, template: '')
  notifiers = @notifiers || {}
  raise BadConfiguration, "a notify is already defined for '#{recipient}'" unless notifiers[recipient].nil?

  alert_notifier = Noticent::Definitions::Alert::Notifier.new(self, recipient, template: template)
  notifiers[recipient] = alert_notifier
  @notifiers = notifiers

  alert_notifier
end

#validate!Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/noticent/definitions/alert.rb', line 64

def validate!
  channels = @config.alert_channels(@name)
  raise BadConfiguration, "no notifiers are assigned to alert '#{@name}'" if @notifiers.nil? || @notifiers.empty?

  channels.each do |channel|
    raise BadConfiguration, "channel #{channel.name} (#{channel.klass}) has no method called #{@name}" unless channel.klass.method_defined? @name
  end

  # if a payload class is available, we can make sure it has a constructor with the name of the event
  raise Noticent::BadConfiguration, "payload #{@scope.payload_class} doesn't have a class method called #{name}" if @scope.check_constructor && !@scope.payload_class.respond_to?(@constructor_name)
end