Class: Gloo::Plugin::LibManager

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/plugin/lib_manager.rb

Constant Summary collapse

GEM_PREFIX =

Constants for library management

'gloo-'
INIT_CLASS_SUFFIX =
'Init'

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ LibManager

Set up the library manager.



19
20
21
22
23
# File 'lib/gloo/plugin/lib_manager.rb', line 19

def initialize( engine )
  @engine = engine
  @libraries = {}
  @engine.log.debug 'library manager intialized...'
end

Instance Method Details

#core_lib_name(name) ⇒ Object

Get the start file for the gem core library.



35
36
37
# File 'lib/gloo/plugin/lib_manager.rb', line 35

def core_lib_name name
  return GEM_PREFIX + name
end

#init_class_name(name) ⇒ Object

Get the library initialization class name.



42
43
44
# File 'lib/gloo/plugin/lib_manager.rb', line 42

def init_class_name name
  return name.camelize + INIT_CLASS_SUFFIX
end

#load_lib(name) ⇒ Object

Load the library of the given name. The name will correspond with the name of a directory within the gloo libraries directory.



51
52
53
54
55
56
57
58
# File 'lib/gloo/plugin/lib_manager.rb', line 51

def load_lib( name )
  @engine.log.debug "Loading core library: #{name}"
  gem_name = core_lib_name name
  
  @libraries[name] = gem_name
  register_library name, gem_name
  @engine.log.debug "Core library loaded: #{name}"
end

#loaded_librariesObject

Get the loaded libraries.



28
29
30
# File 'lib/gloo/plugin/lib_manager.rb', line 28

def loaded_libraries
  return @libraries
end

#register_library(name, gem_name) ⇒ Object

Give the library a chance to register its verbs and objects.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gloo/plugin/lib_manager.rb', line 80

def register_library( name, gem_name )
  require_gem gem_name

  class_name = init_class_name name
  @engine.log.debug "Looking for library entry point class to register: #{class_name}"
  begin
    plugin_class = Object.const_get( class_name )
    inst = plugin_class.new
    lib_cb = Callback.new( @engine )
    inst.register( lib_cb )
  rescue NameError => ex
    @engine.log.error "Warning: Could not find class #{class_name}", ex
  end
end

#require_gem(gem_name) ⇒ Object

Require a gem by name, installing it if it’s not available.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gloo/plugin/lib_manager.rb', line 63

def require_gem gem_name
  @engine.log.debug "Going to require gem: #{gem_name}"
  begin
    gem gem_name
  rescue Gem::LoadError
    @engine.log.info "Gem not found, attempting to install: #{gem_name}"
    system("gem install #{gem_name}")
    Gem.clear_paths
    gem gem_name
  end

  require gem_name
end