Class: Noticent::Definitions::Scope

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name, payload_class: nil, check_constructor: true) ⇒ Scope

Returns a new instance of Scope.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/noticent/definitions/scope.rb', line 10

def initialize(config, name, payload_class: nil, check_constructor: true)
  @config = config
  @name = name
  @check_constructor = check_constructor

  sub_module = @config.use_sub_modules ? '::Payloads::' : '::'
  suggested_name = config.base_module_name + sub_module + "#{name.capitalize}Payload"
  @payload_class = payload_class.nil? ? suggested_name.constantize : payload_class
rescue NameError
  raise BadConfiguration, "scope #{suggested_name} class not found"
end

Instance Attribute Details

#check_constructorObject (readonly)

Returns the value of attribute check_constructor.



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

def check_constructor
  @check_constructor
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#payload_classObject (readonly)

Returns the value of attribute payload_class.



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

def payload_class
  @payload_class
end

Instance Method Details

#alert(name, constructor_name: nil, &block) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/noticent/definitions/scope.rb', line 22

def alert(name, constructor_name: nil, &block)
  alerts = @config.instance_variable_get(:@alerts) || {}

  raise BadConfiguration, "alert '#{name}' already defined" if alerts.include? name

  alert = Noticent::Definitions::Alert.new(@config, name: name, scope: self, constructor_name: constructor_name.nil? ? name : constructor_name)
  @config.hooks&.run(:pre_alert_registration, alert)
  alert.instance_eval(&block) if block_given?
  @config.hooks&.run(:post_alert_registration, alert)

  alerts[name] = alert

  @config.instance_variable_set(:@alerts, alerts)
  alert
end

#validate!Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/noticent/definitions/scope.rb', line 38

def validate!
  # klass is valid already as it's used in the initializer
  # does it have the right attributes?
  # fetch all alerts for this scope
  return if @payload_class.nil?

  @config.alerts_by_scope(name).each do |alert|
    next if alert.notifiers.nil?

    alert.notifiers.keys.each do |recipient|
      raise BadConfiguration, "payload class #{@payload_class} doesn't have a method or attribute called #{recipient}" unless @payload_class.method_defined? recipient
    end
  end

  raise BadConfiguration, "payload class #{@payload_class} does have an attribute or method called #{name}_id" if !@payload_class.nil? && !@payload_class.method_defined?("#{name}_id")
end