module Naplug::InstanceMethods

Attributes

plugins[R]

@!scope instancce

Public Class Methods

new(args = {}) click to toggle source
# File lib/naplug.rb, line 73
def initialize(args = {})
  @plugins = Hash.new
  plugins!

  @_args = Hash.new
  args! args
end

Public Instance Methods

args() click to toggle source

Returns the arguments of the plugin @return [Hash] a hash by argument key of argument values

# File lib/naplug.rb, line 83
def args
  @_args
end
args!(args) click to toggle source

Sets and propagates plugin arguments @param [Hash <Symbol, Object>] args

# File lib/naplug.rb, line 89
def args!(args)
  @_args.merge! args
  @plugins.each do |tag,plugin|
    plugin_args = args.key?(tag) ? args[tag] : {}
    shared_args = args.select { |t,a| not @plugins.keys.include? t }
    plugin.args! shared_args.merge! plugin_args
  end
end
eject!(payload = nil) click to toggle source
# File lib/naplug.rb, line 141
def eject!(payload = nil)
  o = case payload
        when String then payload
        when Exception then "#{payload.backtrace[1][/.+:\d+/]}: #{payload.message}"
        else nil
          caller[0][/.+:\d+/]
      end
  print "UNKNOWN: plugin eject! in %s\n" % [o]
  Kernel::exit 3
end
eval(tag = default_plugin.tag) click to toggle source
# File lib/naplug.rb, line 132
def eval(tag = default_plugin.tag)
  @plugins[tag].eval
end
eval!(tag = default_plugin.tag) click to toggle source
# File lib/naplug.rb, line 136
def eval!(tag = default_plugin.tag)
  @plugins[tag].eval
  exit tag
end
exec(t = default_plugin.tag) click to toggle source

Execute the plugin @param tag [Symbol] a plugin tag

# File lib/naplug.rb, line 123
def exec(t = default_plugin.tag)
  plugin = target_plugin t
  if plugin.has_plugins?
    plugin.plugins.each_value { |p| exec p }
  else
    plexec plugin
  end
end
exec!(tag = default_plugin.tag) click to toggle source

Execute, evaluate and exit the plugin according to the plugin status, outputting the plugin's text output (and performance data, if applicable) @param tag [Symbol] a plugin tag

# File lib/naplug.rb, line 112
def exec!(tag = default_plugin.tag)
  t = Benchmark.realtime do
    exec tag
    eval tag
  end
#  @plugins[tag].perfdata! "monitoring.#{File.basename($0)}.#{tag}", t if @plugins[tag].meta.benchmark
  exit tag
end
perfdata(tag = default_plugin.tag) click to toggle source

@return [Array<PerformanceData>] a list of performance data objects

# File lib/naplug.rb, line 153
def perfdata(tag = default_plugin.tag)
  @plugins[tag].perfdata(:deep).flatten.select { |pd| pd}
end
to_str(tag = default_plugin.tag) click to toggle source
# File lib/naplug.rb, line 98
def to_str(tag = default_plugin.tag)
  pd = perfdata(tag)
  if pd.empty?
    s_format = '%s: %s'
    s_array = [@plugins[tag].status,@plugins[tag].output]
  else
    s_format = '%s: %s | %s'
    s_array = [@plugins[tag].status,@plugins[tag].output,pd.join(' ').strip]
  end
  s_format % s_array
end

Private Instance Methods

default_plugin() click to toggle source
# File lib/naplug.rb, line 183
def default_plugin
  return @plugins[:main] if @plugins.key? :main
  return @plugins[@plugins.keys[0]] if @plugins.size == 1
  raise Naplug::Error, 'unable to determine default plugin'
end
exit(tag = default_plugin.tag) click to toggle source
# File lib/naplug.rb, line 197
def exit(tag = default_plugin.tag)
  print "%s\n" % [to_str(tag)]
  Kernel::exit @plugins[tag].status.to_i
end
method_missing(method, *args, &block) click to toggle source
# File lib/naplug.rb, line 202
def method_missing(method, *args, &block)
  message = "undefined instance variable or method #{method}"
  case @_runinng
    when nil?
      begin; raise Naplug::Error, message; rescue => e; eject! e ; end
    else
      raise Naplug::Error, message
  end
end
plexec(p) click to toggle source
# File lib/naplug.rb, line 159
def plexec(p)
  begin
    @_running = p.tag
    instance_exec p, &p.block
    @_running = nil
  rescue Naplug::Error => e
    p.status.unknown!
    p.output! "#{e.backtrace[1][/[^\/]+:\d+/]}: #{e.message}"
    p.payload! e
  rescue => e
    p.status.unknown!
    p.output!  "#{e.backtrace[0][/[^\/]+:\d+/]}: #{e.message}"
    p.payload! e
  ensure
    @_runinng = nil
  end
end
plugins!() click to toggle source
# File lib/naplug.rb, line 177
def plugins!
  self.class.plugins.each do |tag,plugin|
    @plugins[tag] = Plugin.new tag, plugin.block, plugin.meta.to_h.merge(:meta => false)
  end
end
respond_to_missing?(method, *) click to toggle source
Calls superclass method
# File lib/naplug.rb, line 212
def respond_to_missing?(method, *)
  @plugins.keys? method || super
end
target_plugin(target) click to toggle source
# File lib/naplug.rb, line 189
def target_plugin(target)
  case target
    when Symbol then @plugins[target]
    when Plugin then target
    else raise Naplug::Error, "unable to determine target plugin"
  end
end