Class: Naplug::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/naplug/plugin.rb

Defined Under Namespace

Classes: DuplicatePlugin

Constant Summary collapse

DEFAULT_META =
{ :debug => false, :enabled => true }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, block, meta = {}) ⇒ Plugin

Returns a new instance of Plugin.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/naplug/plugin.rb', line 17

def initialize(tag, block, meta = {})
  @tag = tag
  @block = block
  @plugins = Hash.new

  @_args = Hash.new
  @_data = OpenStruct.new :status => Status.new, :output => Output.new, :payload => nil, :perfdata => nil
  @_meta = OpenStruct.new DEFAULT_META.merge meta

  begin; instance_eval &block ; rescue => e; nil ; end

end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/naplug/plugin.rb', line 11

def block
  @block
end

#metaObject (readonly)

Returns the value of attribute meta.



11
12
13
# File 'lib/naplug/plugin.rb', line 11

def meta
  @meta
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



11
12
13
# File 'lib/naplug/plugin.rb', line 11

def plugins
  @plugins
end

#tagObject (readonly)

Returns the value of attribute tag.



11
12
13
# File 'lib/naplug/plugin.rb', line 11

def tag
  @tag
end

Instance Method Details

#[](k) ⇒ Object



122
123
124
# File 'lib/naplug/plugin.rb', line 122

def [](k)
  @_args[k]
end

#[]=(k, v) ⇒ Object



126
127
128
# File 'lib/naplug/plugin.rb', line 126

def []=(k,v)
  @_args[k] = v
end

#argsObject



109
110
111
# File 'lib/naplug/plugin.rb', line 109

def args
  @_args
end

#args!(args) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/naplug/plugin.rb', line 113

def args!(args)
  @_args.merge! args
  @plugins.each do |tag,plug|
    plug_args = args.key?(tag) ? args[tag] : {}
    shared_args = args.select { |t,a| not @plugins.keys.include? t }
    plug.args! shared_args.merge! plug_args
  end
end

#disable!Object

disable execution of the plugin; metaplugins cannot be disabled



41
42
43
# File 'lib/naplug/plugin.rb', line 41

def disable!
  is_meta? ? nil : @_meta.enabled = false
end

#enable!Object

enable execution of the plugin; metaplugins are always enabled



36
37
38
# File 'lib/naplug/plugin.rb', line 36

def enable!
  is_meta? ? nil : @_meta.enabled = true
end

#evalObject



134
135
136
137
138
139
140
141
# File 'lib/naplug/plugin.rb', line 134

def eval
  unless @plugins.empty?
    wcu_plugins = @plugins.values.select { |plug| plug.status.not_ok? }
    plugins = wcu_plugins.empty? ? @plugins.values : wcu_plugins
    output! plugins.map { |plug| "[#{plug.status.to_y}#{plug.tag} #{plug.output}]" }.join(' ')
    @_data.status = plugins.map { |plug| plug.status }.max
  end
end

#has_plugins?Boolean Also known as: has_plugs?

true when a plugin contains plugs

Returns:

  • (Boolean)


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

def has_plugins?
  @plugins.empty? ? false : true
end

#is_disabled?Boolean

true when the plugin is disabled; false otherwise

Returns:

  • (Boolean)


51
52
53
# File 'lib/naplug/plugin.rb', line 51

def is_disabled?
  not @_meta.enabled
end

#is_enabled?Boolean

true when plugin is enabled; false otherwise

Returns:

  • (Boolean)


46
47
48
# File 'lib/naplug/plugin.rb', line 46

def is_enabled?
  @_meta.enabled
end

#is_meta?True, False

Returns true if this plugin is a metaplugin, false otherwise.

Returns:

  • (True, False)

    true if this plugin is a metaplugin, false otherwise



31
32
33
# File 'lib/naplug/plugin.rb', line 31

def is_meta?
  @_meta.status
end

#long_outputObject



83
84
85
# File 'lib/naplug/plugin.rb', line 83

def long_output
  @_data.output.long_output
end

#long_output!(long_output) ⇒ Object



87
88
89
# File 'lib/naplug/plugin.rb', line 87

def long_output!(long_output)
  @_data.output.push long_output
end

#outputString

Gets plugin text output

Returns:

  • (String)

    plugin text output



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

def output
  @_data.output
end

#output!(text_output) ⇒ String

Sets plugin text output

Parameters:

  • text_output (String)

    plugin text output

Returns:

  • (String)

    new plugin text output



79
80
81
# File 'lib/naplug/plugin.rb', line 79

def output!(text_output)
  @_data.output.text_output = text_output
end

#parentObject



55
56
57
# File 'lib/naplug/plugin.rb', line 55

def parent
  @_meta.parent
end

#payloadObject



101
102
103
# File 'lib/naplug/plugin.rb', line 101

def payload
  @_data.payload
end

#payload!(p) ⇒ Object



105
106
107
# File 'lib/naplug/plugin.rb', line 105

def payload!(p)
  @_data.payload = p
end

#perfdataObject

returns the performance data of the plugin as a PerformanceData object



92
93
94
# File 'lib/naplug/plugin.rb', line 92

def perfdata
  @_data.perfdata
end

#perfdata!(label, value, f = {}) ⇒ Object



96
97
98
99
# File 'lib/naplug/plugin.rb', line 96

def perfdata!(label,value,f = {})
  @_data.perfdata ||= PerformanceData.new self
  @_data.perfdata[label] = value, f
end

#statusStatus

Returns plugin status.

Returns:



66
67
68
# File 'lib/naplug/plugin.rb', line 66

def status
  @_data.status
end

#to_strObject



130
131
132
# File 'lib/naplug/plugin.rb', line 130

def to_str
  '%s: %s' % [status,output]
end