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 }
- VALID_META_OPTIONS =
[ :debug, :state, :description, :parent ]
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
- #description ⇒ 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.
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, ) @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 begin instance_eval &block rescue ArgumentError => e raise rescue 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 @meta 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
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 |
#args ⇒ Object
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 |
#description ⇒ Object
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! ? 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! ? nil : @_meta.enabled = true end |
#eval ⇒ Object
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
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
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
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.
40 41 42 |
# File 'lib/naplug/plugin.rb', line 40 def @_meta.status end |
#long_output ⇒ Object
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 |
#output ⇒ String
Gets 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
92 93 94 |
# File 'lib/naplug/plugin.rb', line 92 def output!(text_output) @_data.output.text_output = text_output end |
#parent ⇒ Object
64 65 66 |
# File 'lib/naplug/plugin.rb', line 64 def parent @_meta.parent end |
#payload ⇒ Object
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 |
#perfdata ⇒ Object
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 |
#status ⇒ Status
Returns plugin status.
79 80 81 |
# File 'lib/naplug/plugin.rb', line 79 def status @_data.status end |
#to_str ⇒ Object
143 144 145 |
# File 'lib/naplug/plugin.rb', line 143 def to_str '%s: %s' % [status,output] end |