Sha256: 9d86a1cc836bbc019360ab98fdc0b55b3ce9d78e091f33f6ceebb4f371a160c9

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

class Lbrt::Alert::DSL::Context::Alert
  include Lbrt::Utils::TemplateHelper

  REQUIRED_ATTRIBUTES = %w(
    description
    attributes
    active
    rearm_seconds
    rearm_per_signal
  )

  def initialize(context, name, services, &block)
    @context = context.merge(:alert_name => name)
    @name = name
    @services = services

    @result = {
      'attributes' => {},
      'conditions' => [],
      'services' => [],
    }

    instance_eval(&block)
  end

  attr_reader :context

  def result
    REQUIRED_ATTRIBUTES.each do |name|
      unless @result.has_key?(name)
        raise "Alert `#{@name}`: #{name} is not defined"
      end
    end

    @result
  end

  private

  def description(value)
    @result['description'] = value.to_s
  end

  def attributes(value)
    unless value.is_a?(Hash)
      raise TypeError, "wrong argument type #{value.class}: #{value.inspect} (expected Hash)"
    end

    @result['attributes'] = value
  end

  def active(value)
    @result['active'] = !!value
  end

  def rearm_seconds(value)
    @result['rearm_seconds'] = value.to_i
  end

  def rearm_per_signal(value)
    @result['rearm_per_signal'] = !!value
  end

  def condition(&block)
    @result['conditions'] << Lbrt::Alert::DSL::Context::Alert::Condition.new(@context, @name, &block).result
  end

  def service(type, title)
    service_key = [type, title]
    service = @services[service_key]

    unless service
      raise "Service `#{type}/#{title}` is not found"
    end

    service['title'] = title
    service['type'] = type
    @result['services'] << service
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lbrt-0.1.7 lib/lbrt/alert/dsl/context/alert.rb
lbrt-0.1.6 lib/lbrt/alert/dsl/context/alert.rb
lbrt-0.1.5 lib/lbrt/alert/dsl/context/alert.rb