Sha256: 2fe386bbacc55e6d6d88f21080bd075eb2f2fa9d607870391e4f8316774f1641

Contents?: true

Size: 1.58 KB

Versions: 18

Compression:

Stored size: 1.58 KB

Contents

class Kontena::Callback

  attr_reader :command

  def initialize(command)
    @command = command
  end

  # Register callback for command types it is supposed to run with.
  def self.matches_commands(*commands)
    cmd_types = {}
    
    commands.each do |cmd|
      cmd_class, cmd_type = cmd.split(' ', 2)

      if cmd_class == '*'
        cmd_class = :all
      end

      if cmd_type.nil? || cmd_type == '*'
        cmd_type = :all
      else
        cmd_type = cmd_type.to_sym
      end
      cmd_types[cmd_class.to_sym] = cmd_type
    end

    # Finally it should be normalized into a hash that looks like :cmd_class => :cmd_type, :app => :init, :grid => :all
    cmd_types.each do |cmd_class, cmd_type|
      Kontena::Callback.callbacks[cmd_class] ||= {}
      Kontena::Callback.callbacks[cmd_class][cmd_type] ||= []
      Kontena::Callback.callbacks[cmd_class][cmd_type] << self
    end
  end

  def self.callbacks
    @@callbacks ||= {}
  end

  def self.run_callbacks(cmd_type, state, obj)
    [cmd_type.last, :all].compact.uniq.each do |cmdtype|
      [cmd_type.first, :all].compact.uniq.each do |cmdclass|
        callbacks.fetch(cmdclass, {}).fetch(cmdtype, []).each do |klass|
          if klass.instance_methods.include?(state)
            cb = klass.new(obj)
            if cb.send(state).kind_of?(FalseClass)
              ENV["DEBUG"] && STDERR.puts("Execution aborted by #{klass}")
              exit 1
            end
          end
        end
      end
    end
  end
end

Dir[File.expand_path('../callbacks/**/*.rb', __FILE__)].sort_by{ |file| File.basename(file) }.each { |file| require file }

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
kontena-cli-1.1.6 lib/kontena/callback.rb
kontena-cli-1.2.0.pre1 lib/kontena/callback.rb
kontena-cli-1.1.5 lib/kontena/callback.rb
kontena-cli-1.1.5.rc3 lib/kontena/callback.rb
kontena-cli-1.1.5.rc2 lib/kontena/callback.rb
kontena-cli-1.1.5.rc1 lib/kontena/callback.rb
kontena-cli-1.1.4 lib/kontena/callback.rb
kontena-cli-1.2.0.dev1 lib/kontena/callback.rb
kontena-cli-1.1.3 lib/kontena/callback.rb
kontena-cli-1.1.2 lib/kontena/callback.rb
kontena-cli-1.1.2.rc2 lib/kontena/callback.rb
kontena-cli-1.1.2.rc1 lib/kontena/callback.rb
kontena-cli-1.1.1 lib/kontena/callback.rb
kontena-cli-1.1.1.rc1 lib/kontena/callback.rb
kontena-cli-1.1.0 lib/kontena/callback.rb
kontena-cli-1.1.0.rc2 lib/kontena/callback.rb
kontena-cli-1.1.0.rc1 lib/kontena/callback.rb
kontena-cli-1.1.0.pre1 lib/kontena/callback.rb