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 }
VALID_META_OPTIONS =
[ :debug, :state, :description, :parent ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, block, meta) ⇒ Plugin

Returns a new instance of Plugin.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/naplug/plugin.rb', line 18

def initialize(tag, block, meta)
  validate_meta_options 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 ArgumentError => e
    raise
  rescue
    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



135
136
137
# File 'lib/naplug/plugin.rb', line 135

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

#[]=(k, v) ⇒ Object



139
140
141
# File 'lib/naplug/plugin.rb', line 139

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

#argsObject



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

def args
  @_args
end

#args!(args) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/naplug/plugin.rb', line 126

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

#descriptionObject



68
69
70
# File 'lib/naplug/plugin.rb', line 68

def description
  @_meta.description
end

#disable!Object

disable execution of the plugin; metaplugins cannot be disabled



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

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

#enable!Object

enable execution of the plugin; metaplugins are always enabled



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

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

#evalObject



147
148
149
150
151
152
153
154
# File 'lib/naplug/plugin.rb', line 147

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)


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

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

#is_disabled?Boolean

true when the plugin is disabled; false otherwise

Returns:

  • (Boolean)


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

def is_disabled?
  not @_meta.enabled
end

#is_enabled?Boolean

true when plugin is enabled; false otherwise

Returns:

  • (Boolean)


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

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



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

def is_meta?
  @_meta.status
end

#long_outputObject



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

def long_output
  @_data.output.long_output
end

#long_output!(long_output) ⇒ Object



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

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

#outputString

Gets plugin text output

Returns:

  • (String)

    plugin text output



85
86
87
# File 'lib/naplug/plugin.rb', line 85

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



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

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

#parentObject



64
65
66
# File 'lib/naplug/plugin.rb', line 64

def parent
  @_meta.parent
end

#payloadObject



114
115
116
# File 'lib/naplug/plugin.rb', line 114

def payload
  @_data.payload
end

#payload!(p) ⇒ Object



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

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

#perfdataObject

returns the performance data of the plugin as a PerformanceData object



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

def perfdata
  @_data.perfdata
end

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



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

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

#statusStatus

Returns plugin status.

Returns:



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

def status
  @_data.status
end

#to_strObject



143
144
145
# File 'lib/naplug/plugin.rb', line 143

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