Class: Boson::Manager

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/boson/manager.rb

Overview

Handles loading of libraries and commands.

Defined Under Namespace

Modules: API

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#after_create_commands, #before_create_commands, #during_after_load, #handle_load_action_error, #load_dependencies

Constructor Details

#initializeManager

Returns a new instance of Manager



39
40
41
# File 'lib/boson/manager.rb', line 39

def initialize
  @failed_libraries = []
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance



21
22
23
# File 'lib/boson/manager.rb', line 21

def instance
  @instance
end

Instance Attribute Details

#failed_librariesObject

Returns the value of attribute failed_libraries



38
39
40
# File 'lib/boson/manager.rb', line 38

def failed_libraries
  @failed_libraries
end

#verboseObject

Returns the value of attribute verbose



38
39
40
# File 'lib/boson/manager.rb', line 38

def verbose
  @verbose
end

Class Method Details

.add_library(lib) ⇒ Object

Adds a library to Boson.libraries



28
29
30
31
# File 'lib/boson/manager.rb', line 28

def self.add_library(lib)
  Boson.libraries.delete(Boson.library(lib.name))
  Boson.libraries << lib
end

.load(libraries, options = {}) ⇒ Object

Loads a library or an array of libraries with options. Manager loads the first library subclass to return true for Library#handles. Any options that aren't listed here are passed as library attributes to the libraries (see Library.new)

Examples:

Manager.load MyRunner

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :verbose (Boolean)

    Prints each library's loaded status along with more verbose errors. Default is false.



17
18
19
# File 'lib/boson/manager.rb', line 17

def self.load(libraries, options={})
  instance.load(libraries, options)
end

.loaded?(lib_name) ⇒ Boolean

Given a library name, determines if it's loaded

Returns:

  • (Boolean)


34
35
36
# File 'lib/boson/manager.rb', line 34

def self.loaded?(lib_name)
  ((lib = Boson.library(lib_name)) && lib.loaded) ? true : false
end

Instance Method Details

#add_failed_library(library) ⇒ Object

Adds a library to the failed list



51
52
53
# File 'lib/boson/manager.rb', line 51

def add_failed_library(library)
  failed_libraries << library
end

#after_loadObject

Called after a library is loaded



56
57
58
59
60
61
62
# File 'lib/boson/manager.rb', line 56

def after_load
  create_commands(@library)
  self.class.add_library(@library)
  puts "Loaded library #{@library.name}" if verbose
  during_after_load
  true
end

#load(libraries, options = {}) ⇒ Object

Loads libraries



44
45
46
47
48
# File 'lib/boson/manager.rb', line 44

def load(libraries, options={})
  Array(libraries).map {|e|
    (@library = load_once(e, options)) ? after_load : false
  }.all?
end

#redefine_commands(lib, commands) ⇒ Object

Redefines commands



65
66
67
68
69
70
71
72
73
74
# File 'lib/boson/manager.rb', line 65

def redefine_commands(lib, commands)
  option_commands = lib.command_objects(commands).select(&:option_command?)
  accepted, rejected = option_commands.partition {|e|
    e.args(lib) || e.arg_size }
  if verbose && rejected.size > 0
    puts "Following commands cannot have options until their arguments " +
      "are configured: " + rejected.map {|e| e.name}.join(', ')
  end
  accepted.each {|cmd| Scientist.redefine_command(lib.namespace_object, cmd) }
end