Sha256: d5fcacb303bf62703d8f4784d7931b21fd9f0d5759bc217f639914f330117f8f

Contents?: true

Size: 777 Bytes

Versions: 4

Compression:

Stored size: 777 Bytes

Contents

class Kommando::When
  VALID_EVENTS = :start, :retry, :timeout, :error, :exit

  def initialize
    @whens = {}
  end

  def register(event_name, block)
    event_name_as_sym = event_name.to_sym
    validate_event_name(event_name_as_sym)

    @whens[event_name_as_sym] = if @whens[event_name_as_sym]
      @whens[event_name_as_sym] << block
    else
      [block]
    end
  end

  def fire(event_name)
    event_name_as_sym = event_name.to_sym
    validate_event_name(event_name_as_sym)

    return unless blocks = @whens[event_name]

    blocks.each do |block|
      block.call
    end
  end

  private
  def validate_event_name(event_name_as_sym)
    raise Kommando::Error, "When '#{event_name_as_sym}' is not known." unless VALID_EVENTS.include? event_name_as_sym
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kommando-0.0.19 lib/kommando/when.rb
kommando-0.0.18 lib/kommando/when.rb
kommando-0.0.17 lib/kommando/when.rb
kommando-0.0.16 lib/kommando/when.rb