Class: Naplug::Plugin
- Inherits:
-
Object
- Object
- Naplug::Plugin
- Defined in:
- lib/naplug/plugin.rb
Defined Under Namespace
Classes: DuplicatePlugin
Constant Summary collapse
- DEFAULT_META =
{ :debug => false, :enabled => true }
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #args ⇒ Object
- #args!(args) ⇒ Object
-
#disable! ⇒ Object
disable execution of the plugin; metaplugins cannot be disabled.
-
#enable! ⇒ Object
enable execution of the plugin; metaplugins are always enabled.
- #eval ⇒ Object
-
#has_plugins? ⇒ Boolean
(also: #has_plugs?)
true when a plugin contains plugs.
-
#initialize(tag, block, meta = {}) ⇒ Plugin
constructor
A new instance of Plugin.
-
#is_disabled? ⇒ Boolean
true when the plugin is disabled; false otherwise.
-
#is_enabled? ⇒ Boolean
true when plugin is enabled; false otherwise.
-
#is_meta? ⇒ True, False
True if this plugin is a metaplugin, false otherwise.
- #long_output ⇒ Object
- #long_output!(long_output) ⇒ Object
-
#output ⇒ String
Gets plugin text output.
-
#output!(text_output) ⇒ String
Sets plugin text output.
- #parent ⇒ Object
- #payload ⇒ Object
- #payload!(p) ⇒ Object
-
#perfdata ⇒ Object
returns the performance data of the plugin as a PerformanceData object.
- #perfdata!(label, value, f = {}) ⇒ Object
-
#status ⇒ Status
Plugin status.
- #to_str ⇒ Object
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, = {}) @tag = tag @block = block @plugins = Hash.new @_args = Hash.new @_data = OpenStruct.new :status => Status.new, :output => Output.new, :payload => nil, :perfdata => nil = OpenStruct.new DEFAULT_META.merge begin; instance_eval &block ; rescue => e; nil ; end end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
11 12 13 |
# File 'lib/naplug/plugin.rb', line 11 def block @block end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
11 12 13 |
# File 'lib/naplug/plugin.rb', line 11 def end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
11 12 13 |
# File 'lib/naplug/plugin.rb', line 11 def plugins @plugins end |
#tag ⇒ Object (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 |
#args ⇒ Object
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! ? nil : .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! ? nil : .enabled = true end |
#eval ⇒ Object
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
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
51 52 53 |
# File 'lib/naplug/plugin.rb', line 51 def is_disabled? not .enabled end |
#is_enabled? ⇒ Boolean
true when plugin is enabled; false otherwise
46 47 48 |
# File 'lib/naplug/plugin.rb', line 46 def is_enabled? .enabled end |
#is_meta? ⇒ True, False
Returns true if this plugin is a metaplugin, false otherwise.
31 32 33 |
# File 'lib/naplug/plugin.rb', line 31 def .status end |
#long_output ⇒ Object
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 |
#output ⇒ String
Gets 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
79 80 81 |
# File 'lib/naplug/plugin.rb', line 79 def output!(text_output) @_data.output.text_output = text_output end |
#parent ⇒ Object
55 56 57 |
# File 'lib/naplug/plugin.rb', line 55 def parent .parent end |
#payload ⇒ Object
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 |
#perfdata ⇒ Object
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 |
#status ⇒ Status
Returns plugin status.
66 67 68 |
# File 'lib/naplug/plugin.rb', line 66 def status @_data.status end |
#to_str ⇒ Object
130 131 132 |
# File 'lib/naplug/plugin.rb', line 130 def to_str '%s: %s' % [status,output] end |