Class: Nucleon::Plugin::Base
- Defined in:
- lib/core/plugin/base.rb
Instance Attribute Summary
Attributes inherited from Core
Class Method Summary collapse
-
.build_info(namespace, plugin_type, data) ⇒ Object
—————————————————————————– Utilities.
-
.init_plugin_collection(*external_block_methods) ⇒ Object
—.
- .register_ids ⇒ Object
-
.translate(data) ⇒ Object
—.
-
.translate_reference(reference, editable = false) ⇒ Object
—.
Instance Method Summary collapse
-
#admin_exec(return_result = true, &block) ⇒ Object
—.
-
#ask(message, options = {}) ⇒ Object
—————————————————————————– Input.
-
#code ⇒ Object
—————————————————————————– Status codes.
- #codes(*codes) ⇒ Object
-
#error(message, options = {}) ⇒ Object
—.
-
#extended_config(type, options = {}) ⇒ Object
—.
-
#extension(hook, options = {}, &code) ⇒ Object
—.
-
#extension_check(hook, options = {}) ⇒ Object
—.
-
#extension_collect(hook, options = {}) ⇒ Object
—.
-
#extension_set(hook, value, options = {}) ⇒ Object
—.
-
#hook_method(hook) ⇒ Object
—————————————————————————– Extensions.
-
#info(message, options = {}) ⇒ Object
—.
-
#initialize(namespace, plugin_type, provider, options) ⇒ Base
constructor
All Plugin classes should directly or indirectly extend Base.
-
#meta ⇒ Object
—.
-
#method_missing(method, *args, &block) ⇒ Object
—.
-
#myself ⇒ Object
(also: #me)
—————————————————————————– Property accessor / modifiers.
-
#normalize(reload) ⇒ Object
—————————————————————————– Plugin operations.
-
#parallel_finalize ⇒ Object
—.
-
#password(type, options = {}) ⇒ Object
—.
-
#plugin_directory ⇒ Object
—.
-
#plugin_file ⇒ Object
—.
-
#plugin_instance_name ⇒ Object
—.
-
#plugin_name ⇒ Object
—.
- #plugin_name=(plugin_name) ⇒ Object
-
#plugin_namespace ⇒ Object
—.
- #plugin_parent ⇒ Object
-
#plugin_parent=(parent) ⇒ Object
—.
-
#plugin_provider ⇒ Object
—.
-
#plugin_type ⇒ Object
—.
-
#prefixed_message(type, prefix, message, options = {}) ⇒ Object
—.
-
#quiet=(quiet) ⇒ Object
—.
-
#quiet? ⇒ Boolean
—————————————————————————– Checks.
-
#remove_plugin ⇒ Object
—.
-
#render(data, options = {}) ⇒ Object
—.
-
#safe_exec(return_result = true, &code) ⇒ Object
—.
-
#set_meta(meta) ⇒ Object
—.
- #status ⇒ Object
-
#status=(status) ⇒ Object
—.
-
#success(message, options = {}) ⇒ Object
—.
-
#translate_reference(reference, editable = false) ⇒ Object
—.
-
#warn(message, options = {}) ⇒ Object
—.
Methods inherited from Core
#initialized?, logger, ui, ui_group, #ui_group
Methods included from Mixin::Colors
#black, #blue, #cyan, #green, #grey, #purple, #red, #yellow
Methods inherited from Config
#[], #[]=, #array, array, #clear, #defaults, #delete, #empty?, ensure, #export, filter, #filter, #get, #get_array, #get_hash, #has_key?, hash, #hash, #import, #init, init, init_flat, #keys, #set, #string, string, #string_map, string_map, symbol, #symbol, symbol_map, #symbol_map, test, #test
Methods included from Mixin::ConfigOptions
#all_options, #clear_options, #contexts, #get_options, #set_options
Methods included from Mixin::ConfigCollection
#all_properties, #clear_properties, #delete_property, #get_property, #save_properties, #set_property
Constructor Details
#initialize(namespace, plugin_type, provider, options) ⇒ Base
All Plugin classes should directly or indirectly extend Base
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/core/plugin/base.rb', line 12 def initialize(namespace, plugin_type, provider, ) @actor = Nucleon.handle(self) config = Util::Data.clean(Config.ensure(), false) name = Util::Data.ensure_value(config.delete(:plugin_name), config.delete(:name, provider)) @quiet = config.delete(:quiet, false) (config.delete(:meta, Config.new)) # No logging statements above this line!! super(config.import({ :logger => "#{namespace}->#{plugin_type}->#{plugin_provider}" }), {}, true, false, false) myself.plugin_name = name logger.debug("Normalizing #{namespace} #{plugin_type} plugin #{plugin_name}") normalize(false) @initialized = true end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
40 41 42 |
# File 'lib/core/plugin/base.rb', line 40 def method_missing(method, *args, &block) return nil end |
Class Method Details
.build_info(namespace, plugin_type, data) ⇒ Object
Utilities
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/core/plugin/base.rb', line 338 def self.build_info(namespace, plugin_type, data) plugins = [] if data.is_a?(Hash) data = [ data ] end logger.debug("Building plugin list of #{plugin_type}") if data.is_a?(Array) data.each do |info| unless Util::Data.empty?(info) info = translate(info) if Util::Data.empty?(info[:provider]) info[:provider] = Nucleon.type_default(namespace, plugin_type) end plugins << info end end end return plugins end |
.init_plugin_collection(*external_block_methods) ⇒ Object
399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/core/plugin/base.rb', line 399 def self.init_plugin_collection(*external_block_methods) logger.debug("Initializing plugin collection interface at #{Time.now}") include Parallel external_block_exec(*external_block_methods) include Mixin::Settings include Mixin::SubConfig extend Mixin::Macro::PluginInterface end |
.register_ids ⇒ Object
6 7 8 |
# File 'lib/core/plugin/base.rb', line 6 def self.register_ids [ :plugin_name, :name ] end |
.translate(data) ⇒ Object
365 366 367 368 |
# File 'lib/core/plugin/base.rb', line 365 def self.translate(data) logger.debug("Translating input data to internal plugin structure") return ( data.is_a?(Hash) ? symbol_map(data) : data ) end |
.translate_reference(reference, editable = false) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/core/plugin/base.rb', line 372 def self.translate_reference(reference, editable = false) # ex: provider:::name if reference && reference.match(/^\s*([a-zA-Z0-9_-]+)(?::::)?(.*)?\s*$/) provider = $1 name = $2 logger.debug("Translating plugin reference: #{provider} #{name}") info = { :provider => provider, :name => name } logger.debug("Plugin reference info: #{info.inspect}") return info end nil end |
Instance Method Details
#admin_exec(return_result = true, &block) ⇒ Object
430 431 432 433 434 435 436 437 |
# File 'lib/core/plugin/base.rb', line 430 def admin_exec(return_result = true, &block) if Nucleon.admin? safe_exec(return_result, &block) else warn("The #{plugin_provider} action must be run as a machine administrator", { :i18n => false }) myself.status = code.access_denied end end |
#ask(message, options = {}) ⇒ Object
Input
209 210 211 |
# File 'lib/core/plugin/base.rb', line 209 def ask(, = {}) ui.ask(, ) end |
#code ⇒ Object
Status codes
142 143 144 |
# File 'lib/core/plugin/base.rb', line 142 def code Nucleon.code end |
#codes(*codes) ⇒ Object
146 147 148 |
# File 'lib/core/plugin/base.rb', line 146 def codes(*codes) Nucleon.codes(*codes) end |
#error(message, options = {}) ⇒ Object
306 307 308 309 310 311 312 313 314 |
# File 'lib/core/plugin/base.rb', line 306 def error(, = {}) config = Config.new().import({ :operation => :error }) unless quiet? = (, config) ui.error(, config.export) end end |
#extended_config(type, options = {}) ⇒ Object
182 183 184 185 186 |
# File 'lib/core/plugin/base.rb', line 182 def extended_config(type, = {}) config = Nucleon.config(type, Config.ensure().import({ :plugin => myself })) config.delete(:plugin) config end |
#extension(hook, options = {}, &code) ⇒ Object
176 177 178 |
# File 'lib/core/plugin/base.rb', line 176 def extension(hook, = {}, &code) Nucleon.exec(hook_method(hook), Config.ensure().import({ :plugin => myself }), &code) end |
#extension_check(hook, options = {}) ⇒ Object
190 191 192 |
# File 'lib/core/plugin/base.rb', line 190 def extension_check(hook, = {}) Nucleon.check(hook_method(hook), Config.ensure().import({ :plugin => myself })) end |
#extension_collect(hook, options = {}) ⇒ Object
202 203 204 |
# File 'lib/core/plugin/base.rb', line 202 def extension_collect(hook, = {}) Nucleon.collect(hook_method(hook), Config.ensure().import({ :plugin => myself })) end |
#extension_set(hook, value, options = {}) ⇒ Object
196 197 198 |
# File 'lib/core/plugin/base.rb', line 196 def extension_set(hook, value, = {}) Nucleon.value(hook_method(hook), value, Config.ensure().import({ :plugin => myself })) end |
#hook_method(hook) ⇒ Object
Extensions
170 171 172 |
# File 'lib/core/plugin/base.rb', line 170 def hook_method(hook) "#{plugin_type}_#{plugin_provider}_#{hook}" end |
#info(message, options = {}) ⇒ Object
282 283 284 285 286 287 288 289 290 |
# File 'lib/core/plugin/base.rb', line 282 def info(, = {}) config = Config.new().import({ :operation => :info }) unless quiet? = (, config) ui.info(, config.export) end end |
#meta ⇒ Object
73 74 75 |
# File 'lib/core/plugin/base.rb', line 73 def return end |
#myself ⇒ Object Also known as: me
Property accessor / modifiers
60 61 62 |
# File 'lib/core/plugin/base.rb', line 60 def myself @actor end |
#normalize(reload) ⇒ Object
Plugin operations
163 164 165 |
# File 'lib/core/plugin/base.rb', line 163 def normalize(reload) # Implement in sub classes end |
#parallel_finalize ⇒ Object
34 35 36 |
# File 'lib/core/plugin/base.rb', line 34 def parallel_finalize remove_plugin end |
#password(type, options = {}) ⇒ Object
215 216 217 |
# File 'lib/core/plugin/base.rb', line 215 def password(type, = {}) ui.password(type, ) end |
#plugin_directory ⇒ Object
113 114 115 |
# File 'lib/core/plugin/base.rb', line 113 def plugin_directory return .get(:directory) end |
#plugin_file ⇒ Object
119 120 121 |
# File 'lib/core/plugin/base.rb', line 119 def plugin_file return .get(:file) end |
#plugin_instance_name ⇒ Object
125 126 127 |
# File 'lib/core/plugin/base.rb', line 125 def plugin_instance_name return .get(:instance_name) end |
#plugin_name ⇒ Object
103 104 105 |
# File 'lib/core/plugin/base.rb', line 103 def plugin_name return .get(:name) end |
#plugin_name=(plugin_name) ⇒ Object
107 108 109 |
# File 'lib/core/plugin/base.rb', line 107 def plugin_name=plugin_name .set(:name, string(plugin_name)) end |
#plugin_namespace ⇒ Object
85 86 87 |
# File 'lib/core/plugin/base.rb', line 85 def plugin_namespace return .get(:namespace) end |
#plugin_parent ⇒ Object
135 136 137 |
# File 'lib/core/plugin/base.rb', line 135 def plugin_parent return .get(:parent) end |
#plugin_parent=(parent) ⇒ Object
131 132 133 |
# File 'lib/core/plugin/base.rb', line 131 def plugin_parent=parent .set(:parent, parent) if parent.is_a?(Nucleon::Plugin::Base) end |
#plugin_provider ⇒ Object
97 98 99 |
# File 'lib/core/plugin/base.rb', line 97 def plugin_provider return .get(:provider) end |
#plugin_type ⇒ Object
91 92 93 |
# File 'lib/core/plugin/base.rb', line 91 def plugin_type return .get(:type) end |
#prefixed_message(type, prefix, message, options = {}) ⇒ Object
330 331 332 333 |
# File 'lib/core/plugin/base.rb', line 330 def (type, prefix, , = {}) return unless [ :info, :warn, :error, :success ].include?(type.to_sym) send(type, prefix.to_s + (, Config.new().import({ :prefix => false, :operation => type.to_sym }).export), Config.new().import({ :i18n => false }).export) end |
#quiet=(quiet) ⇒ Object
67 68 69 |
# File 'lib/core/plugin/base.rb', line 67 def quiet=quiet @quiet = quiet end |
#quiet? ⇒ Boolean
Checks
53 54 55 |
# File 'lib/core/plugin/base.rb', line 53 def quiet? @quiet end |
#remove_plugin ⇒ Object
46 47 48 |
# File 'lib/core/plugin/base.rb', line 46 def remove_plugin # Implement in sub classes if needed for cleanup end |
#render(data, options = {}) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/core/plugin/base.rb', line 267 def render(data, = {}) config = Config.ensure() if ! quiet? || config[:silent] translator = nil translator = CORL.translator({}, config[:format]) if config[:format] data = translator.generate(data) if translator ui.dump(data, ) unless data.strip.empty? || config[:silent] end data end |
#safe_exec(return_result = true, &code) ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/core/plugin/base.rb', line 413 def safe_exec(return_result = true, &code) begin result = code.call return result if return_result return true rescue => error logger.error(error.inspect) logger.error(error.) error(error., { :prefix => false, :i18n => false }) if error. end return false end |
#set_meta(meta) ⇒ Object
79 80 81 |
# File 'lib/core/plugin/base.rb', line 79 def () = Config.ensure() end |
#status ⇒ Object
156 157 158 |
# File 'lib/core/plugin/base.rb', line 156 def status .get(:status, code.unknown_status) end |
#status=(status) ⇒ Object
152 153 154 |
# File 'lib/core/plugin/base.rb', line 152 def status=status .set(:status, status) end |
#success(message, options = {}) ⇒ Object
318 319 320 321 322 323 324 325 326 |
# File 'lib/core/plugin/base.rb', line 318 def success(, = {}) config = Config.new().import({ :operation => :success }) unless quiet? = (, config) ui.success(, config.export) end end |
#translate_reference(reference, editable = false) ⇒ Object
393 394 395 |
# File 'lib/core/plugin/base.rb', line 393 def translate_reference(reference, editable = false) myself.class.translate_reference(reference, editable) end |
#warn(message, options = {}) ⇒ Object
294 295 296 297 298 299 300 301 302 |
# File 'lib/core/plugin/base.rb', line 294 def warn(, = {}) config = Config.new().import({ :operation => :warn }) unless quiet? = (, config) ui.warn(, config.export) end end |