class Aruba::Platforms::Announcer

Announcer

@private

@example Activate your you own channel in cucumber

Before('@announce-my-channel') do
  aruba.announcer.activate :my_channel
end

@example Activate your you own channel in rspec > 3

before do
  current_example = context.example
  aruba.announcer.activate :my_channel if current_example.metadata[:announce_my_channel]
end

Aruba.announcer.announce(:my_channel, 'my message')

Attributes

announcer[R]
announcers[R]
channels[R]
colorizer[R]
output_formats[R]

Public Class Methods

new(*args) click to toggle source
# File lib/aruba/platforms/announcer.rb, line 58
def initialize(*args)
  @announcers = []
  @announcers << PutsAnnouncer.new
  @announcers << KernelPutsAnnouncer.new

  @colorizer = Aruba::Colorizer.new

  @announcer         = @announcers.first
  @channels          = {}
  @output_formats    = {}

  @options           = args[1] || {}

  after_init
end

Public Instance Methods

activate(*chns) click to toggle source

Activate a channel

@param [Symbol] channel

The name of the channel to activate
# File lib/aruba/platforms/announcer.rb, line 159
def activate(*chns)
  chns.flatten.each { |c| channels[c.to_sym] = true }

  self
end
activated?(channel) click to toggle source

Check if channel is activated

@param [Symbol] channel

The name of the channel to check
# File lib/aruba/platforms/announcer.rb, line 151
def activated?(channel)
  channels[channel.to_sym] == true
end
announce(channel, *args) { || ... } click to toggle source

Announce information to channel

@param [Symbol] channel

The name of the channel to check

@param [Array] args

Arguments

@yield

If block is given, that one is called and the return value is used as
message to be announced.
# File lib/aruba/platforms/announcer.rb, line 176
def announce(channel, *args, &block)
  channel = channel.to_sym

  the_output_format = if output_formats.key? channel
                        output_formats[channel]
                      else
                        proc { |v| format('%s', v) }
                      end

  return unless activated?(channel)

  begin
    if block_given?
      value = yield
      args << value
    end

    message = the_output_format.call(*args)
    message += "\n"
    message = colorizer.cyan(message)
  rescue NotImplementedError => e
    message = "Error fetching announced value for #{channel}: #{e.message}"
  end

  announcer.announce(message)

  nil
end
cmd(cmd) click to toggle source

@deprecated

# File lib/aruba/platforms/announcer.rb, line 224
def cmd(cmd)
  warn('The announcer now has a new api to activate channels. Please use this one announce(:command, message)')
  announce :command, cmd
end
dir(dir) click to toggle source

@deprecated

# File lib/aruba/platforms/announcer.rb, line 218
def dir(dir)
  warn('The announcer now has a new api to activate channels. Please use this one announce(:directory, message)')
  announce :directory, dir
end
env(name, value) click to toggle source

@deprecated

# File lib/aruba/platforms/announcer.rb, line 230
def env(name, value)
  warn('The announcer now has a new api to activate channels. Please use this one: announce(:changed_environment, key, value)')

  announce :changed_environment, name, value
end
mode=(m) click to toggle source

Change mode of announcer

@param [Symbol] m

The mode to set
# File lib/aruba/platforms/announcer.rb, line 141
def mode=(m)
  @announcer = @announcers.find { |a| f.mode? m.to_sym }

  self
end
reset() click to toggle source

Reset announcer

# File lib/aruba/platforms/announcer.rb, line 133
def reset
  @announcer = @announcers.first
end
stderr(content) click to toggle source

@deprecated

# File lib/aruba/platforms/announcer.rb, line 212
def stderr(content)
  warn('The announcer now has a new api to activate channels. Please use this one: announce(:stderr, message)')
  announce :stderr, content
end
stdout(content) click to toggle source

@deprecated

# File lib/aruba/platforms/announcer.rb, line 206
def stdout(content)
  warn('The announcer now has a new api to activate channels. Please use this one: announce(:stdout, message)')
  announce :stdout, content
end

Private Instance Methods

after_init() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/aruba/platforms/announcer.rb, line 77
def after_init
  output_format :changed_configuration, proc { |n, v| format('# %s = %s', n, v) }
  output_format :changed_environment, proc { |n, v| format('$ export %s=%s', n, Shellwords.escape(v)) }
  output_format :command, '$ %s'
  output_format :directory, '$ cd %s'
  output_format :environment, proc { |n, v| format('$ export %s=%s', n, Shellwords.escape(v)) }
  output_format :full_environment, proc { |h| format("<<-ENVIRONMENT\n%s\nENVIRONMENT", Aruba.platform.simple_table(h)) }
  output_format :modified_environment, proc { |n, v| format('$ export %s=%s', n, Shellwords.escape(v)) }
  output_format :stderr, "<<-STDERR\n%s\nSTDERR"
  output_format :stdout, "<<-STDOUT\n%s\nSTDOUT"
  output_format :command_content, "<<-COMMAND\n%s\nCOMMAND"
  output_format :stop_signal, proc { |p, s| format('Command will be stopped with `kill -%s %s`', s, p) }
  output_format :timeout, '# %s-timeout: %s seconds'
  output_format :wait_time, '# %s: %s seconds'
  output_format :command_filesystem_status, proc { |status|
    format("<<-COMMAND FILESYSTEM STATUS\n%s\nCOMMAND FILESYSTEM STATUS",
           Aruba.platform.simple_table(status.to_h, :sort => false)) }

  # rubocop:disable Metrics/LineLength
  if @options[:stdout]
    warn('The use of "@announce_stdout-instance" variable and "options[:stdout] = true" for Announcer.new is deprecated. Please use "announcer.activate(:stdout)" instead.')
    activate :stdout
  end
  if @options[:stderr]
    warn('The use of "@announce_stderr-instance" variable and "options[:stderr] = true" for Announcer.new is deprecated. Please use "announcer.activate(:stderr)" instead.')
    activate :stderr
  end
  if @options[:dir]
    warn('The use of "@announce_dir-instance" variable and "options[:dir] = true" for Announcer.new is deprecated. Please use "announcer.activate(:directory)" instead.')
    activate :directory
  end
  if @options[:cmd]
    warn('The use of "@announce_cmd-instance" variable and "options[:cmd] = true" for Announcer.new is deprecated. Please use "announcer.activate(:command)" instead.')
    activate :command
  end
  if @options[:env]
    warn('The use of "@announce_env-instance" variable and "options[:env] = true" for Announcer.new is deprecated. Please use "announcer.activate(:modified_environment)" instead.')
    activate :modified_enviroment
  end
  # rubocop:enable Metrics/LineLength
end
output_format(channel, string = '%s', &block) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/aruba/platforms/announcer.rb, line 120
def output_format(channel, string = '%s', &block)
  output_formats[channel.to_sym] = if block_given?
                                     block
                                   elsif string.is_a?(Proc)
                                     string
                                   else
                                     proc { |*args| format(string, *args) }
                                   end
end