Module: Boson::Loader

Included in:
Library
Defined in:
lib/boson/loader.rb

Overview

This module is mixed into Library to give it load() functionality. When creating your own Library subclass, you should at least override load_source_and_set_module.

Instance Method Summary collapse

Instance Method Details

#actual_load_commandsObject

Actually includes module and its commands



72
73
74
# File 'lib/boson/loader.rb', line 72

def actual_load_commands
  include_in_universe
end

#after_includeObject

Method hook for @module after it's been included



61
# File 'lib/boson/loader.rb', line 61

def after_include; end

#before_load_commandsObject

Method hook called after @module has been created



69
# File 'lib/boson/loader.rb', line 69

def before_load_commands; end

#clean_library_commandsObject

Cleans @commands from set_library_commands



88
89
90
91
92
93
# File 'lib/boson/loader.rb', line 88

def clean_library_commands
  aliases = @commands_hash.select {|k,v| @commands.include?(k) }.
    map {|k,v| v[:alias] }.compact
  @commands -= aliases
  @commands.uniq!
end

#detect_additions(options = {}, &block) ⇒ Object

Wraps around module loading for unexpected additions



38
39
40
41
42
# File 'lib/boson/loader.rb', line 38

def detect_additions(options={}, &block)
  Util.detect(options, &block).tap do |detected|
    @commands.concat detected[:methods].map(&:to_s)
  end
end

#handle_method_conflict_error(err) ⇒ Object

called when MethodConflictError is rescued



64
65
66
# File 'lib/boson/loader.rb', line 64

def handle_method_conflict_error(err)
  raise err
end

#loadObject

Loads a library and its dependencies and returns true if library loads correctly.



16
17
18
19
20
21
22
23
# File 'lib/boson/loader.rb', line 16

def load
  load_source_and_set_module
  module_callbacks if @module
  yield if block_given? # load dependencies
  detect_additions { load_commands } if load_commands?
  set_library_commands
  loaded_correctly? && (@loaded = true)
end

#load_commandsObject

Prepares for command loading, loads commands and rescues certain errors.



45
46
47
48
49
50
51
52
53
# File 'lib/boson/loader.rb', line 45

def load_commands
  @module = @module ? Util.constantize(@module) :
    Util.create_module(Boson::Commands, clean_name)
  before_load_commands
  check_for_method_conflicts unless @force
  actual_load_commands
rescue MethodConflictError => err
  handle_method_conflict_error err
end

#load_commands?Boolean

Determines if load_commands should be called

Returns:

  • (Boolean)


33
34
35
# File 'lib/boson/loader.rb', line 33

def load_commands?
  @module
end

#load_source_and_set_moduleObject

Method hook at the beginning of #load. This method should load the source and set instance variables necessary to make a library valid i.e. @module.



27
# File 'lib/boson/loader.rb', line 27

def load_source_and_set_module; end

#loaded_correctly?Boolean

Boolean which indicates if library loaded correctly.

Returns:

  • (Boolean)


56
57
58
# File 'lib/boson/loader.rb', line 56

def loaded_correctly?
  !!@module
end

#method_conflictsObject

Returns array of method conflicts



77
78
79
80
# File 'lib/boson/loader.rb', line 77

def method_conflicts
  (@module.instance_methods + @module.private_instance_methods) &
    (Boson.main_object.methods + Boson.main_object.private_methods)
end

#module_callbacksObject

Method hook for @module before loading



30
# File 'lib/boson/loader.rb', line 30

def module_callbacks; end

#set_library_commandsObject

Handles setting and cleaning @commands



83
84
85
# File 'lib/boson/loader.rb', line 83

def set_library_commands
  clean_library_commands
end