Class: Nucleon::Plugin::Base

Inherits:
Core show all
Defined in:
lib/core/plugin/base.rb

Instance Attribute Summary

Attributes inherited from Core

#logger, #ui

Class Method Summary collapse

Instance Method Summary collapse

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

#[], #[]=, #append, 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, #prepend, #set, #string, string, string_map, #string_map, symbol, #symbol, #symbol_array, #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, options)
  @actor = Nucleon.handle(self)

  config = Util::Data.clean(Config.ensure(options), false)
  name   = Util::Data.ensure_value(config.delete(:plugin_name), config.delete(:name, provider))

  @quiet = config.delete(:quiet, false)

  set_meta(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



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/core/plugin/base.rb', line 346

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

.codeObject


Status codes



142
143
144
# File 'lib/core/plugin/base.rb', line 142

def self.code
  Nucleon.code
end

.codes(*codes) ⇒ Object



150
151
152
# File 'lib/core/plugin/base.rb', line 150

def self.codes(*codes)
  Nucleon.codes(*codes)
end

.init_plugin_collection(*external_block_methods) ⇒ Object




407
408
409
410
411
412
413
414
415
416
417
# File 'lib/core/plugin/base.rb', line 407

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_idsObject



6
7
8
# File 'lib/core/plugin/base.rb', line 6

def self.register_ids
  [ :plugin_name, :name ]
end

.translate(data) ⇒ Object




373
374
375
376
# File 'lib/core/plugin/base.rb', line 373

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




380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/core/plugin/base.rb', line 380

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




438
439
440
441
442
443
444
445
# File 'lib/core/plugin/base.rb', line 438

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



217
218
219
# File 'lib/core/plugin/base.rb', line 217

def ask(message, options = {})
  ui.ask(message, options)
end

#codeObject



146
147
148
# File 'lib/core/plugin/base.rb', line 146

def code
  self.class.code
end

#codes(*codes) ⇒ Object



154
155
156
# File 'lib/core/plugin/base.rb', line 154

def codes(*codes)
  self.class.codes(*codes)
end

#error(message, options = {}) ⇒ Object




314
315
316
317
318
319
320
321
322
# File 'lib/core/plugin/base.rb', line 314

def error(message, options = {})
  config = Config.new(options).import({ :operation => :error })

  unless quiet?
    message = render_message(message, config)
    ui.error(message, config.export)
  end
  message
end

#extended_config(type, options = {}) ⇒ Object




190
191
192
193
194
# File 'lib/core/plugin/base.rb', line 190

def extended_config(type, options = {})
  config = Nucleon.config(type, Config.ensure(options).import({ :plugin => myself }))
  config.delete(:plugin)
  config
end

#extension(hook, options = {}, &code) ⇒ Object




184
185
186
# File 'lib/core/plugin/base.rb', line 184

def extension(hook, options = {}, &code)
  Nucleon.exec(hook_method(hook), Config.ensure(options).import({ :plugin => myself }), &code)
end

#extension_check(hook, options = {}) ⇒ Object




198
199
200
# File 'lib/core/plugin/base.rb', line 198

def extension_check(hook, options = {})
  Nucleon.check(hook_method(hook), Config.ensure(options).import({ :plugin => myself }))
end

#extension_collect(hook, options = {}) ⇒ Object




210
211
212
# File 'lib/core/plugin/base.rb', line 210

def extension_collect(hook, options = {})
  Nucleon.collect(hook_method(hook), Config.ensure(options).import({ :plugin => myself }))
end

#extension_set(hook, value, options = {}) ⇒ Object




204
205
206
# File 'lib/core/plugin/base.rb', line 204

def extension_set(hook, value, options = {})
  Nucleon.value(hook_method(hook), value, Config.ensure(options).import({ :plugin => myself }))
end

#hook_method(hook) ⇒ Object


Extensions



178
179
180
# File 'lib/core/plugin/base.rb', line 178

def hook_method(hook)
  "#{plugin_type}_#{plugin_provider}_#{hook}"
end

#info(message, options = {}) ⇒ Object




290
291
292
293
294
295
296
297
298
# File 'lib/core/plugin/base.rb', line 290

def info(message, options = {})
  config = Config.new(options).import({ :operation => :info })

  unless quiet?
    message = render_message(message, config)
    ui.info(message, config.export)
  end
  message
end

#metaObject




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

def meta
  return @meta
end

#myselfObject 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



171
172
173
# File 'lib/core/plugin/base.rb', line 171

def normalize(reload)
  # Implement in sub classes
end

#parallel_finalizeObject




34
35
36
# File 'lib/core/plugin/base.rb', line 34

def parallel_finalize
  remove_plugin
end

#password(type, options = {}) ⇒ Object




223
224
225
# File 'lib/core/plugin/base.rb', line 223

def password(type, options = {})
  ui.password(type, options)
end

#plugin_directoryObject




113
114
115
# File 'lib/core/plugin/base.rb', line 113

def plugin_directory
  return meta.get(:directory)
end

#plugin_fileObject




119
120
121
# File 'lib/core/plugin/base.rb', line 119

def plugin_file
  return meta.get(:file)
end

#plugin_instance_nameObject




125
126
127
# File 'lib/core/plugin/base.rb', line 125

def plugin_instance_name
  return meta.get(:instance_name)
end

#plugin_nameObject




103
104
105
# File 'lib/core/plugin/base.rb', line 103

def plugin_name
  return meta.get(:name)
end

#plugin_name=(plugin_name) ⇒ Object



107
108
109
# File 'lib/core/plugin/base.rb', line 107

def plugin_name=plugin_name
  meta.set(:name, string(plugin_name))
end

#plugin_namespaceObject




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

def plugin_namespace
  return meta.get(:namespace)
end

#plugin_parentObject



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

def plugin_parent
  return meta.get(:parent)
end

#plugin_parent=(parent) ⇒ Object




131
132
133
# File 'lib/core/plugin/base.rb', line 131

def plugin_parent=parent
  meta.set(:parent, parent) if parent.is_a?(Nucleon::Plugin::Base)
end

#plugin_providerObject




97
98
99
# File 'lib/core/plugin/base.rb', line 97

def plugin_provider
  return meta.get(:provider)
end

#plugin_typeObject




91
92
93
# File 'lib/core/plugin/base.rb', line 91

def plugin_type
  return meta.get(:type)
end

#prefixed_message(type, prefix, message, options = {}) ⇒ Object




338
339
340
341
# File 'lib/core/plugin/base.rb', line 338

def prefixed_message(type, prefix, message, options = {})
  return unless [ :info, :warn, :error, :success ].include?(type.to_sym)
  send(type, prefix.to_s + render_message(message, Config.new(options).import({ :prefix => false, :operation => type.to_sym }).export), Config.new(options).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

Returns:

  • (Boolean)


53
54
55
# File 'lib/core/plugin/base.rb', line 53

def quiet?
  @quiet
end

#remove_pluginObject




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




275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/core/plugin/base.rb', line 275

def render(data, options = {})
  config = Config.ensure(options)

  if ! quiet? || config[:silent]
    translator  = nil
    translator  = Nucleon.translator({}, config[:format]) if config[:format]
    data        = translator.generate(data) if translator

    ui.dump(data, options) unless data.strip.empty? || config[:silent]
  end
  data
end

#safe_exec(return_result = true, &code) ⇒ Object




421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/core/plugin/base.rb', line 421

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.message)

    error(error.message, { :prefix => false, :i18n => false }) if error.message
  end
  return false
end

#set_meta(meta) ⇒ Object




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

def set_meta(meta)
  @meta = Config.ensure(meta)
end

#statusObject



164
165
166
# File 'lib/core/plugin/base.rb', line 164

def status
  meta.get(:status, code.unknown_status)
end

#status=(status) ⇒ Object




160
161
162
# File 'lib/core/plugin/base.rb', line 160

def status=status
  meta.set(:status, status)
end

#success(message, options = {}) ⇒ Object




326
327
328
329
330
331
332
333
334
# File 'lib/core/plugin/base.rb', line 326

def success(message, options = {})
  config = Config.new(options).import({ :operation => :success })

  unless quiet?
    message = render_message(message, config)
    ui.success(message, config.export)
  end
  message
end

#translate_reference(reference, editable = false) ⇒ Object




401
402
403
# File 'lib/core/plugin/base.rb', line 401

def translate_reference(reference, editable = false)
  myself.class.translate_reference(reference, editable)
end

#warn(message, options = {}) ⇒ Object




302
303
304
305
306
307
308
309
310
# File 'lib/core/plugin/base.rb', line 302

def warn(message, options = {})
  config = Config.new(options).import({ :operation => :warn })

  unless quiet?
    message = render_message(message, config)
    ui.warn(message, config.export)
  end
  message
end