Module: Naplug::InstanceMethods

Defined in:
lib/naplug.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



194
195
196
197
198
199
200
201
202
# File 'lib/naplug.rb', line 194

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

Instance Attribute Details

#pluginsObject (readonly)

Returns the value of attribute plugins.



61
62
63
# File 'lib/naplug.rb', line 61

def plugins
  @plugins
end

Instance Method Details

#argsHash

Returns the arguments of the plugin

Returns:

  • (Hash)

    a hash by argument key of argument values



73
74
75
# File 'lib/naplug.rb', line 73

def args
  @_args
end

#args!(args) ⇒ Object

Sets and propagates plugin arguments

Parameters:

  • args (Hash <Symbol, Object>)


79
80
81
82
83
84
85
86
# File 'lib/naplug.rb', line 79

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) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/naplug.rb', line 128

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) ⇒ Object



119
120
121
# File 'lib/naplug.rb', line 119

def eval(tag = default_plugin.tag)
  @plugins[tag].eval
end

#eval!(tag = default_plugin.tag) ⇒ Object



123
124
125
126
# File 'lib/naplug.rb', line 123

def eval!(tag = default_plugin.tag)
  @plugins[tag].eval
  exit tag
end

#exec(t = default_plugin.tag) ⇒ Object

Execute the plugin

Parameters:

  • tag (Symbol)

    a plugin tag



110
111
112
113
114
115
116
117
# File 'lib/naplug.rb', line 110

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) ⇒ Object

Execute, evaluate and exit the plugin according to the plugin status, outputting the plugin’s text output (and performance data, if applicable)

Parameters:

  • tag (Symbol) (defaults to: default_plugin.tag)

    a plugin tag



102
103
104
105
106
# File 'lib/naplug.rb', line 102

def exec!(tag = default_plugin.tag)
  exec tag
  eval tag
  exit tag
end

#initialize(args = {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/naplug.rb', line 63

def initialize(args = {})
  @plugins = Hash.new
  plugins!

  @_args = Hash.new
  args! args
end

#perfdata(tag = default_plugin.tag) ⇒ Array<PerformanceData>

Returns a list of performance data objects.

Returns:



140
141
142
143
144
145
146
147
# File 'lib/naplug.rb', line 140

def perfdata(tag = default_plugin.tag)
  plugin = @plugins[tag]
  if plugin.has_plugins?
    plugin.plugins.values.select { |plug| plug.perfdata }.map { |plug| plug.perfdata }
  else
    plugin.perfdata ? [plugin.perfdata] : []
  end
end

#to_str(tag = default_plugin.tag) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/naplug.rb', line 88

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